Skip to content

Commit eb1493a

Browse files
committed
fix: add mutex to ensure correct result submission in multi-threaded tests
- Implemented a mutex to prevent duplicate result submissions when running tests in multiple threads. - Ensured thread-safe result handling for improved reliability.
1 parent 2d96a5e commit eb1493a

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

package-lock.json

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

qase-javascript-commons/changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2+
3+
## What's new
4+
5+
Added a mutex to ensure correct result submission when running tests in multiple threads, preventing potential
6+
duplication.
7+
18
29

310
## What's new

qase-javascript-commons/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "qase-javascript-commons",
3-
"version": "2.2.11",
3+
"version": "2.2.12",
44
"description": "Qase JS Reporters",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",
@@ -34,7 +34,8 @@
3434
"mime-types": "^2.1.33",
3535
"qaseio": "~2.4.0",
3636
"strip-ansi": "^6.0.1",
37-
"uuid": "^9.0.0"
37+
"uuid": "^9.0.0",
38+
"async-mutex": "~0.5.0"
3839
},
3940
"devDependencies": {
4041
"@jest/globals": "^29.5.0",

qase-javascript-commons/src/reporters/testops-reporter.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { QaseError } from '../utils/qase-error';
3333
import { LoggerInterface } from '../utils/logger';
3434
import axios from 'axios';
3535
import { StateManager } from '../state/state';
36+
import { Mutex } from 'async-mutex';
3637

3738
const defaultChunkSize = 200;
3839

@@ -164,6 +165,8 @@ export class TestOpsReporter extends AbstractReporter {
164165
*/
165166
private isTestRunReady = false;
166167

168+
private mutex = new Mutex();
169+
167170
/**
168171
* @param {LoggerInterface} logger
169172
* @param {ReporterOptionsType & TestOpsOptionsType} options
@@ -224,18 +227,24 @@ export class TestOpsReporter extends AbstractReporter {
224227
}
225228
}
226229

227-
await super.addTestResult(result);
230+
const release = await this.mutex.acquire();
231+
try {
228232

229-
if (!this.isTestRunReady) {
230-
return;
231-
}
233+
await super.addTestResult(result);
234+
235+
if (!this.isTestRunReady) {
236+
return;
237+
}
232238

233-
const countOfResults = this.batchSize + this.firstIndex;
239+
const countOfResults = this.batchSize + this.firstIndex;
234240

235-
if (this.results.length >= countOfResults) {
236-
const firstIndex = this.firstIndex;
237-
this.firstIndex = countOfResults;
238-
await this.publishResults(this.results.slice(firstIndex, countOfResults));
241+
if (this.results.length >= countOfResults) {
242+
const firstIndex = this.firstIndex;
243+
this.firstIndex = countOfResults;
244+
await this.publishResults(this.results.slice(firstIndex, countOfResults));
245+
}
246+
} finally {
247+
release();
239248
}
240249
}
241250

@@ -328,7 +337,12 @@ export class TestOpsReporter extends AbstractReporter {
328337
* @returns {Promise<void>}
329338
*/
330339
public async publish(): Promise<void> {
331-
await this.sendResults();
340+
const release = await this.mutex.acquire();
341+
try {
342+
await this.sendResults();
343+
} finally {
344+
release();
345+
}
332346
await this.complete();
333347
}
334348

0 commit comments

Comments
 (0)