TestCounterInt64_TimestampCollision tests that allocateUpdatedAtMs produces unique timestamps even when called rapidly within the same millisecond.
(t *testing.T)
| 1471 | // TestCounterInt64_TimestampCollision tests that allocateUpdatedAtMs produces unique timestamps |
| 1472 | // even when called rapidly within the same millisecond. |
| 1473 | func TestCounterInt64_TimestampCollision(t *testing.T) { |
| 1474 | t.Run("allocateUpdatedAtMs produces monotonically increasing timestamps", func(t *testing.T) { |
| 1475 | base := &baseSharedVariable{} |
| 1476 | |
| 1477 | // Rapidly allocate many timestamps |
| 1478 | timestamps := make([]int64, 1000) |
| 1479 | for i := 0; i < 1000; i++ { |
| 1480 | timestamps[i] = base.allocateUpdatedAtMs() |
| 1481 | } |
| 1482 | |
| 1483 | // All should be strictly increasing |
| 1484 | for i := 1; i < len(timestamps); i++ { |
| 1485 | assert.Greater(t, timestamps[i], timestamps[i-1], |
| 1486 | "timestamp %d should be > timestamp %d", i, i-1) |
| 1487 | } |
| 1488 | }) |
| 1489 | |
| 1490 | t.Run("concurrent allocations produce unique timestamps", func(t *testing.T) { |
| 1491 | base := &baseSharedVariable{} |
| 1492 | |
| 1493 | var wg sync.WaitGroup |
| 1494 | results := make(chan int64, 100) |
| 1495 | |
| 1496 | // 10 goroutines each allocate 10 timestamps |
| 1497 | for g := 0; g < 10; g++ { |
| 1498 | wg.Add(1) |
| 1499 | go func() { |
| 1500 | defer wg.Done() |
| 1501 | for i := 0; i < 10; i++ { |
| 1502 | results <- base.allocateUpdatedAtMs() |
| 1503 | } |
| 1504 | }() |
| 1505 | } |
| 1506 | wg.Wait() |
| 1507 | close(results) |
| 1508 | |
| 1509 | // Collect all timestamps |
| 1510 | seen := make(map[int64]bool) |
| 1511 | for ts := range results { |
| 1512 | assert.False(t, seen[ts], "duplicate timestamp detected: %d", ts) |
| 1513 | seen[ts] = true |
| 1514 | } |
| 1515 | assert.Equal(t, 100, len(seen), "should have 100 unique timestamps") |
| 1516 | }) |
| 1517 | } |
| 1518 | |
| 1519 | // TestCounterInt64_BackgroundPushCoalescing tests that multiple rapid TryUpdate calls |
| 1520 | // are coalesced into fewer remote pushes. |
nothing calls this directly
no test coverage detected