TestPool_ConcurrentAccess 测试并发访问
(t *testing.T)
| 296 | |
| 297 | // TestPool_ConcurrentAccess 测试并发访问 |
| 298 | func TestPool_ConcurrentAccess(t *testing.T) { |
| 299 | deps := createTestDeps(t) |
| 300 | pool := NewPool(&PoolOptions{ |
| 301 | Dependencies: deps, |
| 302 | MaxAgents: 100, |
| 303 | }) |
| 304 | defer func() { |
| 305 | if err := pool.Shutdown(); err != nil { |
| 306 | t.Errorf("Shutdown failed: %v", err) |
| 307 | } |
| 308 | }() |
| 309 | |
| 310 | ctx := context.Background() |
| 311 | concurrency := 50 |
| 312 | var wg sync.WaitGroup |
| 313 | |
| 314 | // 并发创建 Agent |
| 315 | for i := range concurrency { |
| 316 | wg.Add(1) |
| 317 | go func(idx int) { |
| 318 | defer wg.Done() |
| 319 | |
| 320 | config := createTestConfig("concurrent-agent-" + string(rune('0'+idx))) |
| 321 | _, err := pool.Create(ctx, config) |
| 322 | if err != nil { |
| 323 | t.Logf("Failed to create agent %d: %v", idx, err) |
| 324 | } |
| 325 | }(i) |
| 326 | } |
| 327 | |
| 328 | wg.Wait() |
| 329 | |
| 330 | // 验证池大小 |
| 331 | size := pool.Size() |
| 332 | if size != concurrency { |
| 333 | t.Logf("Expected %d agents, got %d (some creates may have failed)", concurrency, size) |
| 334 | } |
| 335 | |
| 336 | // 并发读取 Agent |
| 337 | for i := range concurrency { |
| 338 | wg.Add(1) |
| 339 | go func(idx int) { |
| 340 | defer wg.Done() |
| 341 | |
| 342 | agentID := "concurrent-agent-" + string(rune('0'+idx)) |
| 343 | _, exists := pool.Get(agentID) |
| 344 | if !exists { |
| 345 | t.Logf("Agent %s not found", agentID) |
| 346 | } |
| 347 | }(i) |
| 348 | } |
| 349 | |
| 350 | wg.Wait() |
| 351 | } |
| 352 | |
| 353 | // TestPool_Shutdown 测试关闭池 |
| 354 | func TestPool_Shutdown(t *testing.T) { |
nothing calls this directly
no test coverage detected