-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathraft_retriever_test.go
More file actions
61 lines (47 loc) · 1.4 KB
/
raft_retriever_test.go
File metadata and controls
61 lines (47 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package syncing
import (
"context"
"sync"
"testing"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
"github.com/evstack/ev-node/pkg/genesis"
pkgraft "github.com/evstack/ev-node/pkg/raft"
)
type stubRaftNode struct {
mu sync.Mutex
callbacks []chan<- pkgraft.RaftApplyMsg
}
func (s *stubRaftNode) IsLeader() bool { return false }
func (s *stubRaftNode) HasQuorum() bool { return false }
func (s *stubRaftNode) GetState() *pkgraft.RaftBlockState { return nil }
func (s *stubRaftNode) Broadcast(context.Context, *pkgraft.RaftBlockState) error { return nil }
func (s *stubRaftNode) SetApplyCallback(ch chan<- pkgraft.RaftApplyMsg) {
s.mu.Lock()
defer s.mu.Unlock()
s.callbacks = append(s.callbacks, ch)
}
func (s *stubRaftNode) recordedCallbacks() []chan<- pkgraft.RaftApplyMsg {
s.mu.Lock()
defer s.mu.Unlock()
out := make([]chan<- pkgraft.RaftApplyMsg, len(s.callbacks))
copy(out, s.callbacks)
return out
}
func TestRaftRetrieverStopClearsApplyCallback(t *testing.T) {
t.Parallel()
raftNode := &stubRaftNode{}
retriever := newRaftRetriever(
raftNode,
genesis.Genesis{},
zerolog.Nop(),
nil,
func(context.Context, *pkgraft.RaftBlockState) error { return nil },
)
require.NoError(t, retriever.Start(t.Context()))
retriever.Stop()
callbacks := raftNode.recordedCallbacks()
require.Len(t, callbacks, 2)
require.NotNil(t, callbacks[0])
require.Nil(t, callbacks[1])
}