Skip to content

Commit

Permalink
fix: prevent concurrent api invocations map writes (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
le-yams committed Aug 24, 2023
1 parent 4770a57 commit ec902ed
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http/httptest"
"net/url"
"strings"
"sync"
)

// TestingT is an interface wrapper around *testing.T.
Expand All @@ -20,6 +21,7 @@ type APIMock struct {
calls map[HTTPCall]http.HandlerFunc
testState TestingT
invocations map[HTTPCall][]*Invocation
mu sync.Mutex
}

type HTTPCall struct {
Expand All @@ -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 {
Expand Down

0 comments on commit ec902ed

Please sign in to comment.