@@ -43,7 +43,7 @@ func TestMessageRouting(t *testing.T) {
43
43
require .Equal (wantNodeID , nodeID )
44
44
require .Equal (wantMsg , msg )
45
45
},
46
- AppRequestF : func (_ context.Context , nodeID ids.NodeID , _ time.Time , msg []byte ) ([]byte , error ) {
46
+ AppRequestF : func (_ context.Context , nodeID ids.NodeID , _ time.Time , msg []byte ) ([]byte , * common. AppError ) {
47
47
appRequestCalled = true
48
48
require .Equal (wantNodeID , nodeID )
49
49
require .Equal (wantMsg , msg )
@@ -352,7 +352,7 @@ func TestCrossChainAppRequestFailed(t *testing.T) {
352
352
}
353
353
354
354
// Messages for unregistered handlers should be dropped gracefully
355
- func TestMessageForUnregisteredHandler (t * testing.T ) {
355
+ func TestAppGossipMessageForUnregisteredHandler (t * testing.T ) {
356
356
tests := []struct {
357
357
name string
358
358
msg []byte
@@ -379,26 +379,110 @@ func TestMessageForUnregisteredHandler(t *testing.T) {
379
379
AppGossipF : func (context.Context , ids.NodeID , []byte ) {
380
380
require .Fail ("should not be called" )
381
381
},
382
- AppRequestF : func (context.Context , ids.NodeID , time.Time , []byte ) ([]byte , error ) {
383
- require .Fail ("should not be called" )
384
- return nil , nil
385
- },
386
- CrossChainAppRequestF : func (context.Context , ids.ID , time.Time , []byte ) ([]byte , error ) {
382
+ }
383
+ network , err := NewNetwork (logging.NoLog {}, nil , prometheus .NewRegistry (), "" )
384
+ require .NoError (err )
385
+ require .NoError (network .AddHandler (handlerID , handler ))
386
+ require .NoError (network .AppGossip (ctx , ids .EmptyNodeID , tt .msg ))
387
+ })
388
+ }
389
+ }
390
+
391
+ // An unregistered handler should gracefully drop messages by responding
392
+ // to the requester with a common.AppError
393
+ func TestAppRequestMessageForUnregisteredHandler (t * testing.T ) {
394
+ tests := []struct {
395
+ name string
396
+ msg []byte
397
+ }{
398
+ {
399
+ name : "nil" ,
400
+ msg : nil ,
401
+ },
402
+ {
403
+ name : "empty" ,
404
+ msg : []byte {},
405
+ },
406
+ {
407
+ name : "non-empty" ,
408
+ msg : []byte ("foobar" ),
409
+ },
410
+ }
411
+
412
+ for _ , tt := range tests {
413
+ t .Run (tt .name , func (t * testing.T ) {
414
+ require := require .New (t )
415
+ ctx := context .Background ()
416
+ handler := & TestHandler {
417
+ AppRequestF : func (context.Context , ids.NodeID , time.Time , []byte ) ([]byte , * common.AppError ) {
387
418
require .Fail ("should not be called" )
388
419
return nil , nil
389
420
},
390
421
}
391
- network , err := NewNetwork (logging.NoLog {}, nil , prometheus .NewRegistry (), "" )
422
+
423
+ wantNodeID := ids .GenerateTestNodeID ()
424
+ wantRequestID := uint32 (111 )
425
+
426
+ done := make (chan struct {})
427
+ sender := & common.SenderTest {}
428
+ sender .SendAppErrorF = func (_ context.Context , nodeID ids.NodeID , requestID uint32 , errorCode int32 , errorMessage string ) error {
429
+ defer close (done )
430
+
431
+ require .Equal (wantNodeID , nodeID )
432
+ require .Equal (wantRequestID , requestID )
433
+ require .Equal (ErrUnregisteredHandler .Code , errorCode )
434
+ require .Equal (ErrUnregisteredHandler .Message , errorMessage )
435
+
436
+ return nil
437
+ }
438
+ network , err := NewNetwork (logging.NoLog {}, sender , prometheus .NewRegistry (), "" )
392
439
require .NoError (err )
393
440
require .NoError (network .AddHandler (handlerID , handler ))
394
441
395
- require .NoError (network .AppRequest (ctx , ids .EmptyNodeID , 0 , time.Time {}, tt .msg ))
396
- require .NoError (network .AppGossip (ctx , ids .EmptyNodeID , tt .msg ))
397
- require .NoError (network .CrossChainAppRequest (ctx , ids .Empty , 0 , time.Time {}, tt .msg ))
442
+ require .NoError (network .AppRequest (ctx , wantNodeID , wantRequestID , time.Time {}, tt .msg ))
443
+ <- done
398
444
})
399
445
}
400
446
}
401
447
448
+ // A handler that errors should send an AppError to the requesting peer
449
+ func TestAppError (t * testing.T ) {
450
+ require := require .New (t )
451
+ ctx := context .Background ()
452
+ appError := & common.AppError {
453
+ Code : 123 ,
454
+ Message : "foo" ,
455
+ }
456
+ handler := & TestHandler {
457
+ AppRequestF : func (context.Context , ids.NodeID , time.Time , []byte ) ([]byte , * common.AppError ) {
458
+ return nil , appError
459
+ },
460
+ }
461
+
462
+ wantNodeID := ids .GenerateTestNodeID ()
463
+ wantRequestID := uint32 (111 )
464
+
465
+ done := make (chan struct {})
466
+ sender := & common.SenderTest {}
467
+ sender .SendAppErrorF = func (_ context.Context , nodeID ids.NodeID , requestID uint32 , errorCode int32 , errorMessage string ) error {
468
+ defer close (done )
469
+
470
+ require .Equal (wantNodeID , nodeID )
471
+ require .Equal (wantRequestID , requestID )
472
+ require .Equal (appError .Code , errorCode )
473
+ require .Equal (appError .Message , errorMessage )
474
+
475
+ return nil
476
+ }
477
+ network , err := NewNetwork (logging.NoLog {}, sender , prometheus .NewRegistry (), "" )
478
+ require .NoError (err )
479
+ require .NoError (network .AddHandler (handlerID , handler ))
480
+ msg := PrefixMessage (ProtocolPrefix (handlerID ), []byte ("message" ))
481
+
482
+ require .NoError (network .AppRequest (ctx , wantNodeID , wantRequestID , time.Time {}, msg ))
483
+ <- done
484
+ }
485
+
402
486
// A response or timeout for a request we never made should return an error
403
487
func TestResponseForUnrequestedRequest (t * testing.T ) {
404
488
tests := []struct {
@@ -427,7 +511,7 @@ func TestResponseForUnrequestedRequest(t *testing.T) {
427
511
AppGossipF : func (context.Context , ids.NodeID , []byte ) {
428
512
require .Fail ("should not be called" )
429
513
},
430
- AppRequestF : func (context.Context , ids.NodeID , time.Time , []byte ) ([]byte , error ) {
514
+ AppRequestF : func (context.Context , ids.NodeID , time.Time , []byte ) ([]byte , * common. AppError ) {
431
515
require .Fail ("should not be called" )
432
516
return nil , nil
433
517
},
0 commit comments