diff --git a/Makefile b/Makefile index 44020c0..26bc7ae 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ devrun=docker run $(devrunopts) --rm \ ## run isolated tests .PHONY: test test: - go test ./... -timeout 10s -race -shuffle on + go test -run="$(testcase)" ./... -timeout 10s -race -shuffle on ## Run lint .PHONY: lint diff --git a/metrica_test.go b/metrica_test.go index bef1ac0..d6ebad5 100644 --- a/metrica_test.go +++ b/metrica_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "net/http" "net/http/httptest" + "sync" "testing" ) @@ -47,6 +48,28 @@ func TestHandlerCount_Multiple(t *testing.T) { } } +func TestHandlerCount_Concurrent(t *testing.T) { + c := NewAtomicCounter() + mux := Handler(c) + var wg sync.WaitGroup + + for i := 0; i < 1000; i++ { + wg.Add(1) + go func() { + req := httptest.NewRequest("GET", "/count", nil) + w := httptest.NewRecorder() + mux.ServeHTTP(w, req) + if w.Code != http.StatusOK { + t.Errorf("expected status OK; got %v", w.Code) + } + defer wg.Done() + }() + } + wg.Wait() + + assert(t, int64(1000), c.Value()) +} + func assert(t *testing.T, want interface{}, got interface{}) { if want != got { t.Errorf("expected %v; got %v", want, got)