@@ -37,15 +37,22 @@ import (
37
37
)
38
38
39
39
const (
40
- _testNamespace = "test-matching"
40
+ _testNamespaceFixed = "test-fixed"
41
+ _testNamespaceEphemeral = "test-ephemeral"
41
42
)
42
43
43
44
func TestGetShardOwner (t * testing.T ) {
44
45
cfg := config.ShardDistribution {
45
46
Enabled : true ,
46
47
Namespaces : []config.Namespace {
47
48
{
48
- Name : _testNamespace ,
49
+ Name : _testNamespaceFixed ,
50
+ Type : config .NamespaceTypeFixed ,
51
+ ShardNum : 32 ,
52
+ },
53
+ {
54
+ Name : _testNamespaceEphemeral ,
55
+ Type : config .NamespaceTypeEphemeral ,
49
56
},
50
57
},
51
58
}
@@ -59,50 +66,119 @@ func TestGetShardOwner(t *testing.T) {
59
66
expectedErrMsg string
60
67
}{
61
68
{
62
- name : "Existing_Success" ,
69
+ name : "InvalidNamespace" ,
70
+ request : & types.GetShardOwnerRequest {
71
+ Namespace : "namespace not found invalidNamespace" ,
72
+ ShardKey : "1" ,
73
+ },
74
+ expectedError : true ,
75
+ expectedErrMsg : "namespace not found" ,
76
+ },
77
+ {
78
+ name : "LookupError" ,
79
+ request : & types.GetShardOwnerRequest {
80
+ Namespace : _testNamespaceFixed ,
81
+ ShardKey : "1" ,
82
+ },
83
+ setupMocks : func (mockStore * store.MockStore ) {
84
+ mockStore .EXPECT ().GetShardOwner (gomock .Any (), _testNamespaceFixed , "1" ).Return ("" , errors .New ("lookup error" ))
85
+ },
86
+ expectedError : true ,
87
+ expectedErrMsg : "lookup error" ,
88
+ },
89
+ {
90
+ name : "Existing_Success_Fixed" ,
63
91
request : & types.GetShardOwnerRequest {
64
- Namespace : _testNamespace ,
92
+ Namespace : _testNamespaceFixed ,
65
93
ShardKey : "123" ,
66
94
},
67
95
setupMocks : func (mockStore * store.MockStore ) {
68
- mockStore .EXPECT ().GetShardOwner (gomock .Any (), _testNamespace , "123" ).Return ("owner1" , nil )
96
+ mockStore .EXPECT ().GetShardOwner (gomock .Any (), _testNamespaceFixed , "123" ).Return ("owner1" , nil )
69
97
},
70
98
expectedOwner : "owner1" ,
71
99
expectedError : false ,
72
100
},
73
-
74
101
{
75
- name : "InvalidNamespace " ,
102
+ name : "ShardNotFound_Fixed " ,
76
103
request : & types.GetShardOwnerRequest {
77
- Namespace : "namespace not found invalidNamespace" ,
78
- ShardKey : "1" ,
104
+ Namespace : _testNamespaceFixed ,
105
+ ShardKey : "NON-EXISTING-SHARD" ,
106
+ },
107
+ setupMocks : func (mockStore * store.MockStore ) {
108
+ mockStore .EXPECT ().GetShardOwner (gomock .Any (), _testNamespaceFixed , "NON-EXISTING-SHARD" ).Return ("" , store .ErrShardNotFound )
79
109
},
80
110
expectedError : true ,
81
- expectedErrMsg : "namespace not found" ,
111
+ expectedErrMsg : "shard not found" ,
82
112
},
83
113
{
84
- name : "LookupError " ,
114
+ name : "Existing_Success_Ephemeral " ,
85
115
request : & types.GetShardOwnerRequest {
86
- Namespace : _testNamespace ,
87
- ShardKey : "1" ,
116
+ Namespace : _testNamespaceEphemeral ,
117
+ ShardKey : "123" ,
118
+ },
119
+ setupMocks : func (mockStore * store.MockStore ) {
120
+ mockStore .EXPECT ().GetShardOwner (gomock .Any (), _testNamespaceEphemeral , "123" ).Return ("owner1" , nil )
121
+ },
122
+ expectedOwner : "owner1" ,
123
+ expectedError : false ,
124
+ },
125
+ {
126
+ name : "ShardNotFound_Ephemeral" ,
127
+ request : & types.GetShardOwnerRequest {
128
+ Namespace : _testNamespaceEphemeral ,
129
+ ShardKey : "NON-EXISTING-SHARD" ,
88
130
},
89
131
setupMocks : func (mockStore * store.MockStore ) {
90
- mockStore .EXPECT ().GetShardOwner (gomock .Any (), _testNamespace , "1" ).Return ("" , errors .New ("lookup error" ))
132
+ mockStore .EXPECT ().GetShardOwner (gomock .Any (), _testNamespaceEphemeral , "NON-EXISTING-SHARD" ).Return ("" , store .ErrShardNotFound )
133
+ mockStore .EXPECT ().GetState (gomock .Any (), _testNamespaceEphemeral ).Return (& store.NamespaceState {
134
+ ShardAssignments : map [string ]store.AssignedState {
135
+ "owner1" : {
136
+ AssignedShards : map [string ]* types.ShardAssignment {
137
+ "shard1" : {Status : types .AssignmentStatusREADY },
138
+ "shard2" : {Status : types .AssignmentStatusREADY },
139
+ "shard3" : {Status : types .AssignmentStatusREADY },
140
+ },
141
+ },
142
+ "owner2" : {
143
+ AssignedShards : map [string ]* types.ShardAssignment {
144
+ "shard4" : {Status : types .AssignmentStatusREADY },
145
+ },
146
+ },
147
+ },
148
+ }, nil )
149
+ // owner2 has the fewest shards assigned, so we assign the shard to it
150
+ mockStore .EXPECT ().AssignShard (gomock .Any (), _testNamespaceEphemeral , "NON-EXISTING-SHARD" , "owner2" ).Return (nil )
151
+ },
152
+ expectedOwner : "owner2" ,
153
+ expectedError : false ,
154
+ },
155
+ {
156
+ name : "ShardNotFound_Ephemeral_GetStateFailure" ,
157
+ request : & types.GetShardOwnerRequest {
158
+ Namespace : _testNamespaceEphemeral ,
159
+ ShardKey : "NON-EXISTING-SHARD" ,
160
+ },
161
+ setupMocks : func (mockStore * store.MockStore ) {
162
+ mockStore .EXPECT ().GetShardOwner (gomock .Any (), _testNamespaceEphemeral , "NON-EXISTING-SHARD" ).Return ("" , store .ErrShardNotFound )
163
+ mockStore .EXPECT ().GetState (gomock .Any (), _testNamespaceEphemeral ).Return (nil , errors .New ("get state failure" ))
91
164
},
92
165
expectedError : true ,
93
- expectedErrMsg : "lookup error " ,
166
+ expectedErrMsg : "get state failure " ,
94
167
},
95
168
{
96
- name : "ShardNotFound " ,
169
+ name : "ShardNotFound_Ephemeral_AssignShardFailure " ,
97
170
request : & types.GetShardOwnerRequest {
98
- Namespace : _testNamespace ,
171
+ Namespace : _testNamespaceEphemeral ,
99
172
ShardKey : "NON-EXISTING-SHARD" ,
100
173
},
101
174
setupMocks : func (mockStore * store.MockStore ) {
102
- mockStore .EXPECT ().GetShardOwner (gomock .Any (), _testNamespace , "NON-EXISTING-SHARD" ).Return ("" , store .ErrShardNotFound )
175
+ mockStore .EXPECT ().GetShardOwner (gomock .Any (), _testNamespaceEphemeral , "NON-EXISTING-SHARD" ).Return ("" , store .ErrShardNotFound )
176
+ mockStore .EXPECT ().GetState (gomock .Any (), _testNamespaceEphemeral ).Return (& store.NamespaceState {
177
+ ShardAssignments : map [string ]store.AssignedState {"owner1" : {AssignedShards : map [string ]* types.ShardAssignment {}}}}, nil )
178
+ mockStore .EXPECT ().AssignShard (gomock .Any (), _testNamespaceEphemeral , "NON-EXISTING-SHARD" , "owner1" ).Return (errors .New ("assign shard failure" ))
103
179
},
104
180
expectedError : true ,
105
- expectedErrMsg : "shard not found " ,
181
+ expectedErrMsg : "assign shard failure " ,
106
182
},
107
183
}
108
184
0 commit comments