github.com/jshiv/can-go@v0.2.1-0.20210224011015-069e90e90bdf/pkg/canrunner/run_test.go (about) 1 package canrunner_test 2 3 import ( 4 "context" 5 "errors" 6 "os" 7 "testing" 8 "time" 9 10 "github.com/golang/mock/gomock" 11 "go.einride.tech/can" 12 "go.einride.tech/can/internal/gen/mock/mockcanrunner" 13 "go.einride.tech/can/internal/gen/mock/mockclock" 14 "go.einride.tech/can/pkg/canrunner" 15 "go.einride.tech/can/pkg/descriptor" 16 "golang.org/x/sync/errgroup" 17 "gotest.tools/v3/assert" 18 ) 19 20 func TestRunMessageReceiver_NoMessages(t *testing.T) { 21 ctrl := gomock.NewController(t) 22 defer ctrl.Finish() 23 rx := mockcanrunner.NewMockFrameReceiver(ctrl) 24 node := mockcanrunner.NewMockNode(ctrl) 25 clock := mockclock.NewMockClock(ctrl) 26 ctx := context.Background() 27 // when the first receive fails 28 rx.EXPECT().Receive().Return(false) 29 rx.EXPECT().Err().Return(os.ErrClosed) 30 // then an error is returned 31 assert.Assert(t, errors.Is(canrunner.RunMessageReceiver(ctx, rx, node, clock), os.ErrClosed)) 32 } 33 34 func TestRunMessageReceiver_ReceiveMessage(t *testing.T) { 35 ctrl := gomock.NewController(t) 36 defer ctrl.Finish() 37 rx := mockcanrunner.NewMockFrameReceiver(ctrl) 38 node := mockcanrunner.NewMockNode(ctrl) 39 clock := mockclock.NewMockClock(ctrl) 40 msg := mockcanrunner.NewMockReceivedMessage(ctrl) 41 ctx := context.Background() 42 // when the first receive succeeds 43 frame := can.Frame{ID: 42} 44 rx.EXPECT().Receive().Return(true) 45 rx.EXPECT().Frame().Return(frame) 46 // then the receiver should do a message lookup 47 node.EXPECT().ReceivedMessage(frame.ID).Return(msg, true) 48 // and the node should be locked 49 node.EXPECT().Lock() 50 // and the message should be queried for a hook with the same context 51 afterReceiveHook := func(c context.Context) error { 52 assert.DeepEqual(t, ctx, c) 53 return nil 54 } 55 msg.EXPECT().AfterReceiveHook().Return(afterReceiveHook) 56 // and the receive time should be set 57 now := time.Unix(0, 1) 58 clock.EXPECT().Now().Return(now) 59 msg.EXPECT().SetReceiveTime(now) 60 // and the message should be called to unmarshal the frame 61 msg.EXPECT().UnmarshalFrame(frame) 62 // and the node should be unlocked 63 node.EXPECT().Unlock() 64 // when the next receive fails 65 rx.EXPECT().Receive().Return(false) 66 rx.EXPECT().Err().Return(nil) 67 // then the receiver should return 68 assert.NilError(t, canrunner.RunMessageReceiver(ctx, rx, node, clock)) 69 } 70 71 func TestRunMessageTransmitter_TransmitEventMessage(t *testing.T) { 72 t.Skip() // TODO: fix deadlock flakynes. 73 ctrl := gomock.NewController(t) 74 defer ctrl.Finish() 75 tx := mockcanrunner.NewMockFrameTransmitter(ctrl) 76 node := mockcanrunner.NewMockNode(ctrl) 77 msg := mockcanrunner.NewMockTransmittedMessage(ctrl) 78 clock := mockclock.NewMockClock(ctrl) 79 desc := &descriptor.Message{ 80 Name: "TestMessage", 81 SendType: descriptor.SendTypeEvent, 82 } 83 transmitEventChan := make(chan struct{}) 84 wakeUpChan := make(chan struct{}) 85 ctx := context.Background() 86 msg.EXPECT().Descriptor().AnyTimes().Return(desc) 87 msg.EXPECT().TransmitEventChan().Return(transmitEventChan) 88 msg.EXPECT().WakeUpChan().Return(wakeUpChan) 89 // given a running transmitter 90 ctx, cancel := context.WithCancel(context.Background()) 91 var g errgroup.Group 92 g.Go(func() error { 93 return canrunner.RunMessageTransmitter(ctx, tx, node, msg, clock) 94 }) 95 // then message should be queried for if it has cyclic transmission enabled 96 node.EXPECT().Lock() 97 msg.EXPECT().IsCyclicTransmissionEnabled() 98 node.EXPECT().Unlock() 99 // then the node should be locked 100 node.EXPECT().Lock() 101 // and the time should be queried 102 now := time.Unix(0, 1) 103 clock.EXPECT().Now().Return(now) 104 // and the transmit hook should be queried with the same context 105 hook := func(c context.Context) error { 106 assert.Equal(t, ctx, c) 107 return nil 108 } 109 msg.EXPECT().BeforeTransmitHook().Return(hook) 110 // and the message should be marshaled to a CAN frame 111 frame := can.Frame{ID: 42} 112 // and the transmit time should be set 113 msg.EXPECT().SetTransmitTime(now) 114 // and the node should be unlocked 115 node.EXPECT().Unlock() 116 node.EXPECT().Lock() 117 msg.EXPECT().Frame().Return(frame) 118 node.EXPECT().Unlock() 119 // and the CAN frame should be transmitted 120 tx.EXPECT().TransmitFrame(gomock.Any(), frame) 121 // when the transmitter receives a transmit event 122 transmitEventChan <- struct{}{} 123 cancel() 124 assert.NilError(t, g.Wait()) 125 }