Skip to content

Commit

Permalink
fix: Allow the user to specify the timeout (Fixes #298)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyJones committed Jun 21, 2021
1 parent a5c934a commit 4c77ddb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ var server = pact.createServer({
| `pactFileWriteMode` | false | `overwrite` OR `update` OR `merge` | Control how the pact file is created. Defaults to "overwrite" |
| `format` | false | `json` OR `xml` | Format to write the results as, either in JSON or XML, defaults to JSON |
| `out` | false | string | Write output to a file instead of returning it in the promise, defaults to none |
| `timeout` | false | number | How long to wait for the mock server to start up (in milliseconds). Defaults to 30000 (30 seconds) |

#### List Mock Servers

Expand Down Expand Up @@ -418,6 +419,8 @@ 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 @@ -104,5 +104,6 @@ export interface ServerOptions {
provider?: string;
monkeypatch?: string;
logLevel?: LogLevel;
timeout?: number;
pactFileWriteMode?: 'overwrite' | 'update' | 'merge';
}
19 changes: 12 additions & 7 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ import checkTypes = require('check-types');
// 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;
Expand Down Expand Up @@ -73,7 +77,7 @@ export abstract class AbstractService extends events.EventEmitter {
checkTypes.assert.number(options.port);
checkTypes.assert.integer(options.port);
checkTypes.assert.positive(options.port);
checkTypes.assert.inRange(options.port, 0, 65535);
checkTypes.inRange(options.port, 0, 65535);

if (checkTypes.not.inRange(options.port, 1024, 49151)) {
logger.warn(
Expand Down Expand Up @@ -180,7 +184,7 @@ export abstract class AbstractService extends events.EventEmitter {
);

// check service is available
return timeout(this.__waitForServiceUp(), PROCESS_TIMEOUT)
return timeout(this.__waitForServiceUp(), getTimeout(this.options))
.then(() => {
this.__running = true;
this.emit(AbstractService.Events.START_EVENT, this);
Expand All @@ -205,7 +209,7 @@ export abstract class AbstractService extends events.EventEmitter {
Promise.resolve(this.__instance)
.then(spawn.killBinary)
.then(() => this.__waitForServiceDown()),
PROCESS_TIMEOUT
getTimeout(this.options)
)
.catch((err: Error) => {
if (err instanceof TimeoutError) {
Expand Down Expand Up @@ -252,7 +256,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 @@ -286,7 +290,7 @@ export abstract class AbstractService extends events.EventEmitter {
);
return;
}
setTimeout(check, CHECKTIME);
setTimeout(check, getRetryTickTime(this.options));
},
() => resolve()
);
Expand Down Expand Up @@ -337,6 +341,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-core
Expand Down
1 change: 1 addition & 0 deletions src/stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ export interface StubOptions {
sslkey?: string;
log?: string;
logLevel?: LogLevel;
timeout?: number;
}

0 comments on commit 4c77ddb

Please sign in to comment.