-
Notifications
You must be signed in to change notification settings - Fork 3
/
replicaset_test.go
169 lines (146 loc) · 4.19 KB
/
replicaset_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package vshard_router //nolint:revive
import (
"bytes"
"context"
"fmt"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/tarantool/go-tarantool/v2"
"github.com/vmihailenco/msgpack/v5"
mockpool "github.com/KaymeKaydex/go-vshard-router/mocks/pool"
)
func TestReplicasetInfo_String(t *testing.T) {
rsUUID := uuid.New()
rsInfo := ReplicasetInfo{
Name: "test",
UUID: rsUUID,
}
rs := Replicaset{
info: rsInfo,
}
require.Equal(t, rsInfo.String(), rs.String())
require.Contains(t, rsInfo.String(), "test")
require.Contains(t, rsInfo.String(), rsUUID.String())
}
func TestReplicaset_BucketStat(t *testing.T) {
ctx := context.TODO()
rsUUID := uuid.New()
rsInfo := ReplicasetInfo{
Name: "test",
UUID: rsUUID,
}
rs := Replicaset{
info: rsInfo,
}
t.Run("pool do error", func(t *testing.T) {
futureError := fmt.Errorf("testErr")
errFuture := tarantool.NewFuture(tarantool.NewCallRequest("test"))
errFuture.SetError(futureError)
mPool := mockpool.NewPool(t)
mPool.On("Do", mock.Anything, mock.Anything).Return(errFuture)
rs.conn = mPool
_, err := rs.BucketStat(ctx, 123)
require.Equal(t, futureError, err)
})
t.Run("unsupported or broken proto resp", func(t *testing.T) {
f := tarantool.NewFuture(tarantool.NewCallRequest("vshard.storage.bucket_stat"))
bts, _ := msgpack.Marshal([]interface{}{1})
err := f.SetResponse(tarantool.Header{}, bytes.NewReader(bts))
require.NoError(t, err)
mPool := mockpool.NewPool(t)
mPool.On("Do", mock.Anything, mock.Anything).Return(f)
rs.conn = mPool
// todo: add real tests
statInfo, err := rs.BucketStat(ctx, 123)
require.Error(t, err)
require.Equal(t, statInfo, BucketStatInfo{BucketID: 0, Status: ""})
})
/*
TODO: add test for wrong bucket response
unix/:./data/storage_1_a.control> vshard.storage.bucket_stat(1000)
---
- null
- bucket_id: 1000
reason: Not found
code: 1
type: ShardingError
message: 'Cannot perform action with bucket 1000, reason: Not found'
name: WRONG_BUCKET
...
*/
}
func TestCalculateEtalonBalance(t *testing.T) {
tests := []struct {
name string
replicasets []Replicaset
bucketCount int
expectedCounts []int
expectError bool
}{
{
name: "FullBalance",
replicasets: []Replicaset{
{info: ReplicasetInfo{Weight: 1, PinnedCount: 0, IgnoreDisbalance: false}},
{info: ReplicasetInfo{Weight: 1, PinnedCount: 0, IgnoreDisbalance: false}},
{info: ReplicasetInfo{Weight: 1, PinnedCount: 0, IgnoreDisbalance: false}},
},
bucketCount: 9,
expectedCounts: []int{3, 3, 3},
expectError: false,
},
{
name: "PinnedMoreThanWeight",
replicasets: []Replicaset{
{info: ReplicasetInfo{Weight: 1, PinnedCount: 60, IgnoreDisbalance: false}},
{info: ReplicasetInfo{Weight: 1, PinnedCount: 0, IgnoreDisbalance: false}},
},
bucketCount: 100,
expectedCounts: []int{60, 40},
expectError: false,
},
{
name: "ZeroWeight",
replicasets: []Replicaset{
{info: ReplicasetInfo{Weight: 0, PinnedCount: 0, IgnoreDisbalance: false}},
{info: ReplicasetInfo{Weight: 1, PinnedCount: 0, IgnoreDisbalance: false}},
},
bucketCount: 10,
expectError: false,
expectedCounts: []int{0, 10},
},
{
name: "ZeroAllWeights",
replicasets: []Replicaset{
{info: ReplicasetInfo{Weight: 0, PinnedCount: 0, IgnoreDisbalance: false}},
{info: ReplicasetInfo{Weight: 0, PinnedCount: 0, IgnoreDisbalance: false}},
},
bucketCount: 10,
expectError: true,
},
{
name: "UnevenDistribution",
replicasets: []Replicaset{
{info: ReplicasetInfo{Weight: 1, PinnedCount: 0, IgnoreDisbalance: false}},
{info: ReplicasetInfo{Weight: 2, PinnedCount: 0, IgnoreDisbalance: false}},
},
bucketCount: 7,
expectError: false,
expectedCounts: []int{2, 5},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := CalculateEtalonBalance(tt.replicasets, tt.bucketCount)
if tt.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
for i, expectedCount := range tt.expectedCounts {
require.Equal(t, expectedCount, tt.replicasets[i].EtalonBucketCount)
}
}
})
}
}