Skip to content

Commit 48365d3

Browse files
committed
chore: Correct issue with timeouts when test misconfigured
1 parent 9f3f4a2 commit 48365d3

2 files changed

Lines changed: 36 additions & 16 deletions

File tree

packages/case-core/src/core/ReadingCaseContract.ts

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ export class ReadingCaseContract extends BaseCaseContract {
8484
this.initialContext.logger.maintainerDebug(
8585
`Verifying contract between '${this.currentContract.description.consumerName}' and '${this.currentContract.description.providerName}'. There are '${this.currentContract.examples.length}' examples`,
8686
);
87-
const finishedIndicators = [];
87+
const interactionFinishedIndicators: Promise<unknown>[] = [];
88+
const interactionFinishers: Array<() => void> = [];
89+
8890
this.currentContract.examples.forEach((example, index) => {
8991
if (example.result !== 'VERIFIED') {
9092
throw new CaseCoreError(
@@ -96,11 +98,20 @@ export class ReadingCaseContract extends BaseCaseContract {
9698
this.initialContext.logger.maintainerDebug(
9799
`Preparing test framework's callback for: ${names.mockName} `,
98100
);
99-
let exampleFinished: () => void;
100101

101-
finishedIndicators.push(
102-
new Promise<void>((resolve) => {
103-
exampleFinished = resolve;
102+
interactionFinishedIndicators.push(
103+
new Promise<void>((resolve, reject) => {
104+
const timeout = setTimeout(() => {
105+
this.initialContext.logger.error(
106+
`Timeout in interaction[${index}]`,
107+
);
108+
reject(new Error(names.mockName));
109+
}, 30000);
110+
111+
interactionFinishers[index] = () => {
112+
clearTimeout(timeout);
113+
resolve();
114+
};
104115
}),
105116
);
106117
runTestCb(names.mockName, () =>
@@ -134,16 +145,20 @@ export class ReadingCaseContract extends BaseCaseContract {
134145
);
135146
})
136147
.finally(() => {
148+
this.initialContext.logger.deepMaintainerDebug(
149+
`Interaction[${index}] type of finisher`,
150+
interactionFinishers[index],
151+
);
137152
this.initialContext.logger.maintainerDebug(
138153
`Interaction[${index}] completed: ${names.mockName}`,
139154
);
140-
exampleFinished();
155+
interactionFinishers[index]?.();
141156
}),
142157
);
143158
});
144159
let publishFinished: () => void;
145-
146-
finishedIndicators.push(
160+
const publishFinishedIndicators = [];
161+
publishFinishedIndicators.push(
147162
new Promise<void>((resolve) => {
148163
publishFinished = resolve;
149164
}),
@@ -156,20 +171,25 @@ export class ReadingCaseContract extends BaseCaseContract {
156171
this.initialContext.logger.maintainerDebug(
157172
'Test callback for ending record',
158173
);
159-
return this.endRecord().finally(() => {
160-
this.initialContext.logger.maintainerDebug(
161-
`Publishing contract callback completed`,
162-
);
163-
publishFinished();
164-
});
174+
return Promise.allSettled(interactionFinishedIndicators)
175+
.then(() => this.endRecord())
176+
.finally(() => {
177+
this.initialContext.logger.maintainerDebug(
178+
`Publishing contract callback completed`,
179+
);
180+
publishFinished();
181+
});
165182
},
166183
);
167184
if (
168185
this.initialContext['_case:currentRun:context:internals'] &&
169186
this.initialContext['_case:currentRun:context:internals']
170187
.asyncVerification
171188
) {
172-
return Promise.all(finishedIndicators).then(() => {});
189+
return Promise.all([
190+
...interactionFinishedIndicators,
191+
...publishFinishedIndicators,
192+
]).then(() => {});
173193
}
174194
return undefined;
175195
}

packages/case-core/src/index.http.request.verification.misconfigured.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type * as http from 'node:http';
22

33
import { CaseConfigurationError } from '@contract-case/case-plugin-base';
4+
import { Mutex } from 'async-mutex';
45
import type { RunTestCallback } from './core/executeExample/types';
56
import type { StateHandlers } from './entities/states/types';
67

@@ -13,7 +14,6 @@ import { ReadingCaseContract } from './core';
1314
import { readerDependencies } from './connectors/dependencies';
1415
import { readContract } from './connectors/contractStore/contractReader';
1516
import { defaultPrinter } from './__tests__/jest/defaultTestPrinter';
16-
import { Mutex } from 'async-mutex';
1717

1818
describe('Server verification', () => {
1919
let server: http.Server;

0 commit comments

Comments
 (0)