Skip to content

Commit

Permalink
Fix the race condition in the end to end test.
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidtw committed Dec 30, 2023
1 parent 0b51e7f commit 7570113
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions internal/websocket/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -240,8 +241,12 @@ func TestEndToEndConnectionIssues(t *testing.T) {

var msgCnt, connectCnt, disconnectCnt atomic.Int64

var mutex sync.Mutex

got, err := ws.New(
ws.FetchURL(func(context.Context) (string, error) {
mutex.Lock()
defer mutex.Unlock()
if s.URL == "" {
return "", fmt.Errorf("no url")
}
Expand Down Expand Up @@ -280,16 +285,20 @@ func TestEndToEndConnectionIssues(t *testing.T) {
var started bool
for {
if connectCnt.Load() >= 3 && !started {
mutex.Lock()
s.Start()
defer s.Close()
mutex.Unlock()
defer func() {
mutex.Lock()
s.Close()
mutex.Unlock()
}()
started = true
}
if disconnectCnt.Load() > 0 {
break
}

fmt.Printf("connectCnt: %d, disconnectCnt: %d, msgCnt: %d\n", connectCnt.Load(), disconnectCnt.Load(), msgCnt.Load())

select {
case <-ctx.Done():
assert.Fail("timed out waiting for messages")
Expand Down

0 comments on commit 7570113

Please sign in to comment.