Skip to content

Commit

Permalink
Fix vet errors copying mutex (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
steinfletcher authored Aug 30, 2024
1 parent 452fa6e commit 7d8ed83
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ func (m *Mock) copy() *Mock {
req := *m.request
newMock.request = &req

res := *m.response
newMock.response = &res
newMock.response = m.response.deepCopy()

return &newMock
}
Expand Down Expand Up @@ -296,6 +295,31 @@ type MockResponse struct {
mu sync.RWMutex // Add a mutex for thread-safe access
}

func (r *MockResponse) deepCopy() *MockResponse {
newResponse := &MockResponse{
timeout: r.timeout,
headers: make(map[string][]string),
cookies: make([]*Cookie, len(r.cookies)),
body: r.body,
statusCode: r.statusCode,
fixedDelayMillis: r.fixedDelayMillis,
mu: sync.RWMutex{},
}

for k, v := range r.headers {
newHeader := make([]string, len(v))
copy(newHeader, v)
newResponse.headers[k] = newHeader
}

for i, cookie := range r.cookies {
newCookie := *cookie
newResponse.cookies[i] = &newCookie
}

return newResponse
}

// StandaloneMocks for using mocks outside of API tests context
type StandaloneMocks struct {
mocks []*Mock
Expand Down

0 comments on commit 7d8ed83

Please sign in to comment.