MCPcopy
hub / github.com/redis/go-redis / TestConn_UsedAtConcurrentUpdates

Function TestConn_UsedAtConcurrentUpdates

internal/pool/conn_used_at_test.go:175–222  ·  internal/pool/conn_used_at_test.go::TestConn_UsedAtConcurrentUpdates

TestConn_UsedAtConcurrentUpdates verifies that usedAt updates are thread-safe

(t *testing.T)

Source from the content-addressed store, hash-verified

173
174// TestConn_UsedAtConcurrentUpdates verifies that usedAt updates are thread-safe
175func 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)
225func TestConn_UsedAtPrecision(t *testing.T) {

Callers

nothing calls this directly

Calls 6

CloseMethod · 0.95
WithReaderMethod · 0.95
WithWriterMethod · 0.95
UsedAtMethod · 0.95
NewConnFunction · 0.85
CloseMethod · 0.65

Tested by

no test coverage detected