MCPcopy Create free account
hub / github.com/erpc/erpc / TestCounterInt64_RemoteRollbackCallback

Function TestCounterInt64_RemoteRollbackCallback

data/shared_state_variable_test.go:1269–1469  ·  view source on GitHub ↗

TestCounterInt64_RemoteRollbackCallback tests that large rollback callbacks are triggered via remote updates (processNewState) when the rollback is APPLIED.

(t *testing.T)

Source from the content-addressed store, hash-verified

1267// TestCounterInt64_RemoteRollbackCallback tests that large rollback callbacks are triggered
1268// via remote updates (processNewState) when the rollback is APPLIED.
1269func TestCounterInt64_RemoteRollbackCallback(t *testing.T) {
1270 t.Run("large rollback applied and callback fires on remote update", func(t *testing.T) {
1271 counter := &counterInt64{
1272 ignoreRollbackOf: 100, // Ignore rollbacks <= 100
1273 registry: &sharedStateRegistry{
1274 logger: &log.Logger,
1275 instanceId: "test",
1276 },
1277 }
1278 counter.value.Store(10000)
1279 counter.updatedAtUnixMs.Store(1)
1280
1281 var callbackMu sync.Mutex
1282 var callbackCalls []struct{ current, new int64 }
1283 counter.OnLargeRollback(func(currentVal, newVal int64) {
1284 callbackMu.Lock()
1285 callbackCalls = append(callbackCalls, struct{ current, new int64 }{currentVal, newVal})
1286 callbackMu.Unlock()
1287 })
1288
1289 // Remote update with large rollback (gap=5000 > ignoreRollbackOf=100)
1290 updated := counter.processNewState(UpdateSourceRemoteSync, CounterInt64State{
1291 Value: 5000, // 5000 blocks rollback
1292 UpdatedAt: 2, // Fresher timestamp
1293 UpdatedBy: "remote-instance",
1294 })
1295
1296 // Value SHOULD be rolled back (large rollback is applied)
1297 assert.True(t, updated)
1298 assert.Equal(t, int64(5000), counter.GetValue())
1299
1300 // And callback SHOULD have been triggered
1301 callbackMu.Lock()
1302 defer callbackMu.Unlock()
1303 assert.Len(t, callbackCalls, 1, "rollback callback should fire on remote large rollback")
1304 if len(callbackCalls) > 0 {
1305 assert.Equal(t, int64(10000), callbackCalls[0].current)
1306 assert.Equal(t, int64(5000), callbackCalls[0].new)
1307 }
1308 })
1309
1310 t.Run("small rollback ignored and callback does NOT fire", func(t *testing.T) {
1311 counter := &counterInt64{
1312 ignoreRollbackOf: 100, // Ignore rollbacks <= 100
1313 registry: &sharedStateRegistry{
1314 logger: &log.Logger,
1315 instanceId: "test",
1316 },
1317 }
1318 counter.value.Store(10000)
1319 counter.updatedAtUnixMs.Store(1)
1320
1321 var callbackCalls []struct{ current, new int64 }
1322 counter.OnLargeRollback(func(currentVal, newVal int64) {
1323 callbackCalls = append(callbackCalls, struct{ current, new int64 }{currentVal, newVal})
1324 })
1325
1326 // Remote update with small rollback (gap=50 <= ignoreRollbackOf=100)

Callers

nothing calls this directly

Calls 8

OnLargeRollbackMethod · 0.95
processNewStateMethod · 0.95
GetValueMethod · 0.95
StoreMethod · 0.80
updatedAtMsMethod · 0.80
RunMethod · 0.65
LockMethod · 0.65
UnlockMethod · 0.65

Tested by

no test coverage detected