|
| 1 | +package clickhouse |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "database/sql/driver" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func Test_ConnCheck(t *testing.T) { |
| 14 | + const ( |
| 15 | + ddl = ` |
| 16 | + CREATE TABLE clickhouse_test_conncheck ( |
| 17 | + Value String |
| 18 | + ) Engine = Memory |
| 19 | + ` |
| 20 | + dml = ` |
| 21 | + INSERT INTO clickhouse_test_conncheck |
| 22 | + VALUES (?) |
| 23 | + ` |
| 24 | + ) |
| 25 | + |
| 26 | + if connect, err := sql.Open("clickhouse", "tcp://127.0.0.1:9000?debug=false"); assert.NoError(t, err) { |
| 27 | + // We could change settings only at session level. |
| 28 | + // If we have only 1 connection, we guarantee that we change settings for them. |
| 29 | + connect.SetMaxOpenConns(1) |
| 30 | + if _, err := connect.Exec("DROP TABLE IF EXISTS clickhouse_test_conncheck"); assert.NoError(t, err) { |
| 31 | + if _, err := connect.Exec(ddl); assert.NoError(t, err) { |
| 32 | + _, err = connect.Exec("set idle_connection_timeout=1") |
| 33 | + assert.NoError(t, err) |
| 34 | + |
| 35 | + _, err = connect.Exec("set tcp_keep_alive_timeout=0") |
| 36 | + assert.NoError(t, err) |
| 37 | + |
| 38 | + time.Sleep(1100 * time.Millisecond) |
| 39 | + ctx := context.Background() |
| 40 | + tx, err := connect.BeginTx(ctx, nil) |
| 41 | + assert.NoError(t, err) |
| 42 | + |
| 43 | + _, err = tx.PrepareContext(ctx, dml) |
| 44 | + assert.NoError(t, err) |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func Test_ConnCheckNegative(t *testing.T) { |
| 51 | + const ( |
| 52 | + ddl = ` |
| 53 | + CREATE TABLE clickhouse_test_conncheck_negative ( |
| 54 | + Value String |
| 55 | + ) Engine = Memory |
| 56 | + ` |
| 57 | + dml = ` |
| 58 | + INSERT INTO clickhouse_test_conncheck_negative |
| 59 | + VALUES (?) |
| 60 | + ` |
| 61 | + ) |
| 62 | + |
| 63 | + if connect, err := sql.Open("clickhouse", "tcp://127.0.0.1:9000?debug=true&check_connection_liveness=false"); assert.NoError(t, err) { |
| 64 | + // We can only change the settings at the connection level. |
| 65 | + // If we have only one connection, we change the settings specifically for that connection. |
| 66 | + connect.SetMaxOpenConns(1) |
| 67 | + if _, err := connect.Exec("DROP TABLE IF EXISTS clickhouse_test_conncheck_negative"); assert.NoError(t, err) { |
| 68 | + if _, err := connect.Exec(ddl); assert.NoError(t, err) { |
| 69 | + _, err = connect.Exec("set idle_connection_timeout=1") |
| 70 | + assert.NoError(t, err) |
| 71 | + |
| 72 | + _, err = connect.Exec("set tcp_keep_alive_timeout=0") |
| 73 | + assert.NoError(t, err) |
| 74 | + |
| 75 | + time.Sleep(1100 * time.Millisecond) |
| 76 | + ctx := context.Background() |
| 77 | + tx, err := connect.BeginTx(ctx, nil) |
| 78 | + assert.NoError(t, err) |
| 79 | + |
| 80 | + _, err = tx.PrepareContext(ctx, dml) |
| 81 | + assert.Equal(t, driver.ErrBadConn, err) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments