Skip to content

Commit

Permalink
fix: Allow the user to specify the timeout (Fixes #298, backported from
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyJones committed Sep 8, 2021
1 parent 9c20478 commit c1fd849
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ var server = pact.createStub({
| sslcert | false | string | Path to a custom self-signed SSL cert file, 'ssl' option must be set to true to use this option. Defaults false | to none |
| sslkey | false | string | Path a custom key and self-signed SSL cert key file, 'ssl' option must be set to true to use this option false. Defaults to none |
| cors | false | boolean | Allow CORS OPTION requests to be accepted, defaults to 'false' |
| timeout | false | number | How long to wait for the stub server to start up (in milliseconds). Defaults to 30000 (30 seconds) |

### Message Pacts

Expand Down
1 change: 1 addition & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,6 @@ export interface ServerOptions {
provider?: string;
monkeypatch?: string;
logLevel?: LogLevel;
timeout?: number;
pactFileWriteMode?: 'overwrite' | 'update' | 'merge';
}
16 changes: 10 additions & 6 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ import needle = require('needle');
// Get a reference to the global setTimeout object in case it is mocked by a testing library later
const setTimeout = global.setTimeout;

const CHECKTIME = 500;
const RETRY_AMOUNT = 60;
const PROCESS_TIMEOUT = 30000;

const getTimeout = (options: ServiceOptions): number =>
options.timeout || 30000;

const getRetryTickTime = (options: ServiceOptions): number =>
Math.round(getTimeout(options) / RETRY_AMOUNT);
interface AbstractServiceEventInterface {
START_EVENT: string;
STOP_EVENT: string;
Expand Down Expand Up @@ -184,7 +187,7 @@ export abstract class AbstractService extends events.EventEmitter {
// check service is available
return this.__waitForServiceUp()
.timeout(
PROCESS_TIMEOUT,
getTimeout(this.options),
`Couldn't start Pact with PID: ${this.__instance.pid}`
)
.then(() => {
Expand All @@ -199,7 +202,7 @@ export abstract class AbstractService extends events.EventEmitter {
const pid = this.__instance ? this.__instance.pid : -1;
return q(spawn.killBinary(this.__instance))
.then(() => this.__waitForServiceDown())
.timeout(PROCESS_TIMEOUT, `Couldn't stop Pact with PID '${pid}'`)
.timeout(getTimeout(this.options), `Couldn't stop Pact with PID '${pid}'`)
.then(() => {
this.__running = false;
this.emit(AbstractService.Events.STOP_EVENT, this);
Expand Down Expand Up @@ -239,7 +242,7 @@ export abstract class AbstractService extends events.EventEmitter {
);
}
// eslint-disable-next-line @typescript-eslint/no-use-before-define
setTimeout(check.bind(this), CHECKTIME);
setTimeout(check.bind(this), getRetryTickTime(this.options));
};

const check = (): void => {
Expand Down Expand Up @@ -275,7 +278,7 @@ export abstract class AbstractService extends events.EventEmitter {
);
return;
}
setTimeout(check, CHECKTIME);
setTimeout(check, getRetryTickTime(this.options));
},
() => deferred.resolve()
);
Expand Down Expand Up @@ -332,6 +335,7 @@ export interface ServiceOptions {
sslkey?: string;
log?: string;
logLevel?: LogLevel;
timeout?: number;
}

// This is the pact binary's log level, which is a subset of the log levels for pact-node
Expand Down

0 comments on commit c1fd849

Please sign in to comment.