3232func TestRegisterConnection_HappyPath (t * testing.T ) {
3333 ctx , cancel := context .WithCancel (context .Background ())
3434 defer cancel ()
35- r , connectedAgents , byAgentId , byProjectId , _ , info := setupTracker (t )
35+ r , connectedAgents , byAgentId , _ , info := setupTracker (t )
3636
37- byProjectId .EXPECT ().
38- Set (gomock .Any (), info .ProjectId , info .ConnectionId , gomock .Any ())
3937 byAgentId .EXPECT ().
4038 Set (gomock .Any (), info .AgentId , info .ConnectionId , gomock .Any ())
4139 connectedAgents .EXPECT ().
@@ -52,15 +50,12 @@ func TestRegisterConnection_HappyPath(t *testing.T) {
5250func TestRegisterConnection_AllCalledOnError (t * testing.T ) {
5351 ctx , cancel := context .WithCancel (context .Background ())
5452 defer cancel ()
55- r , connectedAgents , byAgentId , byProjectId , _ , info := setupTracker (t )
53+ r , connectedAgents , byAgentId , _ , info := setupTracker (t )
5654
5755 err1 := errors .New ("err1" )
5856 err2 := errors .New ("err2" )
5957 err3 := errors .New ("err3" )
6058
61- byProjectId .EXPECT ().
62- Set (gomock .Any (), info .ProjectId , info .ConnectionId , gomock .Any ()).
63- Return (err1 )
6459 byAgentId .EXPECT ().
6560 Set (gomock .Any (), info .AgentId , info .ConnectionId , gomock .Any ()).
6661 Return (err2 )
@@ -81,14 +76,8 @@ func TestRegisterConnection_AllCalledOnError(t *testing.T) {
8176func TestUnregisterConnection_HappyPath (t * testing.T ) {
8277 ctx , cancel := context .WithCancel (context .Background ())
8378 defer cancel ()
84- r , connectedAgents , byAgentId , byProjectId , _ , info := setupTracker (t )
79+ r , connectedAgents , byAgentId , _ , info := setupTracker (t )
8580
86- gomock .InOrder (
87- byProjectId .EXPECT ().
88- Set (gomock .Any (), info .ProjectId , info .ConnectionId , gomock .Any ()),
89- byProjectId .EXPECT ().
90- Unset (gomock .Any (), info .ProjectId , info .ConnectionId ),
91- )
9281 gomock .InOrder (
9382 byAgentId .EXPECT ().
9483 Set (gomock .Any (), info .AgentId , info .ConnectionId , gomock .Any ()),
@@ -113,18 +102,11 @@ func TestUnregisterConnection_HappyPath(t *testing.T) {
113102func TestUnregisterConnection_AllCalledOnError (t * testing.T ) {
114103 ctx , cancel := context .WithCancel (context .Background ())
115104 defer cancel ()
116- r , connectedAgents , byAgentId , byProjectId , _ , info := setupTracker (t )
105+ r , connectedAgents , byAgentId , _ , info := setupTracker (t )
117106
118107 err1 := errors .New ("err1" )
119108 err2 := errors .New ("err2" )
120109
121- gomock .InOrder (
122- byProjectId .EXPECT ().
123- Set (gomock .Any (), info .ProjectId , info .ConnectionId , gomock .Any ()),
124- byProjectId .EXPECT ().
125- Unset (gomock .Any (), info .ProjectId , info .ConnectionId ).
126- Return (err1 ),
127- )
128110 gomock .InOrder (
129111 byAgentId .EXPECT ().
130112 Set (gomock .Any (), info .AgentId , info .ConnectionId , gomock .Any ()),
@@ -150,43 +132,33 @@ func TestUnregisterConnection_AllCalledOnError(t *testing.T) {
150132}
151133
152134func TestGC_HappyPath (t * testing.T ) {
153- r , connectedAgents , byAgentId , byProjectId , _ , _ := setupTracker (t )
135+ r , connectedAgents , byAgentId , _ , _ := setupTracker (t )
154136
155137 wasCalled1 := false
156138 wasCalled2 := false
157- wasCalled3 := false
158139
159140 connectedAgents .EXPECT ().
160141 GC ().
161142 Return (func (_ context.Context ) (int , error ) {
162- wasCalled3 = true
143+ wasCalled2 = true
163144 return 3 , nil
164145 })
165146
166147 byAgentId .EXPECT ().
167- GC ().
168- Return (func (_ context.Context ) (int , error ) {
169- wasCalled2 = true
170- return 2 , nil
171- })
172-
173- byProjectId .EXPECT ().
174148 GC ().
175149 Return (func (_ context.Context ) (int , error ) {
176150 wasCalled1 = true
177- return 1 , nil
151+ return 2 , nil
178152 })
179153
180- assert .EqualValues (t , 6 , r .runGC (context .Background ()))
154+ assert .EqualValues (t , 5 , r .runGC (context .Background ()))
181155 assert .True (t , wasCalled1 )
182156 assert .True (t , wasCalled2 )
183- assert .True (t , wasCalled3 )
184157}
185158
186159func TestGC_AllCalledOnError (t * testing.T ) {
187- r , connectedAgents , byAgentId , byProjectId , rep , _ := setupTracker (t )
160+ r , connectedAgents , byAgentId , rep , _ := setupTracker (t )
188161
189- wasCalled1 := false
190162 wasCalled2 := false
191163 wasCalled3 := false
192164
@@ -212,37 +184,23 @@ func TestGC_AllCalledOnError(t *testing.T) {
212184 HandleProcessingError (gomock .Any (), gomock .Any (), "Failed to GC data in Redis" , matcher .ErrorEq ("err2" )),
213185 )
214186
215- gomock .InOrder (
216- byProjectId .EXPECT ().
217- GC ().
218- Return (func (_ context.Context ) (int , error ) {
219- wasCalled1 = true
220- return 1 , errors .New ("err1" )
221- }),
222- rep .EXPECT ().
223- HandleProcessingError (gomock .Any (), gomock .Any (), "Failed to GC data in Redis" , matcher .ErrorEq ("err1" )),
224- )
225-
226- assert .EqualValues (t , 6 , r .runGC (context .Background ()))
227- assert .True (t , wasCalled1 )
187+ assert .EqualValues (t , 5 , r .runGC (context .Background ()))
228188 assert .True (t , wasCalled2 )
229189 assert .True (t , wasCalled3 )
230190}
231191
232192func TestRefresh_HappyPath (t * testing.T ) {
233- r , connectedAgents , byAgentId , byProjectId , _ , _ := setupTracker (t )
193+ r , connectedAgents , byAgentId , _ , _ := setupTracker (t )
234194
235195 connectedAgents .EXPECT ().
236196 Refresh (gomock .Any (), gomock .Any ())
237197 byAgentId .EXPECT ().
238198 Refresh (gomock .Any (), gomock .Any ())
239- byProjectId .EXPECT ().
240- Refresh (gomock .Any (), gomock .Any ())
241199 r .refreshRegistrations (context .Background (), time .Now ())
242200}
243201
244202func TestRefresh_AllCalledOnError (t * testing.T ) {
245- r , connectedAgents , byAgentId , byProjectId , rep , _ := setupTracker (t )
203+ r , connectedAgents , byAgentId , rep , _ := setupTracker (t )
246204
247205 gomock .InOrder (
248206 connectedAgents .EXPECT ().
@@ -258,84 +216,11 @@ func TestRefresh_AllCalledOnError(t *testing.T) {
258216 rep .EXPECT ().
259217 HandleProcessingError (gomock .Any (), gomock .Any (), "Failed to refresh hash data in Redis" , matcher .ErrorEq ("err1" )),
260218 )
261- gomock .InOrder (
262- byProjectId .EXPECT ().
263- Refresh (gomock .Any (), gomock .Any ()).
264- Return (errors .New ("err2" )),
265- rep .EXPECT ().
266- HandleProcessingError (gomock .Any (), gomock .Any (), "Failed to refresh hash data in Redis" , matcher .ErrorEq ("err2" )),
267- )
268219 r .refreshRegistrations (context .Background (), time .Now ())
269220}
270221
271- func TestGetConnectionsByProjectId_HappyPath (t * testing.T ) {
272- r , _ , _ , byProjectId , _ , info := setupTracker (t )
273- infoBytes , err := proto .Marshal (info )
274- require .NoError (t , err )
275- byProjectId .EXPECT ().
276- Scan (gomock .Any (), info .ProjectId , gomock .Any ()).
277- Do (func (ctx context.Context , key int64 , cb redistool.ScanCallback ) (int , error ) {
278- var done bool
279- done , err = cb ("k2" , infoBytes , nil )
280- if err != nil || done {
281- return 0 , err
282- }
283- return 0 , nil
284- })
285- var cbCalled int
286- err = r .GetConnectionsByProjectId (context .Background (), info .ProjectId , func (i * ConnectedAgentInfo ) (done bool , err error ) {
287- cbCalled ++
288- assert .Empty (t , cmp .Diff (i , info , protocmp .Transform ()))
289- return false , nil
290- })
291- require .NoError (t , err )
292- assert .EqualValues (t , 1 , cbCalled )
293- }
294-
295- func TestGetConnectionsByProjectId_ScanError (t * testing.T ) {
296- r , _ , _ , byProjectId , rep , info := setupTracker (t )
297- gomock .InOrder (
298- byProjectId .EXPECT ().
299- Scan (gomock .Any (), info .ProjectId , gomock .Any ()).
300- Do (func (ctx context.Context , key int64 , cb redistool.ScanCallback ) (int , error ) {
301- done , err := cb ("" , nil , errors .New ("intended error" ))
302- require .NoError (t , err )
303- assert .False (t , done )
304- return 0 , nil
305- }),
306- rep .EXPECT ().
307- HandleProcessingError (gomock .Any (), gomock .Any (), "Redis hash scan" , matcher .ErrorEq ("intended error" )),
308- )
309- err := r .GetConnectionsByProjectId (context .Background (), info .ProjectId , func (i * ConnectedAgentInfo ) (done bool , err error ) {
310- require .FailNow (t , "unexpected call" )
311- return false , nil
312- })
313- require .NoError (t , err )
314- }
315-
316- func TestGetConnectionsByProjectId_UnmarshalError (t * testing.T ) {
317- r , _ , _ , byProjectId , rep , info := setupTracker (t )
318- gomock .InOrder (
319- byProjectId .EXPECT ().
320- Scan (gomock .Any (), info .ProjectId , gomock .Any ()).
321- Do (func (ctx context.Context , key int64 , cb redistool.ScanCallback ) (int , error ) {
322- done , err := cb ("k2" , []byte {1 , 2 , 3 }, nil ) // invalid bytes
323- require .NoError (t , err ) // ignores error to keep going
324- assert .False (t , done )
325- return 0 , nil
326- }),
327- rep .EXPECT ().
328- HandleProcessingError (gomock .Any (), gomock .Any (), "Redis proto.Unmarshal(ConnectedAgentInfo)" , matcher .ErrorIs (proto .Error )),
329- )
330- err := r .GetConnectionsByProjectId (context .Background (), info .ProjectId , func (i * ConnectedAgentInfo ) (done bool , err error ) {
331- require .FailNow (t , "unexpected call" )
332- return false , nil
333- })
334- require .NoError (t , err )
335- }
336-
337222func TestGetConnectionsByAgentId_HappyPath (t * testing.T ) {
338- r , _ , byAgentId , _ , _ , info := setupTracker (t )
223+ r , _ , byAgentId , _ , info := setupTracker (t )
339224 infoBytes , err := proto .Marshal (info )
340225 require .NoError (t , err )
341226 byAgentId .EXPECT ().
@@ -359,7 +244,7 @@ func TestGetConnectionsByAgentId_HappyPath(t *testing.T) {
359244}
360245
361246func TestGetConnectionsByAgentId_ScanError (t * testing.T ) {
362- r , _ , byAgentId , _ , rep , info := setupTracker (t )
247+ r , _ , byAgentId , rep , info := setupTracker (t )
363248 gomock .InOrder (
364249 byAgentId .EXPECT ().
365250 Scan (gomock .Any (), info .AgentId , gomock .Any ()).
@@ -380,7 +265,7 @@ func TestGetConnectionsByAgentId_ScanError(t *testing.T) {
380265}
381266
382267func TestGetConnectionsByAgentId_UnmarshalError (t * testing.T ) {
383- r , _ , byAgentId , _ , rep , info := setupTracker (t )
268+ r , _ , byAgentId , rep , info := setupTracker (t )
384269 byAgentId .EXPECT ().
385270 Scan (gomock .Any (), info .AgentId , gomock .Any ()).
386271 Do (func (ctx context.Context , key int64 , cb redistool.ScanCallback ) (int , error ) {
@@ -399,7 +284,7 @@ func TestGetConnectionsByAgentId_UnmarshalError(t *testing.T) {
399284}
400285
401286func TestGetConnectedAgentsCount_HappyPath (t * testing.T ) {
402- r , connectedAgents , _ , _ , _ , _ := setupTracker (t )
287+ r , connectedAgents , _ , _ , _ := setupTracker (t )
403288 connectedAgents .EXPECT ().
404289 Len (gomock .Any (), connectedAgentsKey ).
405290 Return (int64 (1 ), nil )
@@ -409,7 +294,7 @@ func TestGetConnectedAgentsCount_HappyPath(t *testing.T) {
409294}
410295
411296func TestGetConnectedAgentsCount_LenError (t * testing.T ) {
412- r , connectedAgents , _ , _ , _ , _ := setupTracker (t )
297+ r , connectedAgents , _ , _ , _ := setupTracker (t )
413298 connectedAgents .EXPECT ().
414299 Len (gomock .Any (), connectedAgentsKey ).
415300 Return (int64 (0 ), errors .New ("intended error" ))
@@ -418,22 +303,20 @@ func TestGetConnectedAgentsCount_LenError(t *testing.T) {
418303 assert .Zero (t , size )
419304}
420305
421- func setupTracker (t * testing.T ) (* RedisTracker , * mock_redis.MockExpiringHash [int64 , int64 ], * mock_redis.MockExpiringHash [int64 , int64 ], * mock_redis. MockExpiringHash [ int64 , int64 ], * mock_tool.MockErrReporter , * ConnectedAgentInfo ) {
306+ func setupTracker (t * testing.T ) (* RedisTracker , * mock_redis.MockExpiringHash [int64 , int64 ], * mock_redis.MockExpiringHash [int64 , int64 ], * mock_tool.MockErrReporter , * ConnectedAgentInfo ) {
422307 ctrl := gomock .NewController (t )
423308 rep := mock_tool .NewMockErrReporter (ctrl )
424309 connectedAgents := mock_redis.NewMockExpiringHash [int64 , int64 ](ctrl )
425310 byAgentId := mock_redis.NewMockExpiringHash [int64 , int64 ](ctrl )
426- byProjectId := mock_redis.NewMockExpiringHash [int64 , int64 ](ctrl )
427311 tr := & RedisTracker {
428- log : zaptest .NewLogger (t ),
429- errRep : rep ,
430- refreshPeriod : time .Minute ,
431- gcPeriod : time .Minute ,
432- connectionsByAgentId : byAgentId ,
433- connectionsByClusterId : byProjectId ,
434- connectedAgents : connectedAgents ,
312+ log : zaptest .NewLogger (t ),
313+ errRep : rep ,
314+ refreshPeriod : time .Minute ,
315+ gcPeriod : time .Minute ,
316+ connectionsByAgentId : byAgentId ,
317+ connectedAgents : connectedAgents ,
435318 }
436- return tr , connectedAgents , byAgentId , byProjectId , rep , connInfo ()
319+ return tr , connectedAgents , byAgentId , rep , connInfo ()
437320}
438321
439322func connInfo () * ConnectedAgentInfo {
@@ -447,6 +330,6 @@ func connInfo() *ConnectedAgentInfo {
447330 ConnectedAt : timestamppb .Now (),
448331 ConnectionId : 123 ,
449332 AgentId : 345 ,
450- ProjectId : 456 ,
333+ ClusterId : " 456" ,
451334 }
452335}
0 commit comments