@@ -20,10 +20,12 @@ import (
20
20
"github.com/stretchr/testify/mock"
21
21
"github.com/stretchr/testify/require"
22
22
23
+ "google.golang.org/grpc/status"
23
24
"google.golang.org/grpc/test/bufconn"
24
25
25
26
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
26
27
"github.com/cosmos/cosmos-sdk/types/module"
28
+ "github.com/vizualni/whoops"
27
29
"google.golang.org/grpc"
28
30
)
29
31
@@ -106,7 +108,8 @@ func TestQueryingMessagesForSigning(t *testing.T) {
106
108
expectsAnyError bool
107
109
}{
108
110
{
109
- name : "called with correct arguments" ,
111
+ name : "called with correct arguments" ,
112
+ expRes : []QueuedMessage [* testdata.SimpleMessage ]{},
110
113
mcksrv : func (t * testing.T ) * consensusmocks.QueryServer {
111
114
srv := consensusmocks .NewQueryServer (t )
112
115
srv .On ("QueuedMessagesForSigning" , mock .Anything , & consensus.QueryQueuedMessagesForSigningRequest {
@@ -498,3 +501,145 @@ func TestBroadcastingMessageSignatures(t *testing.T) {
498
501
})
499
502
}
500
503
}
504
+
505
+ func TestQueryConsensusReached (t * testing.T ) {
506
+ codec := makeCodec ()
507
+ ctx := context .Background ()
508
+
509
+ fakeErr := whoops .String ("oh no" )
510
+
511
+ for _ , tt := range []struct {
512
+ name string
513
+ mcksrv func (t * testing.T ) * consensusmocks.QueryServer
514
+ expectedErr error
515
+ expectedRes []ConsensusReachedMsg [* testdata.SimpleMessage ]
516
+ anyUnpacker codectypes.AnyUnpacker
517
+ }{
518
+ {
519
+ name : "when there are no messages it returns empty slice" ,
520
+ mcksrv : func (t * testing.T ) * consensusmocks.QueryServer {
521
+ srv := consensusmocks .NewQueryServer (t )
522
+ srv .On ("ConsensusReached" , mock .Anything , & consensus.QueryConsensusReachedRequest {
523
+ QueueTypeName : "queue-name" ,
524
+ }).Once ().Return (& consensus.QueryConsensusReachedResponse {}, nil )
525
+ return srv
526
+ },
527
+ },
528
+ {
529
+ name : "when there are messages it returns them" ,
530
+ mcksrv : func (t * testing.T ) * consensusmocks.QueryServer {
531
+ srv := consensusmocks .NewQueryServer (t )
532
+ srv .On ("ConsensusReached" , mock .Anything , & consensus.QueryConsensusReachedRequest {
533
+ QueueTypeName : "queue-name" ,
534
+ }).Once ().Return (& consensus.QueryConsensusReachedResponse {
535
+ Messages : []* consensus.MessageApproved {
536
+ {
537
+ Nonce : []byte ("abc" ),
538
+ Id : 123 ,
539
+ Msg : whoops .Must (codectypes .NewAnyWithValue (& testdata.SimpleMessage {
540
+ Sender : "bob" ,
541
+ })),
542
+ SignData : []* consensus.MessageApprovedSignData {
543
+ {
544
+ ValAddress : "1" ,
545
+ Signature : []byte ("1" ),
546
+ },
547
+ {
548
+ ValAddress : "2" ,
549
+ Signature : []byte ("2" ),
550
+ },
551
+ },
552
+ },
553
+ {
554
+ Nonce : []byte ("def" ),
555
+ Id : 456 ,
556
+ Msg : whoops .Must (codectypes .NewAnyWithValue (& testdata.SimpleMessage {
557
+ Sender : "alice" ,
558
+ })),
559
+ SignData : []* consensus.MessageApprovedSignData {
560
+ {
561
+ ValAddress : "1" ,
562
+ Signature : []byte ("11" ),
563
+ },
564
+ {
565
+ ValAddress : "2" ,
566
+ Signature : []byte ("22" ),
567
+ },
568
+ },
569
+ },
570
+ },
571
+ }, nil )
572
+ return srv
573
+ },
574
+ expectedRes : []ConsensusReachedMsg [* testdata.SimpleMessage ]{
575
+ {
576
+ ID : "123" ,
577
+ Nonce : []byte ("abc" ),
578
+ Msg : & testdata.SimpleMessage {
579
+ Sender : "bob" ,
580
+ },
581
+ Signatures : []ValidatorSignature {
582
+ {
583
+ ValAddress : "1" ,
584
+ Signature : []byte ("1" ),
585
+ },
586
+ {
587
+ ValAddress : "2" ,
588
+ Signature : []byte ("2" ),
589
+ },
590
+ },
591
+ },
592
+ {
593
+ ID : "456" ,
594
+ Nonce : []byte ("def" ),
595
+ Msg : & testdata.SimpleMessage {
596
+ Sender : "alice" ,
597
+ },
598
+ Signatures : []ValidatorSignature {
599
+ {
600
+ ValAddress : "1" ,
601
+ Signature : []byte ("11" ),
602
+ },
603
+ {
604
+ ValAddress : "2" ,
605
+ Signature : []byte ("22" ),
606
+ },
607
+ },
608
+ },
609
+ },
610
+ },
611
+ {
612
+ name : "if there is an error it returns it" ,
613
+ mcksrv : func (t * testing.T ) * consensusmocks.QueryServer {
614
+ srv := consensusmocks .NewQueryServer (t )
615
+ srv .On ("ConsensusReached" , mock .Anything , & consensus.QueryConsensusReachedRequest {
616
+ QueueTypeName : "queue-name" ,
617
+ }).Once ().Return (nil , fakeErr )
618
+ return srv
619
+ },
620
+ expectedErr : fakeErr ,
621
+ },
622
+ } {
623
+ t .Run (tt .name , func (t * testing.T ) {
624
+ srv := tt .mcksrv (t )
625
+ conn , err := grpc .DialContext (ctx , "" , grpc .WithInsecure (), grpc .WithContextDialer (queryServerDailer (t , srv )))
626
+ require .NoError (t , err )
627
+
628
+ if tt .anyUnpacker == nil {
629
+ tt .anyUnpacker = codec .Marshaler
630
+ }
631
+
632
+ msgs , err := queryConsensusReachedMessages [* testdata.SimpleMessage ](ctx , conn , tt .anyUnpacker , "queue-name" )
633
+ if serr := status .Convert (err ); serr != nil {
634
+ if tt .expectedErr != nil {
635
+ require .Equal (t , serr .Message (), tt .expectedErr .Error ())
636
+ } else {
637
+ require .NoError (t , err , "expected err %s, got err %s" , tt .expectedErr , err )
638
+ }
639
+ } else {
640
+ require .ErrorIs (t , err , tt .expectedErr )
641
+ }
642
+ require .Equal (t , tt .expectedRes , msgs )
643
+ })
644
+ }
645
+ }
0 commit comments