Skip to content

Commit 8d3e975

Browse files
committed
fix flaky test TestRuntimeAPILoopWithConcurrency
1 parent 1fe9d1b commit 8d3e975

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

lambda/invoke_loop_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ func defaultInvokeMetadata() eventMetadata {
442442
}
443443

444444
func runtimeAPIServer(eventPayload string, failAfter int, overrides ...eventMetadata) (*httptest.Server, *requestRecord) {
445+
ctx, cancel := context.WithCancel(context.Background())
445446
numInvokesRequested := 0
446447
numInvokesRequestedLock := sync.Mutex{}
447448
record := &requestRecord{}
@@ -461,6 +462,7 @@ func runtimeAPIServer(eventPayload string, failAfter int, overrides ...eventMeta
461462
record.nGets++
462463
record.lock.Unlock()
463464
if shouldFail {
465+
<-ctx.Done() // wait for all handlers to finish.
464466
w.WriteHeader(http.StatusGone)
465467
_, _ = w.Write([]byte("END THE TEST!"))
466468
return
@@ -483,10 +485,15 @@ func runtimeAPIServer(eventPayload string, failAfter int, overrides ...eventMeta
483485
w.WriteHeader(http.StatusAccepted)
484486
record.lock.Lock()
485487
record.nPosts++
488+
done := record.nPosts >= failAfter
486489
record.responses = append(record.responses, response.Bytes())
487490
record.contentTypes = append(record.contentTypes, r.Header.Get("Content-Type"))
488491
record.xrayCauses = append(record.xrayCauses, r.Header.Get(headerXRayErrorCause))
489492
record.lock.Unlock()
493+
if done {
494+
// all handlers are done, cancel the context to let the GET handler exit.
495+
cancel()
496+
}
490497
default:
491498
w.WriteHeader(http.StatusBadRequest)
492499
}

0 commit comments

Comments
 (0)