Skip to content

Commit d98fcaa

Browse files
committed
fix(smoke-testing): Race condition during concurrent initialisation in Artillery's load testing framework.
1 parent 3a0e4a1 commit d98fcaa

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

e2e/artillery/processors/multi-endpoint.ts

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,42 @@ import {
3838
let sharedContext: any = null;
3939
let alicePkpAuthContext: any = null;
4040
let aliceCustomAuthContext: any = null;
41+
let initializationPromise: Promise<any> | null = null;
4142

4243
/**
4344
* Initialize the shared context once per Artillery run
4445
*/
4546
async function initializeSharedContext() {
4647
if (sharedContext) return sharedContext;
48+
49+
// Prevent race conditions by ensuring only one initialization happens
50+
if (initializationPromise) {
51+
return await initializationPromise;
52+
}
4753

48-
try {
49-
console.log('🚀 Initializing Artillery shared context...');
50-
51-
// Use the same init function as e2e tests
52-
sharedContext = await init();
53-
54-
// Create auth contexts using helper functions
55-
alicePkpAuthContext = await createPkpAuthContext(sharedContext);
56-
aliceCustomAuthContext = await createCustomAuthContext(sharedContext);
54+
initializationPromise = (async () => {
55+
try {
56+
console.log('🚀 Initializing Artillery shared context...');
57+
58+
// Use the same init function as e2e tests
59+
sharedContext = await init();
60+
61+
// Create auth contexts using helper functions
62+
alicePkpAuthContext = await createPkpAuthContext(sharedContext);
63+
aliceCustomAuthContext = await createCustomAuthContext(sharedContext);
64+
65+
console.log('✅ Artillery shared context initialized');
66+
return sharedContext;
67+
} catch (error) {
68+
console.error('❌ Failed to initialize Artillery context:', error);
69+
// Reset state on failure so retry is possible
70+
initializationPromise = null;
71+
sharedContext = null;
72+
throw error;
73+
}
74+
})();
5775

58-
console.log('✅ Artillery shared context initialized');
59-
return sharedContext;
60-
} catch (error) {
61-
console.error('❌ Failed to initialize Artillery context:', error);
62-
throw error;
63-
}
76+
return await initializationPromise;
6477
}
6578

6679
/**
@@ -166,7 +179,7 @@ export async function runMultiEndpointTest(context: any, events: any) {
166179
/**
167180
* PKP Sign test functions
168181
*/
169-
export async function runPkpSignTest(context, events) {
182+
export async function runPkpSignTest(context: any, events: any) {
170183
await initializeSharedContext();
171184

172185
const parallelism = context.vars.parallelism || 5;
@@ -176,7 +189,7 @@ export async function runPkpSignTest(context, events) {
176189
await runTestWithMetrics('pkp_sign', testFn, context, events, parallelism);
177190
}
178191

179-
export async function runPkpSignTestWithEoa(context, events) {
192+
export async function runPkpSignTestWithEoa(context: any, events: any) {
180193
await initializeSharedContext();
181194

182195
const parallelism = context.vars.parallelism || 5;
@@ -192,7 +205,7 @@ export async function runPkpSignTestWithEoa(context, events) {
192205
);
193206
}
194207

195-
export async function runPkpSignTestWithPkp(context, events) {
208+
export async function runPkpSignTestWithPkp(context: any, events: any) {
196209
await initializeSharedContext();
197210

198211
const parallelism = context.vars.parallelism || 5;
@@ -208,7 +221,7 @@ export async function runPkpSignTestWithPkp(context, events) {
208221
);
209222
}
210223

211-
export async function runPkpSignTestWithCustom(context, events) {
224+
export async function runPkpSignTestWithCustom(context: any, events: any) {
212225
await initializeSharedContext();
213226

214227
const parallelism = context.vars.parallelism || 5;
@@ -227,7 +240,7 @@ export async function runPkpSignTestWithCustom(context, events) {
227240
/**
228241
* Encrypt/Decrypt test functions
229242
*/
230-
export async function runEncryptDecryptTest(context, events) {
243+
export async function runEncryptDecryptTest(context: any, events: any) {
231244
await initializeSharedContext();
232245

233246
const parallelism = context.vars.parallelism || 3;
@@ -243,7 +256,7 @@ export async function runEncryptDecryptTest(context, events) {
243256
);
244257
}
245258

246-
export async function runPkpEncryptDecryptTest(context, events) {
259+
export async function runPkpEncryptDecryptTest(context: any, events: any) {
247260
await initializeSharedContext();
248261

249262
const parallelism = context.vars.parallelism || 3;
@@ -259,7 +272,7 @@ export async function runPkpEncryptDecryptTest(context, events) {
259272
);
260273
}
261274

262-
export async function runEncryptDecryptFlowTest(context, events) {
275+
export async function runEncryptDecryptFlowTest(context: any, events: any) {
263276
await initializeSharedContext();
264277

265278
const parallelism = context.vars.parallelism || 3;
@@ -278,7 +291,7 @@ export async function runEncryptDecryptFlowTest(context, events) {
278291
/**
279292
* Execute JS test function
280293
*/
281-
export async function runExecuteJsTest(context, events) {
294+
export async function runExecuteJsTest(context: any, events: any) {
282295
await initializeSharedContext();
283296

284297
const parallelism = context.vars.parallelism || 4;
@@ -291,7 +304,7 @@ export async function runExecuteJsTest(context, events) {
291304
/**
292305
* View PKPs test functions
293306
*/
294-
export async function runViewPkpsTest(context, events) {
307+
export async function runViewPkpsTest(context: any, events: any) {
295308
await initializeSharedContext();
296309

297310
const parallelism = context.vars.parallelism || 5;
@@ -325,7 +338,7 @@ export async function runViewPkpsTest(context, events) {
325338
/**
326339
* Viem integration test functions
327340
*/
328-
export async function runViemSignTest(context, events) {
341+
export async function runViemSignTest(context: any, events: any) {
329342
await initializeSharedContext();
330343

331344
const parallelism = context.vars.parallelism || 3;

e2e/src/init.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const init = async (
5050
const aliceViemAccountAuthData = await ViemAccountAuthenticator.authenticate(
5151
aliceViemAccount
5252
);
53-
53+
5454
const bobViemAccount = privateKeyToAccount(generatePrivateKey());
5555
const bobViemAccountAuthData = await ViemAccountAuthenticator.authenticate(
5656
bobViemAccount
@@ -167,7 +167,7 @@ export const init = async (
167167
},
168168
storageProvider: storagePlugins.localStorageNode({
169169
appName: 'my-app',
170-
networkName: 'naga-dev',
170+
networkName: _network,
171171
storagePath: './pkp-tokens',
172172
}),
173173
});
@@ -180,7 +180,7 @@ export const init = async (
180180
},
181181
storageProvider: storagePlugins.localStorageNode({
182182
appName: 'my-app',
183-
networkName: 'naga-dev',
183+
networkName: _network,
184184
storagePath: './pkp-tokens-bob',
185185
}),
186186
});
@@ -219,7 +219,7 @@ export const init = async (
219219
},
220220
storageProvider: storagePlugins.localStorageNode({
221221
appName: 'my-app',
222-
networkName: 'naga-dev',
222+
networkName: _network,
223223
storagePath: './pkp-tokens',
224224
}),
225225
});
@@ -232,7 +232,7 @@ export const init = async (
232232
},
233233
storageProvider: storagePlugins.localStorageNode({
234234
appName: 'my-app',
235-
networkName: 'naga-dev',
235+
networkName: _network,
236236
storagePath: './pkp-tokens-bob',
237237
}),
238238
});

0 commit comments

Comments
 (0)