@@ -38,29 +38,42 @@ import {
3838let sharedContext : any = null ;
3939let alicePkpAuthContext : any = null ;
4040let aliceCustomAuthContext : any = null ;
41+ let initializationPromise : Promise < any > | null = null ;
4142
4243/**
4344 * Initialize the shared context once per Artillery run
4445 */
4546async 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 ;
0 commit comments