Skip to content

Commit

Permalink
fix(tests): deadlock on unsubscribe when epoll disabled (#1380)
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech authored Nov 16, 2024
1 parent 7156536 commit 337a60f
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 85 deletions.
4 changes: 2 additions & 2 deletions aws-lambda-router/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/akrylysov/algnhsa v1.1.0
github.com/aws/aws-lambda-go v1.43.0
github.com/stretchr/testify v1.9.0
github.com/wundergraph/cosmo/router v0.0.0-20241114130725-3a90563346a7
github.com/wundergraph/cosmo/router v0.0.0-20241115174502-7156536a93bf
go.uber.org/zap v1.27.0
)

Expand Down Expand Up @@ -94,7 +94,7 @@ require (
github.com/twmb/franz-go v1.16.1 // indirect
github.com/twmb/franz-go/pkg/kmsg v1.7.0 // indirect
github.com/wundergraph/astjson v0.0.0-20241108124845-44485579ffa5 // indirect
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.122 // indirect
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.123 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect
go.opentelemetry.io/contrib/propagators/b3 v1.23.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions aws-lambda-router/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,10 @@ github.com/vektah/gqlparser/v2 v2.5.16 h1:1gcmLTvs3JLKXckwCwlUagVn/IlV2bwqle0vJ0
github.com/vektah/gqlparser/v2 v2.5.16/go.mod h1:1lz1OeCqgQbQepsGxPVywrjdBHW2T08PUS3pJqepRww=
github.com/wundergraph/astjson v0.0.0-20241108124845-44485579ffa5 h1:rc+IQxG3rrAXEjBywirkzhKkyCKvXLGQXABVD8GiUtU=
github.com/wundergraph/astjson v0.0.0-20241108124845-44485579ffa5/go.mod h1:eOTL6acwctsN4F3b7YE+eE2t8zcJ/doLm9sZzsxxxrE=
github.com/wundergraph/cosmo/router v0.0.0-20241114130725-3a90563346a7 h1:fsyX5S2OnuTz1UE30pjhGRlvnYHgPE3Y6mCADJvDy3I=
github.com/wundergraph/cosmo/router v0.0.0-20241114130725-3a90563346a7/go.mod h1:KTNpl8uEK1MF9a0VeTMkpR9DcdiCVYPA8a0cpt6xNHs=
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.122 h1:nOjGdtP7LkVUq6TxY06yS28gelS4P4YtFtGPLDRYSYA=
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.122/go.mod h1:s4r/lhVEU5s0c6tCgpR0hK6FHEmX0cbrKcMU1pMc/ZI=
github.com/wundergraph/cosmo/router v0.0.0-20241115174502-7156536a93bf h1:HOiFhmZkl4RORU6E7JJi+MTEDt6KhP0qhSQouzgNMYQ=
github.com/wundergraph/cosmo/router v0.0.0-20241115174502-7156536a93bf/go.mod h1:MR8hksFCSekDg/5OPKb7HIXvboAc8xZX0DusUFOqpds=
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.123 h1:JI7axu4G8p52i866OZfXYTLrA2tAE3fjUk3XiBBe6Y8=
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.123/go.mod h1:s4r/lhVEU5s0c6tCgpR0hK6FHEmX0cbrKcMU1pMc/ZI=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 h1:aFJWCqJMNjENlcleuuOkGAPH82y0yULBScfXcIEdS24=
Expand Down
4 changes: 2 additions & 2 deletions demo/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ require (
github.com/rs/cors v1.11.0
github.com/vektah/gqlparser/v2 v2.5.16
github.com/wundergraph/cosmo/composition-go v0.0.0-20240124120900-5effe48a4a1d
github.com/wundergraph/cosmo/router v0.0.0-20241114130725-3a90563346a7
github.com/wundergraph/cosmo/router v0.0.0-20241116210832-a51d06033995
github.com/wundergraph/cosmo/router-tests v0.0.0-20241024215101-0c757faf23de
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.122
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.124
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1
go.opentelemetry.io/otel v1.28.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.23.1
Expand Down
8 changes: 4 additions & 4 deletions demo/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,12 @@ github.com/wundergraph/astjson v0.0.0-20241108124845-44485579ffa5 h1:rc+IQxG3rrA
github.com/wundergraph/astjson v0.0.0-20241108124845-44485579ffa5/go.mod h1:eOTL6acwctsN4F3b7YE+eE2t8zcJ/doLm9sZzsxxxrE=
github.com/wundergraph/cosmo/composition-go v0.0.0-20240124120900-5effe48a4a1d h1:NEUrhuqOaTO1dpW8pz2tu6dKbQAqFvgiF/m4NXdzZm0=
github.com/wundergraph/cosmo/composition-go v0.0.0-20240124120900-5effe48a4a1d/go.mod h1:9I3gPMAlAY+m1/cFL20iN7XHTyuZd3VT5ijccdU/FsI=
github.com/wundergraph/cosmo/router v0.0.0-20241114130725-3a90563346a7 h1:fsyX5S2OnuTz1UE30pjhGRlvnYHgPE3Y6mCADJvDy3I=
github.com/wundergraph/cosmo/router v0.0.0-20241114130725-3a90563346a7/go.mod h1:KTNpl8uEK1MF9a0VeTMkpR9DcdiCVYPA8a0cpt6xNHs=
github.com/wundergraph/cosmo/router v0.0.0-20241116210832-a51d06033995 h1:4NhEd0FxTxDXl0tRPC6HUpcZ53XGIjhPvvxrAN/3h30=
github.com/wundergraph/cosmo/router v0.0.0-20241116210832-a51d06033995/go.mod h1:KFCMzim0Xk1Ef5JFkuokU+SQ7NvDqytww+glK9mJkqI=
github.com/wundergraph/cosmo/router-tests v0.0.0-20241024215101-0c757faf23de h1:kEiNUGYSxHaT1I5GKre0GqHFCHVabpC/N33Chfue/rs=
github.com/wundergraph/cosmo/router-tests v0.0.0-20241024215101-0c757faf23de/go.mod h1:7WvZF+cOkfV4GaXka1qZfCqf/9GmOcksbPoMmJXtja4=
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.122 h1:nOjGdtP7LkVUq6TxY06yS28gelS4P4YtFtGPLDRYSYA=
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.122/go.mod h1:s4r/lhVEU5s0c6tCgpR0hK6FHEmX0cbrKcMU1pMc/ZI=
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.124 h1:TRVm/k59m5g4iYsmAgOEPJ/uXrEb67uYalkhI50MZwU=
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.124/go.mod h1:s4r/lhVEU5s0c6tCgpR0hK6FHEmX0cbrKcMU1pMc/ZI=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down
10 changes: 7 additions & 3 deletions router-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ require (
github.com/tidwall/gjson v1.18.0
github.com/twmb/franz-go v1.16.1
github.com/twmb/franz-go/pkg/kadm v1.11.0
github.com/wundergraph/cosmo/demo v0.0.0-20241114130725-3a90563346a7
github.com/wundergraph/cosmo/router v0.0.0-20241114130725-3a90563346a7
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.123
github.com/wundergraph/cosmo/demo v0.0.0-20241116210832-a51d06033995
github.com/wundergraph/cosmo/router v0.0.0-20241116210832-a51d06033995
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.124
go.opentelemetry.io/otel v1.28.0
go.opentelemetry.io/otel/sdk v1.28.0
go.opentelemetry.io/otel/sdk/metric v1.28.0
Expand Down Expand Up @@ -170,7 +170,11 @@ require (
nhooyr.io/websocket v1.8.11 // indirect
)

// Remember you can use Go workspaces to avoid using replace directives in multiple go.mod files
// Use what is best for your personal workflow. See CONTRIBUTING.md for more information

replace (
github.com/wundergraph/cosmo/demo => ../demo
github.com/wundergraph/cosmo/router => ../router
// github.com/wundergraph/graphql-go-tools/v2 => ../../graphql-go-tools/v2
)
4 changes: 2 additions & 2 deletions router-tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ github.com/vektah/gqlparser/v2 v2.5.16 h1:1gcmLTvs3JLKXckwCwlUagVn/IlV2bwqle0vJ0
github.com/vektah/gqlparser/v2 v2.5.16/go.mod h1:1lz1OeCqgQbQepsGxPVywrjdBHW2T08PUS3pJqepRww=
github.com/wundergraph/astjson v0.0.0-20241108124845-44485579ffa5 h1:rc+IQxG3rrAXEjBywirkzhKkyCKvXLGQXABVD8GiUtU=
github.com/wundergraph/astjson v0.0.0-20241108124845-44485579ffa5/go.mod h1:eOTL6acwctsN4F3b7YE+eE2t8zcJ/doLm9sZzsxxxrE=
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.123 h1:JI7axu4G8p52i866OZfXYTLrA2tAE3fjUk3XiBBe6Y8=
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.123/go.mod h1:s4r/lhVEU5s0c6tCgpR0hK6FHEmX0cbrKcMU1pMc/ZI=
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.124 h1:TRVm/k59m5g4iYsmAgOEPJ/uXrEb67uYalkhI50MZwU=
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.124/go.mod h1:s4r/lhVEU5s0c6tCgpR0hK6FHEmX0cbrKcMU1pMc/ZI=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
153 changes: 88 additions & 65 deletions router-tests/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,71 +492,25 @@ func TestWebSockets(t *testing.T) {
engineExecutionConfiguration.WebSocketReadTimeout = time.Millisecond * 10
},
}, func(t *testing.T, xEnv *testenv.Environment) {

type currentTimePayload struct {
Data struct {
CurrentTime struct {
UnixTime float64 `json:"unixTime"`
Timestamp string `json:"timestamp"`
} `json:"currentTime"`
} `json:"data"`
}

conn := xEnv.InitGraphQLWebSocketConnection(nil, nil, nil)
err := conn.WriteJSON(&testenv.WebSocketMessage{
ID: "1",
Type: "subscribe",
Payload: []byte(`{"query":"subscription { currentTime { unixTime timeStamp }}"}`),
})
require.NoError(t, err)
var msg testenv.WebSocketMessage
var payload currentTimePayload

// Read a result and store its timestamp, next result should be 1 second later
err = conn.ReadJSON(&msg)
require.NoError(t, err)
require.Equal(t, "1", msg.ID)
require.Equal(t, "next", msg.Type)
err = json.Unmarshal(msg.Payload, &payload)
require.NoError(t, err)

unix1 := payload.Data.CurrentTime.UnixTime

err = conn.ReadJSON(&msg)
require.NoError(t, err)
require.Equal(t, "1", msg.ID)
require.Equal(t, "next", msg.Type)
err = json.Unmarshal(msg.Payload, &payload)
require.NoError(t, err)

unix2 := payload.Data.CurrentTime.UnixTime
require.Greater(t, unix2, unix1)

// Sending a complete must stop the subscription
err = conn.WriteJSON(&testenv.WebSocketMessage{
ID: "1",
Type: "complete",
})
require.NoError(t, err)

var complete testenv.WebSocketMessage
err = conn.SetReadDeadline(time.Now().Add(1 * time.Second))
require.NoError(t, err)
err = conn.ReadJSON(&complete)
require.NoError(t, err)
require.Equal(t, "1", complete.ID)
require.Equal(t, "complete", complete.Type)

err = conn.SetReadDeadline(time.Now().Add(1 * time.Second))
require.NoError(t, err)
_, _, err = conn.ReadMessage()
require.Error(t, err)
var netErr net.Error
if errors.As(err, &netErr) {
require.True(t, netErr.Timeout())
} else {
require.Fail(t, "expected net.Error")
}
expectConnectAndReadCurrentTime(t, xEnv)
})
})
t.Run("subscription with multiple reconnects and epoll", func(t *testing.T) {
t.Parallel()
testenv.Run(t, &testenv.Config{}, func(t *testing.T, xEnv *testenv.Environment) {
expectConnectAndReadCurrentTime(t, xEnv)
expectConnectAndReadCurrentTime(t, xEnv)
})
})
t.Run("subscription with multiple reconnects and epoll disabled", func(t *testing.T) {
t.Parallel()
testenv.Run(t, &testenv.Config{
ModifyEngineExecutionConfiguration: func(engineExecutionConfiguration *config.EngineExecutionConfiguration) {
engineExecutionConfiguration.EnableWebSocketEpollKqueue = false
},
}, func(t *testing.T, xEnv *testenv.Environment) {
expectConnectAndReadCurrentTime(t, xEnv)
expectConnectAndReadCurrentTime(t, xEnv)
})
})
t.Run("subscription with header propagation", func(t *testing.T) {
Expand Down Expand Up @@ -2009,3 +1963,72 @@ func TestWebSockets(t *testing.T) {
})
})
}

func expectConnectAndReadCurrentTime(t *testing.T, xEnv *testenv.Environment) {
type currentTimePayload struct {
Data struct {
CurrentTime struct {
UnixTime float64 `json:"unixTime"`
Timestamp string `json:"timestamp"`
} `json:"currentTime"`
} `json:"data"`
}

conn := xEnv.InitGraphQLWebSocketConnection(nil, nil, nil)
defer conn.Close()

err := conn.WriteJSON(&testenv.WebSocketMessage{
ID: "1",
Type: "subscribe",
Payload: []byte(`{"query":"subscription { currentTime { unixTime timeStamp }}"}`),
})
require.NoError(t, err)
var msg testenv.WebSocketMessage
var payload currentTimePayload

// Read a result and store its timestamp, next result should be 1 second later
err = conn.ReadJSON(&msg)
require.NoError(t, err)
require.Equal(t, "1", msg.ID)
require.Equal(t, "next", msg.Type)
err = json.Unmarshal(msg.Payload, &payload)
require.NoError(t, err)

unix1 := payload.Data.CurrentTime.UnixTime

err = conn.ReadJSON(&msg)
require.NoError(t, err)
require.Equal(t, "1", msg.ID)
require.Equal(t, "next", msg.Type)
err = json.Unmarshal(msg.Payload, &payload)
require.NoError(t, err)

unix2 := payload.Data.CurrentTime.UnixTime
require.Greater(t, unix2, unix1)

// Sending a complete must stop the subscription
err = conn.WriteJSON(&testenv.WebSocketMessage{
ID: "1",
Type: "complete",
})
require.NoError(t, err)

var complete testenv.WebSocketMessage
err = conn.SetReadDeadline(time.Now().Add(1 * time.Second))
require.NoError(t, err)
err = conn.ReadJSON(&complete)
require.NoError(t, err)
require.Equal(t, "1", complete.ID)
require.Equal(t, "complete", complete.Type)

err = conn.SetReadDeadline(time.Now().Add(1 * time.Second))
require.NoError(t, err)
_, _, err = conn.ReadMessage()
require.Error(t, err)
var netErr net.Error
if errors.As(err, &netErr) {
require.True(t, netErr.Timeout())
} else {
require.Fail(t, "expected net.Error")
}
}
5 changes: 4 additions & 1 deletion router/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ require (
github.com/tidwall/gjson v1.18.0
github.com/tidwall/sjson v1.2.5
github.com/twmb/franz-go v1.16.1
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.123
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.124
// Do not upgrade, it renames attributes we rely on
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1
go.opentelemetry.io/contrib/propagators/b3 v1.23.0
Expand Down Expand Up @@ -143,4 +143,7 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

// Remember you can use Go workspaces to avoid using replace directives in multiple go.mod files
// Use what is best for your personal workflow. See CONTRIBUTING.md for more information

//replace github.com/wundergraph/graphql-go-tools/v2 => ../../graphql-go-tools/v2
4 changes: 2 additions & 2 deletions router/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ github.com/vektah/gqlparser/v2 v2.5.16 h1:1gcmLTvs3JLKXckwCwlUagVn/IlV2bwqle0vJ0
github.com/vektah/gqlparser/v2 v2.5.16/go.mod h1:1lz1OeCqgQbQepsGxPVywrjdBHW2T08PUS3pJqepRww=
github.com/wundergraph/astjson v0.0.0-20241108124845-44485579ffa5 h1:rc+IQxG3rrAXEjBywirkzhKkyCKvXLGQXABVD8GiUtU=
github.com/wundergraph/astjson v0.0.0-20241108124845-44485579ffa5/go.mod h1:eOTL6acwctsN4F3b7YE+eE2t8zcJ/doLm9sZzsxxxrE=
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.123 h1:JI7axu4G8p52i866OZfXYTLrA2tAE3fjUk3XiBBe6Y8=
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.123/go.mod h1:s4r/lhVEU5s0c6tCgpR0hK6FHEmX0cbrKcMU1pMc/ZI=
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.124 h1:TRVm/k59m5g4iYsmAgOEPJ/uXrEb67uYalkhI50MZwU=
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.124/go.mod h1:s4r/lhVEU5s0c6tCgpR0hK6FHEmX0cbrKcMU1pMc/ZI=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 h1:aFJWCqJMNjENlcleuuOkGAPH82y0yULBScfXcIEdS24=
Expand Down

0 comments on commit 337a60f

Please sign in to comment.