TestDoubleFreeTurnHighConcurrency tests the bug under high concurrency
(t *testing.T)
| 137 | |
| 138 | // TestDoubleFreeTurnHighConcurrency tests the bug under high concurrency |
| 139 | func TestDoubleFreeTurnHighConcurrency(t *testing.T) { |
| 140 | var dialCount atomic.Int32 |
| 141 | var getSuccesses atomic.Int32 |
| 142 | var getFailures atomic.Int32 |
| 143 | |
| 144 | slowDialer := func(ctx context.Context) (net.Conn, error) { |
| 145 | dialCount.Add(1) |
| 146 | select { |
| 147 | case <-time.After(200 * time.Millisecond): |
| 148 | server, client := net.Pipe() |
| 149 | go func() { |
| 150 | defer server.Close() |
| 151 | buf := make([]byte, 1024) |
| 152 | for { |
| 153 | _, err := server.Read(buf) |
| 154 | if err != nil { |
| 155 | return |
| 156 | } |
| 157 | } |
| 158 | }() |
| 159 | return client, nil |
| 160 | case <-ctx.Done(): |
| 161 | return nil, ctx.Err() |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | opt := &Options{ |
| 166 | Dialer: slowDialer, |
| 167 | PoolSize: 20, |
| 168 | MaxConcurrentDials: 20, |
| 169 | MinIdleConns: 0, |
| 170 | PoolTimeout: 100 * time.Millisecond, |
| 171 | DialTimeout: 5 * time.Second, |
| 172 | } |
| 173 | |
| 174 | connPool := NewConnPool(opt) |
| 175 | defer connPool.Close() |
| 176 | |
| 177 | // Create many requests with varying timeouts |
| 178 | // Some will timeout before dial completes, triggering the putIdleConn delivery path |
| 179 | const numRequests = 100 |
| 180 | var wg sync.WaitGroup |
| 181 | |
| 182 | for i := 0; i < numRequests; i++ { |
| 183 | wg.Add(1) |
| 184 | go func(id int) { |
| 185 | defer wg.Done() |
| 186 | |
| 187 | // Vary timeout: some short (will timeout), some long (will succeed) |
| 188 | timeout := 100 * time.Millisecond |
| 189 | if id%3 == 0 { |
| 190 | timeout = 500 * time.Millisecond |
| 191 | } |
| 192 | |
| 193 | ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 194 | defer cancel() |
| 195 | |
| 196 | cn, err := connPool.Get(ctx) |