From d83bf1d906fbe2ee054f1830491dcada14ca51e4 Mon Sep 17 00:00:00 2001 From: Viktor Stanchev Date: Tue, 5 Sep 2023 01:10:28 +0000 Subject: [PATCH 1/2] fix flaky test TestSubscriptions --- codegen/testserver/followschema/subscription_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/codegen/testserver/followschema/subscription_test.go b/codegen/testserver/followschema/subscription_test.go index 6126db99ce8..c4490451903 100644 --- a/codegen/testserver/followschema/subscription_test.go +++ b/codegen/testserver/followschema/subscription_test.go @@ -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, @@ -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) { From cb8ede82bc996e1e2252cec1aedf0bdfcab64495 Mon Sep 17 00:00:00 2001 From: Viktor Stanchev Date: Tue, 5 Sep 2023 01:14:13 +0000 Subject: [PATCH 2/2] update other copy of the test --- codegen/testserver/singlefile/subscription_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/codegen/testserver/singlefile/subscription_test.go b/codegen/testserver/singlefile/subscription_test.go index 24186e5ebee..567b5acd03c 100644 --- a/codegen/testserver/singlefile/subscription_test.go +++ b/codegen/testserver/singlefile/subscription_test.go @@ -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, @@ -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) {