Skip to content

Commit

Permalink
feat: make router-tests less flaky (#1484)
Browse files Browse the repository at this point in the history
  • Loading branch information
alepane21 authored Feb 11, 2025
1 parent 666d68e commit c2c76de
Show file tree
Hide file tree
Showing 13 changed files with 950 additions and 754 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/router-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,19 @@ jobs:
redis-cli -u "redis://cosmo:test@127.0.0.1:$port" ping
echo "ACL user 'cosmo' created with full access on port $port"
done
- uses: nick-fields/retry@v3
- name: Run Integration tests
working-directory: ./router-tests
run: make test test_params="-run '^Test[^(Flaky)]' --timeout=5m --parallel 10"
- name: Run Flaky Integration tests
uses: nick-fields/retry@v3
with:
timeout_minutes: 30
max_attempts: 5
retry_wait_seconds: 5
retry_on: error
command: |
cd router-tests
make test test_params="--timeout=5m"
make test test_params="-run '^TestFlaky' --timeout=5m -p 1 --parallel 1"
image_scan:
if: github.event.pull_request.head.repo.full_name == github.repository
Expand Down
3 changes: 2 additions & 1 deletion router-tests/cache_warmup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,8 @@ func TestCacheWarmup(t *testing.T) {
})
}

func TestCacheWarmupMetrics(t *testing.T) {
// Is set as Flaky so that when running the tests it will be run separately and retried if it fails
func TestFlakyCacheWarmupMetrics(t *testing.T) {
t.Run("should emit planning times metrics during warmup", func(t *testing.T) {
t.Parallel()

Expand Down
7 changes: 7 additions & 0 deletions router-tests/complexity_limits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package integration
import (
"net/http"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/wundergraph/cosmo/router-tests/testenv"
Expand Down Expand Up @@ -152,6 +153,8 @@ func TestComplexityLimits(t *testing.T) {
require.Contains(t, testSpan.Attributes(), otel.WgQueryDepth.Int(3))
require.Contains(t, testSpan.Attributes(), otel.WgQueryDepthCacheHit.Bool(false))
exporter.Reset()
// wait to let cache get consistent
time.Sleep(100 * time.Millisecond)

failedRes2, _ := xEnv.MakeGraphQLRequest(testenv.GraphQLRequest{
Query: `{ employee(id:1) { id details { forename surname } } }`,
Expand All @@ -163,6 +166,8 @@ func TestComplexityLimits(t *testing.T) {
require.Contains(t, testSpan2.Attributes(), otel.WgQueryDepth.Int(3))
require.Contains(t, testSpan2.Attributes(), otel.WgQueryDepthCacheHit.Bool(true))
exporter.Reset()
// wait to let cache get consistent
time.Sleep(100 * time.Millisecond)

successRes := xEnv.MakeGraphQLRequestOK(testenv.GraphQLRequest{
Query: `query { employees { id } }`,
Expand All @@ -172,6 +177,8 @@ func TestComplexityLimits(t *testing.T) {
require.Contains(t, testSpan3.Attributes(), otel.WgQueryDepth.Int(2))
require.Contains(t, testSpan3.Attributes(), otel.WgQueryDepthCacheHit.Bool(false))
exporter.Reset()
// wait to let cache get consistent
time.Sleep(100 * time.Millisecond)

successRes2 := xEnv.MakeGraphQLRequestOK(testenv.GraphQLRequest{
Query: `query { employees { id } }`,
Expand Down
34 changes: 22 additions & 12 deletions router-tests/config_hot_reload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/wundergraph/cosmo/router/pkg/routerconfig"

"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/wundergraph/cosmo/router-tests/testenv"
"github.com/wundergraph/cosmo/router/core"
Expand Down Expand Up @@ -238,28 +239,35 @@ func TestConfigHotReload(t *testing.T) {
},
}, func(t *testing.T, xEnv *testenv.Environment) {

var done atomic.Bool

var startedReq atomic.Bool
go func() {
defer done.Store(true)

startedReq.Store(true)
res, err := xEnv.MakeGraphQLRequestWithContext(context.Background(), testenv.GraphQLRequest{
Query: `{ employees { id } }`,
})
require.NoError(t, err)
require.Equal(t, res.Response.StatusCode, 200)
require.Equal(t, `{"errors":[{"message":"Failed to fetch from Subgraph 'employees'."}],"data":{"employees":null}}`, res.Body)
assert.Equal(t, res.Response.StatusCode, 200)
assert.Equal(t, `{"errors":[{"message":"Failed to fetch from Subgraph 'employees'."}],"data":{"employees":null}}`, res.Body)
}()

// Let's wait a bit to make sure all requests are in flight
// otherwise the shutdown will be too fast and the wait-group will not be done fully
require.Eventually(t, func() bool {
return startedReq.Load()
}, time.Second*10, time.Millisecond*100)
time.Sleep(time.Millisecond * 100)

xEnv.Shutdown()
var done atomic.Bool
go func() {
defer done.Store(true)

err := xEnv.Router.Shutdown(context.Background())
assert.ErrorContains(t, err, context.DeadlineExceeded.Error())
}()

require.Eventually(t, func() bool {
return done.Load()
}, time.Second*5, time.Millisecond*100)
}, time.Second*20, time.Millisecond*100)
})
})

Expand Down Expand Up @@ -314,13 +322,15 @@ func TestConfigHotReload(t *testing.T) {

// Swap config
require.NoError(t, pm.updateConfig(pm.initConfig, "old-1"))

err = conn.ReadJSON(&msg)

// Ensure that the connection is closed. In the future, we might want to send a complete message to the client
// If the operation happen fast enough, ensure that the connection is closed.
// In the future, we might want to send a complete message to the client
// and wait until in-flight messages are delivered before closing the connection
var wsErr *websocket.CloseError
require.ErrorAs(t, err, &wsErr)
if err != nil {
var wsErr *websocket.CloseError
require.ErrorAs(t, err, &wsErr)
}

require.NoError(t, conn.Close())

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration_test
package events_test

import (
"github.com/stretchr/testify/assert"
Expand Down
Loading

0 comments on commit c2c76de

Please sign in to comment.