Skip to content

Commit

Permalink
Fix races in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Oct 31, 2023
1 parent f6979f7 commit 9bfb02f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
22 changes: 16 additions & 6 deletions cmd/pint/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"path"
"strconv"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -62,7 +63,7 @@ func TestScripts(t *testing.T) {
"cert": tlsCert,
},
Setup: func(env *testscript.Env) error {
env.Values["mocks"] = &httpMocks{responses: map[string][]httpMock{}}
env.Values["mocks"] = &httpMocks{resps: map[string][]httpMock{}}
return nil
},
})
Expand Down Expand Up @@ -177,7 +178,7 @@ func httpServer(ts *testscript.TestScript, _ bool, args []string) {
mux := http.NewServeMux()
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var done bool
for n, mockList := range mocks.responses {
for n, mockList := range mocks.responses() {
if n == name {
for _, mock := range mockList {
if mock.pattern != "/" && (r.URL.Path != mock.pattern || !strings.HasPrefix(r.URL.Path, mock.pattern)) {
Expand Down Expand Up @@ -231,14 +232,23 @@ type httpMock struct {
}

type httpMocks struct {
responses map[string][]httpMock
mtx sync.Mutex
resps map[string][]httpMock
}

func (m *httpMocks) add(name string, mock httpMock) {
if _, ok := m.responses[name]; !ok {
m.responses[name] = []httpMock{}
m.mtx.Lock()
defer m.mtx.Unlock()
if _, ok := m.resps[name]; !ok {
m.resps[name] = []httpMock{}
}
m.responses[name] = append(m.responses[name], mock)
m.resps[name] = append(m.resps[name], mock)
}

func (m *httpMocks) responses() map[string][]httpMock {
m.mtx.Lock()
defer m.mtx.Unlock()
return m.resps
}

func tlsCert(ts *testscript.TestScript, _ bool, args []string) {
Expand Down
4 changes: 0 additions & 4 deletions cmd/pint/tests/0157_series_other_servers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ http response prometheus1 /api/v1/query_range 200 {"status":"success","data":{"r
http response prometheus1 /api/v1/query 200 {"status":"success","data":{"resultType":"vector","result":[]}}
http start prometheus1 127.0.0.1:7157

http response prometheus2 /api/v1/metadata 200 {"status":"success","data":{}}
http response prometheus2 /api/v1/status/config 200 {"status":"success","data":{"yaml":"global:\n scrape_interval: 30s\n"}}
http response prometheus2 /api/v1/status/flags 200 {"status":"success","data":{"storage.tsdb.retention.time": "1d"}}
http response prometheus2 /api/v1/query_range 200 {"status":"success","data":{"resultType":"matrix","result":[]}}
http response prometheus2 /api/v1/query 200 {"status":"success","data":{"resultType":"vector","result":[{"metric":{},"value":[1698249632.491,"1"]}]}}
http start prometheus2 127.0.0.1:8157

Expand Down

0 comments on commit 9bfb02f

Please sign in to comment.