TestConn_UsedAtUpdatedOnRead verifies that usedAt is updated when reading from connection
(t *testing.T)
| 11 | |
| 12 | // TestConn_UsedAtUpdatedOnRead verifies that usedAt is updated when reading from connection |
| 13 | func TestConn_UsedAtUpdatedOnRead(t *testing.T) { |
| 14 | // Create a mock connection |
| 15 | server, client := net.Pipe() |
| 16 | defer server.Close() |
| 17 | defer client.Close() |
| 18 | |
| 19 | cn := NewConn(client) |
| 20 | defer cn.Close() |
| 21 | |
| 22 | // Get initial usedAt time |
| 23 | initialUsedAt := cn.UsedAt() |
| 24 | |
| 25 | // Wait 100ms to ensure time difference (usedAt has ~50ms precision from cached time) |
| 26 | time.Sleep(100 * time.Millisecond) |
| 27 | |
| 28 | // Simulate a read operation by calling WithReader |
| 29 | ctx := context.Background() |
| 30 | err := cn.WithReader(ctx, time.Second, func(rd *proto.Reader) error { |
| 31 | // Don't actually read anything, just trigger the deadline update |
| 32 | return nil |
| 33 | }) |
| 34 | |
| 35 | if err != nil { |
| 36 | t.Fatalf("WithReader failed: %v", err) |
| 37 | } |
| 38 | |
| 39 | // Get updated usedAt time |
| 40 | updatedUsedAt := cn.UsedAt() |
| 41 | |
| 42 | // Verify that usedAt was updated |
| 43 | if !updatedUsedAt.After(initialUsedAt) { |
| 44 | t.Errorf("Expected usedAt to be updated after read. Initial: %v, Updated: %v", |
| 45 | initialUsedAt, updatedUsedAt) |
| 46 | } |
| 47 | |
| 48 | // Verify the difference is reasonable (should be around 100ms, accounting for ~50ms cache precision and ~5ms sleep precision) |
| 49 | diff := updatedUsedAt.Sub(initialUsedAt) |
| 50 | if diff < 45*time.Millisecond || diff > 155*time.Millisecond { |
| 51 | t.Errorf("Expected usedAt difference to be around 100ms (±50ms for cache, ±5ms for sleep), got %v", diff) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // TestConn_UsedAtUpdatedOnWrite verifies that usedAt is updated when writing to connection |
| 56 | func TestConn_UsedAtUpdatedOnWrite(t *testing.T) { |
nothing calls this directly
no test coverage detected