From ec902edcadf38a9af7444517fada27d774ff8455 Mon Sep 17 00:00:00 2001 From: Yann Date: Thu, 24 Aug 2023 14:25:09 +0200 Subject: [PATCH] fix: prevent concurrent api invocations map writes (#5) --- api.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/api.go b/api.go index 439736e..c1ce364 100644 --- a/api.go +++ b/api.go @@ -5,6 +5,7 @@ import ( "net/http/httptest" "net/url" "strings" + "sync" ) // TestingT is an interface wrapper around *testing.T. @@ -20,6 +21,7 @@ type APIMock struct { calls map[HTTPCall]http.HandlerFunc testState TestingT invocations map[HTTPCall][]*Invocation + mu sync.Mutex } type HTTPCall struct { @@ -40,9 +42,11 @@ func API(testState TestingT) *APIMock { Path: request.RequestURI, } + mockedAPI.mu.Lock() invocations := mockedAPI.invocations[call] invocations = append(invocations, newInvocation(request, testState)) mockedAPI.invocations[call] = invocations + mockedAPI.mu.Unlock() handler := mockedAPI.calls[call] if handler != nil {