Skip to content

Commit

Permalink
refactor: added tests when context is cancelled for Vote()
Browse files Browse the repository at this point in the history
  • Loading branch information
Yashk767 committed Apr 22, 2024
1 parent 4b6908f commit 64af5d5
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions cmd/vote_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"encoding/hex"
"errors"
Types "github.com/ethereum/go-ethereum/core/types"
Expand All @@ -13,6 +14,7 @@ import (
"razor/utils"
"reflect"
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
Expand Down Expand Up @@ -1212,3 +1214,62 @@ func TestHandleBlock(t *testing.T) {
})
}
}

func TestVote(t *testing.T) {
var (
config types.Configurations
client *ethclient.Client
rogueData types.Rogue
account types.Account
stakerId uint32
backupNodeActionsToIgnore []string
)
type args struct {
header *Types.Header
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test when context is cancelled",
args: args{
header: &Types.Header{
Number: big.NewInt(101),
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

SetUpMockInterfaces()

clientUtilsMock.On("GetLatestBlockWithRetry", mock.Anything).Return(tt.args.header, nil)
cmdUtilsMock.On("HandleBlock", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)

ut := &UtilsStruct{}
errChan := make(chan error)
// Run Vote function in a goroutine
go func() {
errChan <- ut.Vote(ctx, config, client, rogueData, account, stakerId, backupNodeActionsToIgnore)
}()

// Wait for some time to allow Vote function to execute
time.Sleep(time.Second * 2)

// Cancel the context to simulate its done
cancel()

// Check the error returned from the function
err := <-errChan
if (err != nil) != tt.wantErr {
t.Errorf("Vote() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit 64af5d5

Please sign in to comment.