-
Notifications
You must be signed in to change notification settings - Fork 3
/
topology_test.go
111 lines (91 loc) · 2.6 KB
/
topology_test.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package vshard_router //nolint:revive
import (
"context"
"errors"
"testing"
mockpool "github.com/KaymeKaydex/go-vshard-router/mocks/pool"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)
func TestRouter_Topology(t *testing.T) {
router := Router{}
require.NotNil(t, router.Topology())
}
func TestController_AddInstance(t *testing.T) {
ctx := context.Background()
t.Run("no such replicaset", func(t *testing.T) {
router := Router{
idToReplicaset: map[uuid.UUID]*Replicaset{},
cfg: Config{
Loggerf: emptyLogfProvider,
},
}
err := router.Topology().AddInstance(ctx, uuid.New(), InstanceInfo{
Addr: "127.0.0.1:8060",
UUID: uuid.New(),
})
require.True(t, errors.Is(err, ErrReplicasetNotExists))
})
t.Run("invalid instance info", func(t *testing.T) {
router := Router{
idToReplicaset: map[uuid.UUID]*Replicaset{},
cfg: Config{
Loggerf: emptyLogfProvider,
},
}
err := router.Topology().AddInstance(ctx, uuid.New(), InstanceInfo{})
require.True(t, errors.Is(err, ErrInvalidInstanceInfo))
})
}
func TestController_RemoveInstance(t *testing.T) {
ctx := context.Background()
t.Run("no such replicaset", func(t *testing.T) {
router := Router{
idToReplicaset: map[uuid.UUID]*Replicaset{},
cfg: Config{
Loggerf: emptyLogfProvider,
},
}
err := router.Topology().RemoveInstance(ctx, uuid.New(), uuid.New())
require.True(t, errors.Is(err, ErrReplicasetNotExists))
})
}
func TestController_RemoveReplicaset(t *testing.T) {
ctx := context.Background()
uuidToRemove := uuid.New()
mPool := mockpool.NewPool(t)
mPool.On("CloseGraceful").Return(nil)
router := Router{
idToReplicaset: map[uuid.UUID]*Replicaset{
uuidToRemove: {conn: mPool},
},
cfg: Config{
Loggerf: emptyLogfProvider,
},
}
t.Run("no such replicaset", func(t *testing.T) {
t.Parallel()
errs := router.Topology().RemoveReplicaset(ctx, uuid.New())
require.True(t, errors.Is(errs[0], ErrReplicasetNotExists))
})
t.Run("successfully remove", func(t *testing.T) {
t.Parallel()
errs := router.Topology().RemoveReplicaset(ctx, uuidToRemove)
require.Empty(t, errs)
})
}
func TestRouter_AddReplicaset_AlreadyExists(t *testing.T) {
ctx := context.TODO()
alreadyExistingRsUUID := uuid.New()
router := Router{
idToReplicaset: map[uuid.UUID]*Replicaset{
alreadyExistingRsUUID: {},
},
cfg: Config{
Loggerf: emptyLogfProvider,
},
}
// Test that such replicaset already exists
err := router.AddReplicaset(ctx, ReplicasetInfo{UUID: alreadyExistingRsUUID}, []InstanceInfo{})
require.Equalf(t, ErrReplicasetExists, err, "such replicaset must already exists")
}