@@ -228,6 +228,40 @@ func Test_WatchKey_UpdateStale(t *testing.T) {
228
228
})
229
229
}
230
230
231
+ func Test_CAS_UpdateStale (t * testing.T ) {
232
+ ddbMock := NewDynamodbClientMock ()
233
+ codecMock := & CodecMock {}
234
+ descMock := & DescMock {}
235
+ descMockResult := & DescMock {id : "result" }
236
+ startTime := time .Now ().UTC ().Add (- time .Millisecond )
237
+
238
+ c := NewClientMock (ddbMock , codecMock , TestLogger {}, prometheus .NewPedanticRegistry (), defaultPullTime , defaultBackoff )
239
+ expectedUpdatedKeys := []string {"t1" , "t2" }
240
+ expectedUpdated := map [string ][]byte {
241
+ expectedUpdatedKeys [0 ]: []byte (expectedUpdatedKeys [0 ]),
242
+ expectedUpdatedKeys [1 ]: []byte (expectedUpdatedKeys [1 ]),
243
+ }
244
+ expectedBatch := map [dynamodbKey ][]byte {
245
+ {primaryKey : key , sortKey : expectedUpdatedKeys [0 ]}: []byte (expectedUpdatedKeys [0 ]),
246
+ {primaryKey : key , sortKey : expectedUpdatedKeys [1 ]}: []byte (expectedUpdatedKeys [1 ]),
247
+ }
248
+
249
+ ddbMock .On ("Query" ).Return (map [string ][]byte {}, nil ).Once ()
250
+ codecMock .On ("DecodeMultiKey" ).Return (descMock , nil ).Once ()
251
+ descMock .On ("Clone" ).Return (descMock ).Once ()
252
+ descMock .On ("FindDifference" , descMockResult ).Return (descMockResult , []string {}, nil ).Once ()
253
+ codecMock .On ("EncodeMultiKey" ).Return (expectedUpdated , nil ).Once ()
254
+ ddbMock .On ("Batch" , context .TODO (), expectedBatch , []dynamodbKey {}).Once ()
255
+
256
+ err := c .CAS (context .TODO (), key , func (in interface {}) (out interface {}, retry bool , err error ) {
257
+ return descMockResult , true , nil
258
+ })
259
+
260
+ require .NoError (t , err )
261
+ require .Equal (t , descMockResult , c .staleData [key ].data )
262
+ require .True (t , startTime .Before (c .staleData [key ].timestamp ))
263
+ }
264
+
231
265
func Test_WatchPrefix (t * testing.T ) {
232
266
ddbMock := NewDynamodbClientMock ()
233
267
codecMock := & CodecMock {}
@@ -365,6 +399,7 @@ func (m *CodecMock) DecodeMultiKey(map[string][]byte) (interface{}, error) {
365
399
}
366
400
367
401
type DescMock struct {
402
+ id string
368
403
mock.Mock
369
404
}
370
405
0 commit comments