Skip to content

Commit

Permalink
fix race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Jun 28, 2024
1 parent 16f3bea commit 6a807b8
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions example/subscription/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"net/http"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -370,8 +371,7 @@ func testSubscription_LifeCycleEvents(t *testing.T, syncMode bool) {

var lock sync.Mutex
subscriptionResults := []gql.Subscription{}
wasConnected := false
wasDisconnected := false
var wasConnected, wasDisconnected int32
addResult := func(s gql.Subscription) int {
lock.Lock()
defer lock.Unlock()
Expand Down Expand Up @@ -436,20 +436,16 @@ func testSubscription_LifeCycleEvents(t *testing.T, syncMode bool) {
WithTimeout(3 * time.Second).
WithSyncMode(syncMode).
OnConnected(func() {
lock.Lock()
defer lock.Unlock()
log.Println("connected")
wasConnected = true
atomic.StoreInt32(&wasConnected, 1)
}).
OnError(func(sc *gql.SubscriptionClient, err error) error {
t.Fatalf("got error: %v, want: nil", err)
return err
}).
OnDisconnected(func() {
lock.Lock()
defer lock.Unlock()
log.Println("disconnected")
wasDisconnected = true
atomic.StoreInt32(&wasDisconnected, 1)
}).
OnSubscriptionComplete(func(s gql.Subscription) {
log.Println("OnSubscriptionComplete: ", s)
Expand Down Expand Up @@ -542,10 +538,10 @@ func testSubscription_LifeCycleEvents(t *testing.T, syncMode bool) {
}
}

if !wasConnected {
if atomic.LoadInt32(&wasConnected) != 1 {
t.Fatalf("expected OnConnected event, got none")
}
if !wasDisconnected {
if atomic.LoadInt32(&wasDisconnected) != 1 {
t.Fatalf("expected OnDisconnected event, got none")
}
}
Expand Down

0 comments on commit 6a807b8

Please sign in to comment.