Skip to content

Commit bca33c4

Browse files
committed
test: unify deadline error checking into testing.AssertIsTimeoutError
1 parent 8d0d78e commit bca33c4

File tree

4 files changed

+46
-31
lines changed

4 files changed

+46
-31
lines changed

conn_test.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package clickhouse
22

33
import (
44
"context"
5-
"errors"
65
"log/slog"
76
"os"
87
"testing"
@@ -118,20 +117,6 @@ func TestConn_Query_ReadTimeout(t *testing.T) {
118117

119118
t.Run("second row timeout", func(t *testing.T) {
120119
assert.False(t, rows.Next())
121-
err := rows.Err()
122-
assert.True(t, isDeadlineExceededError(err), "error is not a timeout error: %#v", err)
120+
chtesting.AssertIsDeadlineError(t, rows.Err())
123121
})
124122
}
125-
126-
type timeout interface {
127-
Timeout() bool
128-
}
129-
130-
func isDeadlineExceededError(err error) bool {
131-
nerr, ok := errors.Unwrap(err).(timeout)
132-
if !ok {
133-
return false
134-
}
135-
136-
return nerr.Timeout()
137-
}

lib/testing/errors.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package testing
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// AssertIsTimeoutError ensures that the error provided is a timeout error
11+
// It recursively unwraps the provided error and ensures the core error
12+
// implements the Timeout() method and it returns true.
13+
// context deadline error, os deadline error and and poll deadline error each
14+
// implement this and return true.
15+
func AssertIsTimeoutError(t *testing.T, err error) {
16+
assert.True(t, isDeadlineExceededError(err), "error is not a timeout error: %#v", err)
17+
}
18+
19+
type timeout interface {
20+
Timeout() bool
21+
}
22+
23+
func isDeadlineExceededError(err error) bool {
24+
nerr, ok := unwrap(err).(timeout)
25+
if !ok {
26+
return false
27+
}
28+
29+
return nerr.Timeout()
30+
}
31+
32+
// unwrap recursively unwraps the error until it gets the core error
33+
func unwrap(err error) error {
34+
if uerr := errors.Unwrap(err); uerr != nil {
35+
return unwrap(uerr)
36+
}
37+
return err
38+
}

lib/testing/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
package proto
18+
package testing
1919

2020
import (
2121
"errors"

tests/std/context_timeout_test.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ package std
2020
import (
2121
"context"
2222
"fmt"
23-
"github.com/ClickHouse/clickhouse-go/v2"
24-
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
25-
"github.com/stretchr/testify/require"
26-
"net"
27-
"net/url"
2823
"strconv"
2924
"testing"
3025
"time"
3126

27+
"github.com/ClickHouse/clickhouse-go/v2"
28+
chtesting "github.com/ClickHouse/clickhouse-go/v2/lib/testing"
29+
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
30+
"github.com/stretchr/testify/require"
31+
3232
"github.com/stretchr/testify/assert"
3333
)
3434

@@ -46,14 +46,7 @@ func TestStdContextStdTimeout(t *testing.T) {
4646
if row := connect.QueryRowContext(ctx, "SELECT 1, sleep(3)"); assert.NotNil(t, row) {
4747
var a, b int
4848
if err := row.Scan(&a, &b); assert.Error(t, err) {
49-
switch err := err.(type) {
50-
case *net.OpError:
51-
assert.Equal(t, "read", err.Op)
52-
case *url.Error:
53-
assert.Equal(t, context.DeadlineExceeded, err.Err)
54-
default:
55-
assert.ErrorIs(t, err, context.DeadlineExceeded)
56-
}
49+
chtesting.AssertIsDeadlineError(t, err)
5750
}
5851
}
5952
}
@@ -67,7 +60,6 @@ func TestStdContextStdTimeout(t *testing.T) {
6760
}
6861
}
6962
}
70-
7163
})
7264
}
7365
}

0 commit comments

Comments
 (0)