Skip to content

Commit

Permalink
Guard isUsed when called concurrently
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjharding committed Nov 1, 2019
1 parent 48c3507 commit d7c7694
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 d7c7694

Please sign in to comment.