| 562 | } |
| 563 | |
| 564 | func TestCounterInt64_IsStale(t *testing.T) { |
| 565 | tests := []struct { |
| 566 | name string |
| 567 | lastProcessed time.Time |
| 568 | staleness time.Duration |
| 569 | expected bool |
| 570 | }{ |
| 571 | { |
| 572 | name: "not stale", |
| 573 | lastProcessed: time.Now(), |
| 574 | staleness: time.Second, |
| 575 | expected: false, |
| 576 | }, |
| 577 | { |
| 578 | name: "stale", |
| 579 | lastProcessed: time.Now().Add(-2 * time.Second), |
| 580 | staleness: time.Second, |
| 581 | expected: true, |
| 582 | }, |
| 583 | { |
| 584 | name: "exactly at staleness threshold", |
| 585 | lastProcessed: time.Now().Add(-time.Second), |
| 586 | staleness: time.Second, |
| 587 | expected: true, |
| 588 | }, |
| 589 | } |
| 590 | |
| 591 | for _, tt := range tests { |
| 592 | t.Run(tt.name, func(t *testing.T) { |
| 593 | counter := &counterInt64{} |
| 594 | counter.updatedAtUnixMs.Store(tt.lastProcessed.UnixMilli()) |
| 595 | assert.Equal(t, tt.expected, counter.IsStale(tt.staleness)) |
| 596 | }) |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | // Test that calling setValue directly (as done by remote update watchers or initial value fetch) refreshes the |
| 601 | // internal lastUpdated timestamp so that subsequent IsStale checks reflect the recent update. |