TestConn_UsedAtUpdatedOnWrite verifies that usedAt is updated when writing to connection
(t *testing.T)
| 54 | |
| 55 | // TestConn_UsedAtUpdatedOnWrite verifies that usedAt is updated when writing to connection |
| 56 | func TestConn_UsedAtUpdatedOnWrite(t *testing.T) { |
| 57 | // Create a mock connection |
| 58 | server, client := net.Pipe() |
| 59 | defer server.Close() |
| 60 | defer client.Close() |
| 61 | |
| 62 | cn := NewConn(client) |
| 63 | defer cn.Close() |
| 64 | |
| 65 | // Get initial usedAt time |
| 66 | initialUsedAt := cn.UsedAt() |
| 67 | |
| 68 | // Wait at least 100ms to ensure time difference (usedAt has ~50ms precision from cached time) |
| 69 | time.Sleep(100 * time.Millisecond) |
| 70 | |
| 71 | // Simulate a write operation by calling WithWriter |
| 72 | ctx := context.Background() |
| 73 | err := cn.WithWriter(ctx, time.Second, func(wr *proto.Writer) error { |
| 74 | // Don't actually write anything, just trigger the deadline update |
| 75 | return nil |
| 76 | }) |
| 77 | |
| 78 | if err != nil { |
| 79 | t.Fatalf("WithWriter failed: %v", err) |
| 80 | } |
| 81 | |
| 82 | // Get updated usedAt time |
| 83 | updatedUsedAt := cn.UsedAt() |
| 84 | |
| 85 | // Verify that usedAt was updated |
| 86 | if !updatedUsedAt.After(initialUsedAt) { |
| 87 | t.Errorf("Expected usedAt to be updated after write. Initial: %v, Updated: %v", |
| 88 | initialUsedAt, updatedUsedAt) |
| 89 | } |
| 90 | |
| 91 | // Verify the difference is reasonable (should be around 100ms, accounting for ~50ms cache precision) |
| 92 | diff := updatedUsedAt.Sub(initialUsedAt) |
| 93 | |
| 94 | // 50 ms is the cache precision, so we allow up to 110ms difference |
| 95 | if diff < 45*time.Millisecond || diff > 155*time.Millisecond { |
| 96 | t.Errorf("Expected usedAt difference to be around 100 (±50ms for cache) (+-5ms for sleep precision), got %v", diff) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // TestConn_UsedAtUpdatedOnMultipleOperations verifies that usedAt is updated on each operation |
| 101 | func TestConn_UsedAtUpdatedOnMultipleOperations(t *testing.T) { |
nothing calls this directly
no test coverage detected