Skip to content

Commit

Permalink
fix flaky test TestSubscriptions (#2779)
Browse files Browse the repository at this point in the history
* fix flaky test TestSubscriptions

* update other copy of the test
  • Loading branch information
vikstrous authored Sep 5, 2023
1 parent a41f4da commit bee47dc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions codegen/testserver/followschema/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ func TestSubscriptions(t *testing.T) {
})

t.Run("will parse init payload", func(t *testing.T) {
runtime.GC() // ensure no go-routines left from preceding tests
initialGoroutineCount := runtime.NumGoroutine()

sub := c.WebsocketWithPayload(`subscription { initPayload }`, map[string]interface{}{
"Authorization": "Bearer of the curse",
"number": 32,
Expand All @@ -155,6 +158,14 @@ func TestSubscriptions(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "strings = []interface {}{\"hello\", \"world\"}", msg.resp.InitPayload)
sub.Close()

// need a little bit of time for goroutines to settle
start := time.Now()
for time.Since(start).Seconds() < 2 && initialGoroutineCount != runtime.NumGoroutine() {
time.Sleep(5 * time.Millisecond)
}

require.Equal(t, initialGoroutineCount, runtime.NumGoroutine())
})

t.Run("websocket gets errors", func(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions codegen/testserver/singlefile/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ func TestSubscriptions(t *testing.T) {
})

t.Run("will parse init payload", func(t *testing.T) {
runtime.GC() // ensure no go-routines left from preceding tests
initialGoroutineCount := runtime.NumGoroutine()

sub := c.WebsocketWithPayload(`subscription { initPayload }`, map[string]interface{}{
"Authorization": "Bearer of the curse",
"number": 32,
Expand All @@ -155,6 +158,14 @@ func TestSubscriptions(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "strings = []interface {}{\"hello\", \"world\"}", msg.resp.InitPayload)
sub.Close()

// need a little bit of time for goroutines to settle
start := time.Now()
for time.Since(start).Seconds() < 2 && initialGoroutineCount != runtime.NumGoroutine() {
time.Sleep(5 * time.Millisecond)
}

require.Equal(t, initialGoroutineCount, runtime.NumGoroutine())
})

t.Run("websocket gets errors", func(t *testing.T) {
Expand Down

0 comments on commit bee47dc

Please sign in to comment.