Skip to content

Commit

Permalink
Merge pull request #29 from superlistapp/issues/28-fix-data-race
Browse files Browse the repository at this point in the history
Add mutex to FIFOResponseHandler
  • Loading branch information
migueleliasweb committed Jul 18, 2022
2 parents 8306ee3 + cefa7bc commit 122621d
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/mock/server.go
Expand Up @@ -7,6 +7,7 @@ import (
"net/http/httptest"
"strconv"
"strings"
"sync"

"github.com/gorilla/mux"
)
Expand All @@ -26,12 +27,15 @@ type MockBackendOption func(*mux.Router)
//
// Once all available `Responses` have been used, this handler will panic()!
type FIFOReponseHandler struct {
lock sync.Mutex
Responses [][]byte
CurrentIndex int
}

// ServeHTTP implementation of `http.Handler`
func (srh *FIFOReponseHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
srh.lock.Lock()
defer srh.lock.Unlock()
if srh.CurrentIndex > len(srh.Responses) {
panic(fmt.Sprintf(
"go-github-mock: no more mocks available for %s",
Expand Down

0 comments on commit 122621d

Please sign in to comment.