TestConn_UsedAtConcurrentUpdates verifies that usedAt updates are thread-safe
(t *testing.T)
| 173 | |
| 174 | // TestConn_UsedAtConcurrentUpdates verifies that usedAt updates are thread-safe |
| 175 | func TestConn_UsedAtConcurrentUpdates(t *testing.T) { |
| 176 | // Create a mock connection |
| 177 | server, client := net.Pipe() |
| 178 | defer server.Close() |
| 179 | defer client.Close() |
| 180 | |
| 181 | cn := NewConn(client) |
| 182 | defer cn.Close() |
| 183 | |
| 184 | ctx := context.Background() |
| 185 | const numGoroutines = 10 |
| 186 | const numIterations = 10 |
| 187 | |
| 188 | // Launch multiple goroutines that perform operations concurrently |
| 189 | done := make(chan bool, numGoroutines) |
| 190 | for i := 0; i < numGoroutines; i++ { |
| 191 | go func() { |
| 192 | for j := 0; j < numIterations; j++ { |
| 193 | // Alternate between read and write operations |
| 194 | if j%2 == 0 { |
| 195 | _ = cn.WithReader(ctx, time.Second, func(rd *proto.Reader) error { |
| 196 | return nil |
| 197 | }) |
| 198 | } else { |
| 199 | _ = cn.WithWriter(ctx, time.Second, func(wr *proto.Writer) error { |
| 200 | return nil |
| 201 | }) |
| 202 | } |
| 203 | time.Sleep(time.Millisecond) |
| 204 | } |
| 205 | done <- true |
| 206 | }() |
| 207 | } |
| 208 | |
| 209 | // Wait for all goroutines to complete |
| 210 | for i := 0; i < numGoroutines; i++ { |
| 211 | <-done |
| 212 | } |
| 213 | |
| 214 | // Verify that usedAt was updated (should be recent) |
| 215 | usedAt := cn.UsedAt() |
| 216 | timeSinceUsed := time.Since(usedAt) |
| 217 | |
| 218 | // Should be very recent (within last second) |
| 219 | if timeSinceUsed > time.Second { |
| 220 | t.Errorf("Expected usedAt to be recent, but it was %v ago", timeSinceUsed) |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // TestConn_UsedAtPrecision verifies that usedAt has 50ms precision (not nanosecond) |
| 225 | func TestConn_UsedAtPrecision(t *testing.T) { |
nothing calls this directly
no test coverage detected