Skip to content

Commit e2b5656

Browse files
authored
fix flaky test TestRuntimeAPILoopWithConcurrency (#606)
1 parent 1fe9d1b commit e2b5656

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

lambda/invoke_loop_gte_go122_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ func TestRuntimeAPILoopWithConcurrency(t *testing.T) {
6565
endpoint := strings.Split(ts.URL, "://")[1]
6666
expectedError := fmt.Sprintf("failed to GET http://%s/2018-06-01/runtime/invocation/next: got unexpected status code: 410", endpoint)
6767
assert.EqualError(t, startRuntimeAPILoopWithConcurrency(endpoint, handler, concurrency), expectedError)
68+
record.lock.Lock()
6869
assert.GreaterOrEqual(t, record.nGets, nInvokes+1)
6970
assert.Equal(t, nInvokes, record.nPosts)
71+
record.lock.Unlock()
7072
assert.Equal(t, int32(concurrency), maxActive.Load())
7173
responses := make(map[string]int)
7274
for _, response := range record.responses {

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)