@@ -213,30 +213,14 @@ func TestChannel_ReceiveMessage_WhenEstablished(t *testing.T) {
213213 _ = server .Send (ctx , m )
214214
215215 // Act
216- actual , err := c .ReceiveMessage (ctx )
217-
218- // Assert
219- assert .NoError (t , err )
220- assert .Equal (t , m , actual )
221- }
222-
223- func TestChannel_ReceiveMessage_WhenContextDeadline (t * testing.T ) {
224- // Arrange
225- defer goleak .VerifyNone (t )
226- client , _ := newInProcessTransportPair ("localhost" , 1 )
227- c := newChannel (client , 1 )
228- defer silentClose (c )
229- c .setState (SessionStateEstablished )
230- ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Millisecond )
231- defer cancel ()
232-
233- // Act
234- actual , err := c .ReceiveMessage (ctx )
235-
236- // Assert
237- assert .Error (t , err )
238- assert .Equal (t , "receive message: context deadline exceeded" , err .Error ())
239- assert .Nil (t , actual )
216+ select {
217+ case <- ctx .Done ():
218+ t .Fatal (ctx .Err ())
219+ case actual , ok := <- c .MsgChan ():
220+ // Assert
221+ assert .True (t , ok )
222+ assert .Equal (t , m , actual )
223+ }
240224}
241225
242226func TestChannel_ReceiveMessage_WhenFinishedState (t * testing.T ) {
@@ -262,12 +246,16 @@ func receiveMessageWithState(t *testing.T, state SessionState) {
262246 time .Sleep (50 * time .Millisecond )
263247 c .setState (state )
264248 }()
265- actual , err := c .ReceiveMessage (ctx )
266249
267- // Assert
268- assert .Error (t , err )
269- assert .Equal (t , "receive message: channel closed" , err .Error ())
270- assert .Nil (t , actual )
250+ // Act
251+ select {
252+ case <- ctx .Done ():
253+ t .Fatal (ctx .Err ())
254+ case actual , ok := <- c .MsgChan ():
255+ // Assert
256+ assert .False (t , ok )
257+ assert .Nil (t , actual )
258+ }
271259}
272260
273261func TestChannel_SendNotification_WhenEstablished (t * testing.T ) {
@@ -429,30 +417,14 @@ func TestChannel_ReceiveNotification_WhenEstablished(t *testing.T) {
429417 _ = server .Send (ctx , n )
430418
431419 // Act
432- actual , err := c .ReceiveNotification (ctx )
433-
434- // Assert
435- assert .NoError (t , err )
436- assert .Equal (t , n , actual )
437- }
438-
439- func TestChannel_ReceiveNotification_WhenContextCanceled (t * testing.T ) {
440- // Arrange
441- defer goleak .VerifyNone (t )
442- client , _ := newInProcessTransportPair ("localhost" , 1 )
443- c := newChannel (client , 1 )
444- defer silentClose (c )
445- c .setState (SessionStateEstablished )
446- ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Millisecond )
447- defer cancel ()
448-
449- // Act
450- actual , err := c .ReceiveNotification (ctx )
451-
452- // Assert
453- assert .Error (t , err )
454- assert .Equal (t , "receive notification: context deadline exceeded" , err .Error ())
455- assert .Nil (t , actual )
420+ select {
421+ case <- ctx .Done ():
422+ t .Fatal (ctx .Err ())
423+ case actual , ok := <- c .NotChan ():
424+ // Assert
425+ assert .True (t , ok )
426+ assert .Equal (t , n , actual )
427+ }
456428}
457429
458430func TestChannel_ReceiveNotification_WhenFinishedState (t * testing.T ) {
@@ -479,12 +451,16 @@ func receiveNotificationWithState(t *testing.T, state SessionState) {
479451 time .Sleep (50 * time .Millisecond )
480452 c .setState (state )
481453 }()
482- actual , err := c .ReceiveNotification (ctx )
483454
484- // Assert
485- assert .Error (t , err )
486- assert .Equal (t , "receive notification: channel closed" , err .Error ())
487- assert .Nil (t , actual )
455+ // Act
456+ select {
457+ case <- ctx .Done ():
458+ t .Fatal (ctx .Err ())
459+ case actual , ok := <- c .NotChan ():
460+ // Assert
461+ assert .False (t , ok )
462+ assert .Nil (t , actual )
463+ }
488464}
489465
490466func TestChannel_SendRequestCommand_WhenEstablished (t * testing.T ) {
@@ -648,30 +624,14 @@ func TestChannel_ReceiveCommand_WhenEstablished(t *testing.T) {
648624 _ = server .Send (ctx , cmd )
649625
650626 // Act
651- actual , err := c .ReceiveRequestCommand (ctx )
652-
653- // Assert
654- assert .NoError (t , err )
655- assert .Equal (t , cmd , actual )
656- }
657-
658- func TestChannel_ReceiveCommand_WhenContextCanceled (t * testing.T ) {
659- // Arrange
660- defer goleak .VerifyNone (t )
661- client , _ := newInProcessTransportPair ("localhost" , 1 )
662- c := newChannel (client , 1 )
663- defer silentClose (c )
664- c .setState (SessionStateEstablished )
665- ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Millisecond )
666- defer cancel ()
667-
668- // Act
669- actual , err := c .ReceiveRequestCommand (ctx )
670-
671- // Assert
672- assert .Error (t , err )
673- assert .Equal (t , "receive request command: context deadline exceeded" , err .Error ())
674- assert .Nil (t , actual )
627+ select {
628+ case <- ctx .Done ():
629+ t .Fatal (ctx .Err ())
630+ case actual , ok := <- c .ReqCmdChan ():
631+ // Assert
632+ assert .True (t , ok )
633+ assert .Equal (t , cmd , actual )
634+ }
675635}
676636
677637func TestChannel_ReceiveCommand_WhenFinishedState (t * testing.T ) {
@@ -698,12 +658,16 @@ func receiveCommandWithState(t *testing.T, state SessionState) {
698658 time .Sleep (50 * time .Millisecond )
699659 c .setState (state )
700660 }()
701- actual , err := c .ReceiveRequestCommand (ctx )
702661
703- // Assert
704- assert .Error (t , err )
705- assert .Equal (t , "receive request command: channel closed" , err .Error ())
706- assert .Nil (t , actual )
662+ // Act
663+ select {
664+ case <- ctx .Done ():
665+ t .Fatal (ctx .Err ())
666+ case actual , ok := <- c .ReqCmdChan ():
667+ // Assert
668+ assert .False (t , ok )
669+ assert .Nil (t , actual )
670+ }
707671}
708672
709673func TestChannel_ProcessCommand (t * testing.T ) {
@@ -787,7 +751,13 @@ func TestChannel_ProcessCommand_ResponseWithAnotherId(t *testing.T) {
787751 assert .Nil (t , actual )
788752 ctx , cancel = context .WithTimeout (context .Background (), 250 * time .Millisecond )
789753 defer cancel ()
790- actualRespCmd , err := c .ReceiveResponseCommand (ctx )
791- assert .NoError (t , err )
792- assert .Equal (t , respCmd , actualRespCmd )
754+
755+ // Act
756+ select {
757+ case <- ctx .Done ():
758+ t .Fatal (ctx .Err ())
759+ case actualRespCmd , ok := <- c .RespCmdChan ():
760+ assert .True (t , ok )
761+ assert .Equal (t , respCmd , actualRespCmd )
762+ }
793763}
0 commit comments