|
| 1 | +package clickhouse |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "net" |
| 7 | + "runtime/pprof" |
| 8 | + "strings" |
| 9 | + "sync/atomic" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/Altinity/clickhouse-backup/v2/pkg/config" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestConnectClosesFailedPools(t *testing.T) { |
| 18 | + for _, stop := range []string{"break on error", "shutdown", "retry then shutdown"} { |
| 19 | + t.Run(stop, func(t *testing.T) { |
| 20 | + ctx, cancel := context.WithCancel(context.Background()) |
| 21 | + defer cancel() |
| 22 | + var attempts atomic.Int32 |
| 23 | + listener, err := net.Listen("tcp", "127.0.0.1:0") |
| 24 | + require.NoError(t, err) |
| 25 | + defer listener.Close() |
| 26 | + serverDone := make(chan struct{}) |
| 27 | + go func() { |
| 28 | + defer close(serverDone) |
| 29 | + for { |
| 30 | + conn, err := listener.Accept() |
| 31 | + if err != nil { |
| 32 | + return |
| 33 | + } |
| 34 | + // Reject the native handshake so Open succeeds but Ping fails. |
| 35 | + if attempts.Add(1) == 2 && stop == "retry then shutdown" { |
| 36 | + cancel() |
| 37 | + } |
| 38 | + _ = conn.Close() |
| 39 | + } |
| 40 | + }() |
| 41 | + t.Cleanup(func() { |
| 42 | + _ = listener.Close() |
| 43 | + <-serverDone |
| 44 | + }) |
| 45 | + |
| 46 | + ch := NewClickHouse(&config.ClickHouseConfig{ |
| 47 | + Host: "127.0.0.1", |
| 48 | + Port: uint(listener.Addr().(*net.TCPAddr).Port), |
| 49 | + Timeout: "1s", |
| 50 | + MaxConnections: 1, |
| 51 | + }) |
| 52 | + ch.ShutdownCtx = ctx |
| 53 | + ch.BreakConnectOnError = stop == "break on error" |
| 54 | + if stop == "shutdown" { |
| 55 | + cancel() |
| 56 | + } |
| 57 | + |
| 58 | + before := countDrainPoolGoroutines(t) |
| 59 | + // Bound the retry test even if it regresses and stops observing shutdown. |
| 60 | + connectDone := make(chan error, 1) |
| 61 | + go func() { connectDone <- ch.Connect() }() |
| 62 | + select { |
| 63 | + case err := <-connectDone: |
| 64 | + require.Error(t, err) |
| 65 | + case <-time.After(10 * time.Second): |
| 66 | + t.Fatal("Connect did not stop after a failed Ping") |
| 67 | + } |
| 68 | + // Clean up the last pool on failure, without hiding the leak assertion. |
| 69 | + if ch.conn != nil { |
| 70 | + t.Cleanup(func() { _ = ch.conn.Close() }) |
| 71 | + } |
| 72 | + wantAttempts := int32(1) |
| 73 | + if stop == "retry then shutdown" { |
| 74 | + wantAttempts = 2 |
| 75 | + } |
| 76 | + require.Equal(t, wantAttempts, attempts.Load()) |
| 77 | + require.False(t, ch.IsOpen) |
| 78 | + require.Eventually(t, func() bool { |
| 79 | + return countDrainPoolGoroutines(t) == before |
| 80 | + }, time.Second, 10*time.Millisecond, "failed Ping left a connection pool running") |
| 81 | + require.Nil(t, ch.conn) |
| 82 | + }) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// Each native driver pool owns a drain goroutine, including pools with no open |
| 87 | +// sockets. Checking these catches a leak that connection statistics cannot see. |
| 88 | +func countDrainPoolGoroutines(t *testing.T) int { |
| 89 | + t.Helper() |
| 90 | + var stacks bytes.Buffer |
| 91 | + require.NoError(t, pprof.Lookup("goroutine").WriteTo(&stacks, 2)) |
| 92 | + return strings.Count(stacks.String(), "github.com/ClickHouse/clickhouse-go/v2.(*connPool).runDrainPool(") |
| 93 | +} |
0 commit comments