Skip to content

Commit 3435df6

Browse files
committed
test: Increase request iterations in load balancer tests to reduce randomness flakiness in CI
1 parent d95cbce commit 3435df6

2 files changed

Lines changed: 24 additions & 17 deletions

File tree

test/e2e/hooks.test.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ describe('Hooks E2E Tests', () => {
192192
const afterCall = afterResponseCalls[afterResponseCalls.length - 1]
193193
expect(afterCall?.req.url).toContain('/api/hooks/hello')
194194
expect(afterCall?.res.status).toBe(200)
195-
})
195+
}, 20000)
196196

197197
test('should trigger circuit breaker hooks on successful request', async () => {
198198
const initialBeforeCount = beforeCircuitBreakerCalls.length
@@ -218,7 +218,7 @@ describe('Hooks E2E Tests', () => {
218218
expect(afterCall?.req.url).toContain('/api/error/health')
219219
expect(afterCall?.result.state).toBe('CLOSED')
220220
expect(afterCall?.result.success).toBe(true)
221-
})
221+
}, 20000)
222222

223223
test('should trigger onError hook on server error', async () => {
224224
const initialErrorCount = onErrorCalls.length
@@ -237,7 +237,7 @@ describe('Hooks E2E Tests', () => {
237237
const errorCall = onErrorCalls[onErrorCalls.length - 1]
238238
expect(errorCall?.req.url).toContain('/api/error/error')
239239
expect(errorCall?.error.message).toContain('Server error')
240-
})
240+
}, 20000)
241241

242242
test('should trigger onError hook on timeout', async () => {
243243
const initialErrorCount = onErrorCalls.length
@@ -256,7 +256,7 @@ describe('Hooks E2E Tests', () => {
256256
const errorCall = onErrorCalls[onErrorCalls.length - 1]
257257
expect(errorCall?.req.url).toContain('/api/error/timeout')
258258
expect(errorCall?.error.message).toContain('timeout')
259-
})
259+
}, 20000)
260260

261261
test('should trigger circuit breaker hooks with failure state', async () => {
262262
// Wait for circuit breaker to potentially reset
@@ -281,7 +281,7 @@ describe('Hooks E2E Tests', () => {
281281
afterCircuitBreakerCalls[afterCircuitBreakerCalls.length - 1]
282282
expect(afterCall?.req.url).toContain('/api/error/error')
283283
expect(afterCall?.result.success).toBe(false)
284-
})
284+
}, 20000)
285285

286286
test('should trigger all hooks in correct order for successful request', async () => {
287287
// Reset counters
@@ -323,7 +323,7 @@ describe('Hooks E2E Tests', () => {
323323

324324
// Response should be successful
325325
expect(afterResponse?.res.status).toBe(200)
326-
})
326+
}, 20000)
327327

328328
test('should trigger all hooks in correct order for failed request', async () => {
329329
// Wait for circuit breaker to potentially reset
@@ -369,7 +369,7 @@ describe('Hooks E2E Tests', () => {
369369
expect(onError?.error.message).toMatch(
370370
/Server error|Circuit breaker is OPEN/,
371371
)
372-
})
372+
}, 20000)
373373

374374
test('should pass correct proxy configuration to beforeRequest hook', async () => {
375375
const initialCount = beforeRequestCalls.length
@@ -385,7 +385,7 @@ describe('Hooks E2E Tests', () => {
385385
expect(beforeCall?.opts).toBeDefined()
386386
expect(beforeCall?.opts.pathRewrite).toBeDefined()
387387
expect(beforeCall?.opts.pathRewrite['^/api/hooks']).toBe('')
388-
})
388+
}, 20000)
389389

390390
test('should provide response body to afterResponse hook', async () => {
391391
const initialCount = afterResponseCalls.length
@@ -401,7 +401,7 @@ describe('Hooks E2E Tests', () => {
401401
expect(afterCall?.res.status).toBe(200)
402402
expect(afterCall?.res.headers.get('content-type')).toContain('text/plain')
403403
expect(afterCall?.body).toBeDefined()
404-
})
404+
}, 20000)
405405

406406
test('should use fallback response from onError hook when returned', async () => {
407407
// Create a new gateway with onError hook that returns a fallback response
@@ -444,6 +444,8 @@ describe('Hooks E2E Tests', () => {
444444

445445
fallbackGateway.addRoute(fallbackRouteConfig)
446446
const fallbackServer = await fallbackGateway.listen(fallbackGatewayPort)
447+
// Allow the server a brief moment to be fully ready in slower CI environments
448+
await new Promise((resolve) => setTimeout(resolve, 150))
447449

448450
try {
449451
const response = await fetch(
@@ -464,7 +466,7 @@ describe('Hooks E2E Tests', () => {
464466
} finally {
465467
fallbackServer.stop()
466468
}
467-
})
469+
}, 20000)
468470

469471
test('should use fallback response from onError hook on timeout', async () => {
470472
// Create a new gateway with onError hook that handles timeouts
@@ -516,6 +518,8 @@ describe('Hooks E2E Tests', () => {
516518

517519
timeoutGateway.addRoute(timeoutRouteConfig)
518520
const timeoutServer = await timeoutGateway.listen(timeoutGatewayPort)
521+
// Allow the server a brief moment to be fully ready in slower CI environments
522+
await new Promise((resolve) => setTimeout(resolve, 150))
519523

520524
try {
521525
const response = await fetch(
@@ -539,7 +543,7 @@ describe('Hooks E2E Tests', () => {
539543
} finally {
540544
timeoutServer.stop()
541545
}
542-
})
546+
}, 20000)
543547

544548
test('should fallback to default error handling when onError hook throws', async () => {
545549
// Create a new failing server for this test
@@ -601,6 +605,8 @@ describe('Hooks E2E Tests', () => {
601605

602606
selectiveGateway.addRoute(selectiveRouteConfig)
603607
const selectiveServer = await selectiveGateway.listen(selectiveGatewayPort)
608+
// Allow the server a brief moment to be fully ready in slower CI environments
609+
await new Promise((resolve) => setTimeout(resolve, 150))
604610

605611
try {
606612
// Test with timeout (should use fallback)
@@ -622,7 +628,7 @@ describe('Hooks E2E Tests', () => {
622628
selectiveServer.stop()
623629
selectiveFailingServer.stop()
624630
}
625-
})
631+
}, 20000)
626632

627633
test('should handle async fallback response generation', async () => {
628634
// Create a new failing server for this test
@@ -688,7 +694,8 @@ describe('Hooks E2E Tests', () => {
688694
asyncGateway.addRoute(asyncRouteConfig)
689695
const asyncServer = await asyncGateway.listen(asyncGatewayPort)
690696

691-
await new Promise((resolve) => setTimeout(resolve, 50))
697+
// Allow the server a brief moment to be fully ready in slower CI environments
698+
await new Promise((resolve) => setTimeout(resolve, 250))
692699

693700
try {
694701
const testRequestId = `test-${Date.now()}`
@@ -715,5 +722,5 @@ describe('Hooks E2E Tests', () => {
715722
asyncServer.stop()
716723
asyncFailingServer.stop()
717724
}
718-
})
725+
}, 20000)
719726
})

test/load-balancer/load-balancer.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,8 +1205,8 @@ describe('HttpLoadBalancer', () => {
12051205
const request = createMockRequest()
12061206
const selections: string[] = []
12071207

1208-
// Make requests to test distribution
1209-
for (let i = 0; i < 30; i++) {
1208+
// Make many requests to reduce randomness flakiness in CI
1209+
for (let i = 0; i < 300; i++) {
12101210
const target = loadBalancer.selectTarget(request)
12111211
if (target) {
12121212
selections.push(target.url)
@@ -1223,7 +1223,7 @@ describe('HttpLoadBalancer', () => {
12231223
// Should distribute according to weights (1:2 ratio)
12241224
expect(noWeightCount).toBeGreaterThan(0)
12251225
expect(weightedCount).toBeGreaterThan(noWeightCount)
1226-
})
1226+
}, 20000)
12271227

12281228
test('handles session cleanup interval management', () => {
12291229
const config: LoadBalancerConfig = {

0 commit comments

Comments
 (0)