| 373 | } |
| 374 | |
| 375 | func TestCounterInt64_TryUpdateIfStale(t *testing.T) { |
| 376 | tests := []struct { |
| 377 | name string |
| 378 | setupMocks func(*MockConnector, *MockLock) |
| 379 | initialValue int64 |
| 380 | staleness time.Duration |
| 381 | updateValue int64 |
| 382 | lastProcessed time.Time |
| 383 | expectedValue int64 |
| 384 | eventualValue int64 |
| 385 | eventual bool |
| 386 | expectedError error |
| 387 | expectedCalls int |
| 388 | }{ |
| 389 | { |
| 390 | name: "not stale skips update", |
| 391 | setupMocks: func(c *MockConnector, l *MockLock) { |
| 392 | // No mocks needed as it should return early |
| 393 | }, |
| 394 | initialValue: 5, |
| 395 | staleness: time.Second, |
| 396 | updateValue: 10, |
| 397 | lastProcessed: time.Now(), |
| 398 | expectedValue: 5, |
| 399 | expectedError: nil, |
| 400 | expectedCalls: 0, |
| 401 | }, |
| 402 | { |
| 403 | name: "stale local value same as remote unstales local value", |
| 404 | setupMocks: func(c *MockConnector, l *MockLock) { |
| 405 | // No remote I/O expected in the synchronous path (local-only). |
| 406 | }, |
| 407 | initialValue: 5, |
| 408 | staleness: time.Second, |
| 409 | updateValue: 5, |
| 410 | lastProcessed: time.Now().Add(-2 * time.Second), |
| 411 | expectedValue: 5, |
| 412 | expectedError: nil, |
| 413 | expectedCalls: 1, |
| 414 | }, |
| 415 | { |
| 416 | name: "stale value updates successfully", |
| 417 | setupMocks: func(c *MockConnector, l *MockLock) { |
| 418 | c.On("Lock", mock.Anything, "test", mock.Anything).Return(l, nil) |
| 419 | l.On("Unlock", mock.Anything).Return(nil) |
| 420 | c.On("Get", mock.Anything, ConnectorMainIndex, "test", "value", nil). |
| 421 | Return([]byte(`{"v":4,"t":1,"b":"test"}`), nil) |
| 422 | c.On("Set", mock.Anything, "test", "value", mock.Anything, mock.Anything).Return(nil) |
| 423 | c.On("PublishCounterInt64", mock.Anything, "test", mock.Anything).Return(nil) |
| 424 | }, |
| 425 | initialValue: 5, |
| 426 | staleness: time.Second, |
| 427 | updateValue: 10, |
| 428 | lastProcessed: time.Now().Add(-2 * time.Second), |
| 429 | expectedValue: 10, |
| 430 | expectedError: nil, |
| 431 | expectedCalls: 1, |
| 432 | }, |