Skip to content

Commit 287015e

Browse files
committed
ClickHouse#100 PingContext is now supported
1 parent 9df892d commit 287015e

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

clickhouse.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,5 +301,5 @@ func (ch *clickhouse) watchCancel(ctx context.Context) func() {
301301
}
302302
}
303303
}
304-
return nil
304+
return func() {}
305305
}

clickhouse_ping.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ import (
88
)
99

1010
func (ch *clickhouse) Ping(ctx context.Context) error {
11-
return ch.ping()
11+
return ch.ping(ctx)
1212
}
1313

14-
func (ch *clickhouse) ping() error {
14+
func (ch *clickhouse) ping(ctx context.Context) error {
1515
if ch.conn.closed {
1616
return driver.ErrBadConn
1717
}
1818
ch.logf("-> ping")
19+
finish := ch.watchCancel(ctx)
20+
defer finish()
1921
if err := ch.encoder.Uvarint(protocol.ClientPing); err != nil {
2022
return err
2123
}

clickhouse_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,36 @@ func Test_Context_Timeout(t *testing.T) {
10201020
}
10211021
}
10221022

1023+
func Test_Ping_Context_Timeout(t *testing.T) {
1024+
if connect, err := sql.Open("clickhouse", "tcp://127.0.0.1:9000?debug=true"); assert.NoError(t, err) && assert.NoError(t, connect.Ping()) {
1025+
{
1026+
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
1027+
defer cancel()
1028+
if err := connect.PingContext(ctx); assert.Error(t, err) {
1029+
assert.Equal(t, context.DeadlineExceeded, err)
1030+
}
1031+
}
1032+
{
1033+
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*20)
1034+
defer cancel()
1035+
if row := connect.QueryRowContext(ctx, "SELECT 1, sleep(2)"); assert.NotNil(t, row) {
1036+
var a, b int
1037+
assert.Equal(t, driver.ErrBadConn, row.Scan(&a, &b))
1038+
}
1039+
}
1040+
{
1041+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
1042+
defer cancel()
1043+
if row := connect.QueryRowContext(ctx, "SELECT 1, sleep(0.1)"); assert.NotNil(t, row) {
1044+
var value, value2 int
1045+
if assert.NoError(t, row.Scan(&value, &value2)) {
1046+
assert.Equal(t, int(1), value)
1047+
}
1048+
}
1049+
}
1050+
}
1051+
}
1052+
10231053
func Test_Timeout(t *testing.T) {
10241054
if connect, err := sql.Open("clickhouse", "tcp://127.0.0.1:9000?debug=true&read_timeout=0.2"); assert.NoError(t, err) && assert.NoError(t, connect.Ping()) {
10251055
{

rows.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ func (rows *rows) Close() error {
145145
rows.columns = nil
146146
for range rows.stream {
147147
}
148-
if rows.finish != nil {
149-
rows.finish()
150-
}
148+
rows.finish()
151149
return nil
152150
}
153151

stmt.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,12 @@ func (stmt *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (d
7777
func (stmt *stmt) queryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
7878
finish := stmt.ch.watchCancel(ctx)
7979
if err := stmt.ch.sendQuery(stmt.bind(args)); err != nil {
80-
if finish != nil {
81-
finish()
82-
}
80+
finish()
8381
return nil, err
8482
}
8583
meta, err := stmt.ch.readMeta()
8684
if err != nil {
87-
if finish != nil {
88-
finish()
89-
}
85+
finish()
9086
return nil, err
9187
}
9288
rows := rows{

0 commit comments

Comments
 (0)