Skip to content

Commit

Permalink
✨ feat:concur test
Browse files Browse the repository at this point in the history
  • Loading branch information
perebaj committed Oct 10, 2023
1 parent b8ace8b commit 85f6e51
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions metrica_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"sync"
"testing"
)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 85f6e51

Please sign in to comment.