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