Skip to content

Commit

Permalink
test: go 1.21 compatibility
Browse files Browse the repository at this point in the history
Go 1.21 added an optimization that broke one of our tests. Comparing
context is not well-defined in Go so we compare a value inside the
context instead.

This should work regardless of Go version I hope...
  • Loading branch information
Edholm committed Sep 18, 2023
1 parent 6a15d77 commit abd3d3f
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pkg/canrunner/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@ func TestRunMessageReceiver_NoMessages(t *testing.T) {
assert.Assert(t, errors.Is(canrunner.RunMessageReceiver(ctx, rx, node, clock), os.ErrClosed))
}

type contextCompareKeyType string

const contextCompareKey contextCompareKeyType = "sentinel-key"

func TestRunMessageReceiver_ReceiveMessage(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
rx := mockcanrunner.NewMockFrameReceiver(ctrl)
node := mockcanrunner.NewMockNode(ctrl)
clock := mockclock.NewMockClock(ctrl)
msg := mockcanrunner.NewMockReceivedMessage(ctrl)
ctx := context.Background()
const sentinel = "sentinel-value"
ctx := context.WithValue(context.Background(), contextCompareKey, sentinel)
// when the first receive succeeds
frame := can.Frame{ID: 42}
rx.EXPECT().Receive().Return(true)
Expand All @@ -48,8 +53,10 @@ func TestRunMessageReceiver_ReceiveMessage(t *testing.T) {
// and the node should be locked
node.EXPECT().Lock()
// and the message should be queried for a hook with the same context
// Comparing context is not well-defined, so we check for a sentinel value instead.
afterReceiveHook := func(c context.Context) error {
assert.DeepEqual(t, ctx, c)
value := c.Value(contextCompareKey)
assert.DeepEqual(t, value, sentinel)
return nil
}
msg.EXPECT().AfterReceiveHook().Return(afterReceiveHook)
Expand Down

0 comments on commit abd3d3f

Please sign in to comment.