TestManager_ConcurrentApply 用几个 goroutine 同时 Apply,确保内部锁没问题。
(t *testing.T)
| 300 | |
| 301 | // TestManager_ConcurrentApply 用几个 goroutine 同时 Apply,确保内部锁没问题。 |
| 302 | func TestManager_ConcurrentApply(t *testing.T) { |
| 303 | backend := startEchoTLS(t, "x.example.com") |
| 304 | ln, _ := net.Listen("tcp", "127.0.0.1:0") |
| 305 | addr := ln.Addr().String() |
| 306 | _ = ln.Close() |
| 307 | |
| 308 | m := NewManager(filepath.Join(t.TempDir(), "state.json")) |
| 309 | defer m.Close() |
| 310 | |
| 311 | var wg sync.WaitGroup |
| 312 | for i := 0; i < 10; i++ { |
| 313 | wg.Add(1) |
| 314 | go func() { |
| 315 | defer wg.Done() |
| 316 | _ = m.Apply(ManagerConfig{ |
| 317 | Listen: addr, |
| 318 | Routes: []Route{{SNI: "x.example.com", Backend: backend, Mode: ModeTransparent}}, |
| 319 | }) |
| 320 | }() |
| 321 | } |
| 322 | wg.Wait() |
| 323 | |
| 324 | // 收尾:Listen 应可用 |
| 325 | ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 326 | defer cancel() |
| 327 | for { |
| 328 | select { |
| 329 | case <-ctx.Done(): |
| 330 | t.Fatal("proxy never came up") |
| 331 | default: |
| 332 | } |
| 333 | c, err := net.Dial("tcp", addr) |
| 334 | if err == nil { |
| 335 | _ = c.Close() |
| 336 | return |
| 337 | } |
| 338 | time.Sleep(20 * time.Millisecond) |
| 339 | } |
| 340 | } |
nothing calls this directly
no test coverage detected