Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: race condition in tests #38

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
go-version: '1.22'

- name: Unit Test
run: make unit-tests
run: make unit-tests-ci

- name: e2e Test
run: make ci-e2e
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ up: ## run all applications in stack
docker compose up -d

.PHONY: unit-tests
unit-tests: ## run unit tests without e2e-tests directory..
unit-tests: ## run unit tests without e2e-tests directory.
go test -race -count=1 `go list ./... | grep -v e2e-tests`

.PHONY: unit-tests-ci
unit-tests-ci: ## run unit tests without e2e-tests directory (multiple times to find race conditions).
go test -race -count=50 -failfast `go list ./... | grep -v e2e-tests`

.PHONY: ci-e2e
ci-e2e: up
go run ./e2e-tests/scripts/wait-ready/main.go -addr=':80;:8081;:8082'
Expand All @@ -40,4 +44,4 @@ tests-e2e: ## run end to end tests


vendor-licenses: ## report vendor licenses
go-licenses report ./cmd/api --template licenses.tpl > licenses.json 2> licenses-errors
go-licenses report ./cmd/api --template licenses.tpl > licenses.json 2> licenses-errors
8 changes: 6 additions & 2 deletions internal/websocket/common/hub_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,15 @@ func TestCreateRemoveConcurrently(t *testing.T) {
hubs := &sync.Map{}

wg := sync.WaitGroup{}
wg.Add(channelsNo * clientsPerChannel)
// First we create `channelsNo` goroutines. Each of them creates `clientsPerChannel` sub-goroutines.
// This gives us `channelsNo*clientsPerChannel` sub go-routines and `channelsNo` parent goroutines.
// Each of them will call `wg.Done() once and we can't progress until all of them are done.
wg.Add(channelsNo*clientsPerChannel + channelsNo)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caan you explain why this logic? Preferably in comment in code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explained in comment


for i := 0; i < channelsNo; i++ {
channelID := fmt.Sprintf("channel-%d", i)
go func() {
defer wg.Done()
for j := 0; j < clientsPerChannel; j++ {
c, h := hp.registerClient(channelID, &websocket.Conn{})
hubs.Store(h, struct{}{})
Expand All @@ -70,7 +75,6 @@ func TestCreateRemoveConcurrently(t *testing.T) {
hubs.Store(h, struct{}{})
}()
}

wg.Wait()

for c, hub := range hp.hubs {
Expand Down