-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_bootstrap_test.go
More file actions
96 lines (81 loc) · 2.93 KB
/
main_bootstrap_test.go
File metadata and controls
96 lines (81 loc) · 2.93 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"testing"
"time"
"github.com/hashicorp/raft"
"github.com/stretchr/testify/require"
)
func TestResolveBootstrapServers(t *testing.T) {
t.Run("members imply fixed bootstrap servers", func(t *testing.T) {
servers, err := resolveBootstrapServers("n1", []groupSpec{{id: 1, address: "10.0.0.11:50051"}}, "n1=10.0.0.11:50051")
require.NoError(t, err)
require.Equal(t, []raft.Server{
{Suffrage: raft.Voter, ID: "n1", Address: "10.0.0.11:50051"},
}, servers)
})
t.Run("empty members returns nil", func(t *testing.T) {
servers, err := resolveBootstrapServers("n1", []groupSpec{{id: 1, address: "10.0.0.11:50051"}}, "")
require.NoError(t, err)
require.Nil(t, servers)
})
t.Run("single group fixed members", func(t *testing.T) {
servers, err := resolveBootstrapServers(
"n1",
[]groupSpec{{id: 1, address: "10.0.0.11:50051"}},
"n1=10.0.0.11:50051,n2=10.0.0.12:50051",
)
require.NoError(t, err)
require.Equal(t, []raft.Server{
{Suffrage: raft.Voter, ID: "n1", Address: "10.0.0.11:50051"},
{Suffrage: raft.Voter, ID: "n2", Address: "10.0.0.12:50051"},
}, servers)
})
t.Run("multiple groups are rejected", func(t *testing.T) {
_, err := resolveBootstrapServers(
"n1",
[]groupSpec{{id: 1, address: "10.0.0.11:50051"}, {id: 2, address: "10.0.0.11:50052"}},
"n1=10.0.0.11:50051,n2=10.0.0.12:50051",
)
require.ErrorIs(t, err, ErrBootstrapMembersRequireSingleGroup)
})
t.Run("missing local member is rejected", func(t *testing.T) {
_, err := resolveBootstrapServers(
"n1",
[]groupSpec{{id: 1, address: "10.0.0.11:50051"}},
"n2=10.0.0.12:50051",
)
require.ErrorIs(t, err, ErrBootstrapMembersMissingLocalNode)
})
t.Run("local address mismatch is rejected", func(t *testing.T) {
_, err := resolveBootstrapServers(
"n1",
[]groupSpec{{id: 1, address: "10.0.0.11:50051"}},
"n1=10.0.0.99:50051,n2=10.0.0.12:50051",
)
require.ErrorIs(t, err, ErrBootstrapMembersLocalAddrMismatch)
})
t.Run("only separators are rejected", func(t *testing.T) {
_, err := resolveBootstrapServers("n1", []groupSpec{{id: 1, address: "10.0.0.11:50051"}}, " , , ")
require.ErrorIs(t, err, ErrNoBootstrapMembersConfigured)
})
}
func TestDurationToTicks(t *testing.T) {
t.Parallel()
t.Run("zero tick returns min", func(t *testing.T) {
require.Equal(t, 5, durationToTicks(200*time.Millisecond, 0, 5))
})
t.Run("exact division", func(t *testing.T) {
require.Equal(t, 20, durationToTicks(200*time.Millisecond, 10*time.Millisecond, 1))
})
t.Run("remainder rounds up", func(t *testing.T) {
require.Equal(t, 21, durationToTicks(205*time.Millisecond, 10*time.Millisecond, 1))
})
t.Run("below min returns min", func(t *testing.T) {
require.Equal(t, 10, durationToTicks(50*time.Millisecond, 10*time.Millisecond, 10))
})
}
func TestNewRaftFactory_UnsupportedEngine(t *testing.T) {
t.Parallel()
_, err := newRaftFactory(raftEngineType("unknown"))
require.ErrorIs(t, err, ErrUnsupportedRaftEngine)
}