@@ -306,6 +306,7 @@ func TestCoordinationExecutor_Coordinate(t *testing.T) {
306306 func (
307307 walletPublicKeyHash [20 ]byte ,
308308 actionsChecklist []WalletActionType ,
309+ _ uint ,
309310 ) (CoordinationProposal , error ) {
310311 for _ , action := range actionsChecklist {
311312 if walletPublicKeyHash == publicKeyHash && action == ActionRedemption {
@@ -692,6 +693,7 @@ func TestCoordinationExecutor_ExecuteLeaderRoutine(t *testing.T) {
692693 func (
693694 walletPublicKeyHash [20 ]byte ,
694695 actionsChecklist []WalletActionType ,
696+ _ uint ,
695697 ) (
696698 CoordinationProposal ,
697699 error ,
@@ -791,6 +793,97 @@ func TestCoordinationExecutor_ExecuteLeaderRoutine(t *testing.T) {
791793 }
792794}
793795
796+ func TestCoordinationExecutor_GenerateProposal (t * testing.T ) {
797+ var tests = map [string ]struct {
798+ proposalGenerator CoordinationProposalGenerator
799+ expectedProposal CoordinationProposal
800+ expectedError error
801+ }{
802+ "first attempt success" : {
803+ proposalGenerator : newMockCoordinationProposalGenerator (
804+ func (
805+ _ [20 ]byte ,
806+ _ []WalletActionType ,
807+ _ uint ,
808+ ) (CoordinationProposal , error ) {
809+ return & NoopProposal {}, nil
810+ },
811+ ),
812+ expectedProposal : & NoopProposal {},
813+ expectedError : nil ,
814+ },
815+ "last attempt success" : {
816+ proposalGenerator : newMockCoordinationProposalGenerator (
817+ func (
818+ _ [20 ]byte ,
819+ _ []WalletActionType ,
820+ call uint ,
821+ ) (CoordinationProposal , error ) {
822+ if call == 1 {
823+ return nil , fmt .Errorf ("unexpected error" )
824+ } else if call == 2 {
825+ return & NoopProposal {}, nil
826+ } else {
827+ panic ("unexpected call" )
828+ }
829+ },
830+ ),
831+ expectedProposal : & NoopProposal {},
832+ expectedError : nil ,
833+ },
834+ "all attempts failed" : {
835+ proposalGenerator : newMockCoordinationProposalGenerator (
836+ func (
837+ _ [20 ]byte ,
838+ _ []WalletActionType ,
839+ call uint ,
840+ ) (CoordinationProposal , error ) {
841+ return nil , fmt .Errorf ("unexpected error %v" , call )
842+ },
843+ ),
844+ expectedProposal : nil ,
845+ expectedError : fmt .Errorf (
846+ "all attempts failed: [attempt [1] error: [unexpected error 1]; attempt [2] error: [unexpected error 2]]" ,
847+ ),
848+ },
849+ }
850+
851+ for testName , test := range tests {
852+ t .Run (testName , func (t * testing.T ) {
853+ executor := & coordinationExecutor {
854+ // Set only relevant fields.
855+ proposalGenerator : test .proposalGenerator ,
856+ }
857+
858+ proposal , err := executor .generateProposal (
859+ & CoordinationProposalRequest {}, // request fields not relevant
860+ 2 ,
861+ 1 * time .Second ,
862+ )
863+
864+ if ! reflect .DeepEqual (test .expectedError , err ) {
865+ t .Errorf (
866+ "unexpected error\n " +
867+ "expected: %v\n " +
868+ "actual: %v\n " ,
869+ test .expectedError ,
870+ err ,
871+ )
872+ }
873+
874+ if ! reflect .DeepEqual (test .expectedProposal , proposal ) {
875+ t .Errorf (
876+ "unexpected proposal\n " +
877+ "expected: %v\n " +
878+ "actual: %v\n " ,
879+ test .expectedProposal ,
880+ proposal ,
881+ )
882+ }
883+ })
884+ }
885+ }
886+
794887func TestCoordinationExecutor_ExecuteFollowerRoutine (t * testing.T ) {
795888 // Uncompressed public key corresponding to the 20-byte public key hash:
796889 // aa768412ceed10bd423c025542ca90071f9fb62d.
@@ -1163,16 +1256,19 @@ func TestCoordinationExecutor_ExecuteFollowerRoutine_WithIdleLeader(t *testing.T
11631256}
11641257
11651258type mockCoordinationProposalGenerator struct {
1259+ calls uint
11661260 delegate func (
11671261 walletPublicKeyHash [20 ]byte ,
11681262 actionsChecklist []WalletActionType ,
1263+ call uint ,
11691264 ) (CoordinationProposal , error )
11701265}
11711266
11721267func newMockCoordinationProposalGenerator (
11731268 delegate func (
11741269 walletPublicKeyHash [20 ]byte ,
11751270 actionsChecklist []WalletActionType ,
1271+ call uint ,
11761272 ) (CoordinationProposal , error ),
11771273) * mockCoordinationProposalGenerator {
11781274 return & mockCoordinationProposalGenerator {
@@ -1183,5 +1279,10 @@ func newMockCoordinationProposalGenerator(
11831279func (mcpg * mockCoordinationProposalGenerator ) Generate (
11841280 request * CoordinationProposalRequest ,
11851281) (CoordinationProposal , error ) {
1186- return mcpg .delegate (request .WalletPublicKeyHash , request .ActionsChecklist )
1282+ mcpg .calls ++
1283+ return mcpg .delegate (
1284+ request .WalletPublicKeyHash ,
1285+ request .ActionsChecklist ,
1286+ mcpg .calls ,
1287+ )
11871288}
0 commit comments