Skip to content

Commit 45508c2

Browse files
authored
test: fix flaky TestErrCtx and TestContextError (#2670)
1 parent bea31ec commit 45508c2

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

internal/middleware/usagemetrics/usagemetrics_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
1010
"github.com/stretchr/testify/suite"
1111
"google.golang.org/grpc"
12+
"google.golang.org/grpc/codes"
1213
"google.golang.org/grpc/metadata"
14+
"google.golang.org/grpc/status"
1315

1416
"github.com/authzed/authzed-go/pkg/responsemeta"
1517

@@ -111,7 +113,9 @@ func (s *metricsMiddlewareTestSuite) TestErrCtx() {
111113

112114
// SimpleCtx times out after two seconds
113115
_, err := s.Client.PingError(s.SimpleCtx(), &testpb.PingErrorRequest{}, grpc.Trailer(&trailerMD))
114-
s.Require().ErrorContains(err, context.DeadlineExceeded.Error())
116+
117+
// the error may come from the grpc framework (client-side timeout) or from the API itself (it's a race)
118+
s.Require().Equal(codes.DeadlineExceeded, status.Code(err))
115119

116120
// TODO ideally, this test would assert that no error log has been written
117121
// but right now we have no way of capturing the logs

pkg/migrate/migrate_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package migrate
22

33
import (
44
"context"
5+
"errors"
56
"testing"
6-
"time"
77

88
"github.com/stretchr/testify/require"
99
)
@@ -46,7 +46,7 @@ type fakeTx struct{}
4646

4747
func TestContextError(t *testing.T) {
4848
req := require.New(t)
49-
ctx, cancelFunc := context.WithDeadline(t.Context(), time.Now().Add(1*time.Millisecond))
49+
ctx, cancelFunc := context.WithCancel(context.Background())
5050
m := NewManager[Driver[fakeConnPool, fakeTx], fakeConnPool, fakeTx]()
5151

5252
err := m.Register("1", "", func(ctx context.Context, conn fakeConnPool) error {
@@ -56,7 +56,7 @@ func TestContextError(t *testing.T) {
5656
req.NoError(err)
5757

5858
err = m.Register("2", "1", func(ctx context.Context, conn fakeConnPool) error {
59-
panic("the second migration should never be executed")
59+
return errors.New("the second migration should never be executed")
6060
}, noTxMigration)
6161
req.NoError(err)
6262

0 commit comments

Comments
 (0)