Skip to content

Commit

Permalink
Merge pull request #66 from chrisjharding/race
Browse files Browse the repository at this point in the history
Guard isUsed when called concurrently
  • Loading branch information
steinfletcher authored Nov 1, 2019
2 parents 48c3507 + d7c7694 commit 21ad4e6
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"regexp"
"sort"
"strings"
"sync"
)

// Transport wraps components used to observe and manipulate the real request and response objects
Expand Down Expand Up @@ -195,6 +196,7 @@ func buildResponseFromMock(mockResponse *MockResponse) *http.Response {

// Mock represents the entire interaction for a mock to be used for testing
type Mock struct {
m sync.Mutex
isUsed bool
request *MockRequest
response *MockResponse
Expand Down Expand Up @@ -370,6 +372,8 @@ func (m *Mock) Method(method string) *MockRequest {
func matches(req *http.Request, mocks []*Mock) (*MockResponse, error) {
mockError := newUnmatchedMockError()
for mockNumber, mock := range mocks {
mock.m.Lock() // lock is for isUsed when matches is called concurrently by RoundTripper
defer mock.m.Unlock()
if mock.isUsed {
continue
}
Expand Down

0 comments on commit 21ad4e6

Please sign in to comment.