-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathconnect_timeout_test.go
More file actions
268 lines (248 loc) · 10.5 KB
/
Copy pathconnect_timeout_test.go
File metadata and controls
268 lines (248 loc) · 10.5 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*+*****************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (c) 2014-2019 Appsicle
* Copyright (c) 2019-2026 QuestDB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package questdb
import (
"context"
"errors"
"net"
"net/http"
"strings"
"sync/atomic"
"testing"
"time"
)
// connect_timeout is a COMMON key: the parser accepts it on every schema so a
// shared connect string ports from the Java client. It is wired on HTTP and
// QWP; the TCP ILP dial leaves it inert (matching the Java client, where only
// HttpClient.connect / WebSocketClient.doConnect honour it).
func TestConnectTimeoutParsedOnAllSchemas(t *testing.T) {
for _, schema := range []string{"http", "https", "tcp", "tcps", "ws", "wss"} {
conf := schema + "::addr=localhost:9000;connect_timeout=2500;"
c, err := confFromStr(conf)
if err != nil {
t.Fatalf("%s: confFromStr: %v", schema, err)
}
if c.connectTimeoutMs != 2500 {
t.Errorf("%s: connectTimeoutMs=%d, want 2500", schema, c.connectTimeoutMs)
}
}
}
func TestConnectTimeoutRejectsNonPositive(t *testing.T) {
for _, v := range []string{"0", "-1", "abc", "5.5"} {
if _, err := confFromStr("ws::addr=a:9000;connect_timeout=" + v + ";"); err == nil {
t.Errorf("connect_timeout=%q: expected error, got nil", v)
}
}
}
func TestConnectTimeoutOptionIngest(t *testing.T) {
c := newLineSenderConfig(qwpSenderType)
WithConnectTimeout(750 * time.Millisecond)(c)
if c.connectTimeoutMs != 750 {
t.Errorf("connectTimeoutMs=%d, want 750", c.connectTimeoutMs)
}
}
func TestConnectTimeoutEgressConf(t *testing.T) {
c, err := parseQwpQueryConf("ws::addr=a:9000;connect_timeout=1200;")
if err != nil {
t.Fatalf("parseQwpQueryConf: %v", err)
}
if c.connectTimeoutMs != 1200 {
t.Errorf("connectTimeoutMs=%d, want 1200", c.connectTimeoutMs)
}
for _, v := range []string{"0", "-1", "abc"} {
if _, err := parseQwpQueryConf("ws::addr=a:9000;connect_timeout=" + v + ";"); err == nil {
t.Errorf("connect_timeout=%q: expected error, got nil", v)
}
}
}
func TestConnectTimeoutEgressOption(t *testing.T) {
c := qwpQueryDefaultConfig()
WithQwpQueryConnectTimeout(900 * time.Millisecond)(c)
if c.connectTimeoutMs != 900 {
t.Errorf("connectTimeoutMs=%d, want 900", c.connectTimeoutMs)
}
}
// blackholeAddr is TEST-NET-1 (RFC 5737): on a normal network the SYN is
// silently dropped, so a TCP connect stalls until our connect_timeout fires —
// long before the OS connect timeout (60-120s). When the runner has no route,
// the connect fast-fails instead and the test skips rather than flakes, mirroring
// Java's NetConnectTimeoutTest / QwpQueryClientConnectTimeoutTest.
const blackholeAddr = "192.0.2.1:9009"
// assertBoundedConnectFailure skips on a fast no-route fail (the connect_timeout
// path was not exercised) and otherwise asserts the dial bailed well inside the
// budget instead of riding the OS connect timeout.
func assertBoundedConnectFailure(t *testing.T, elapsed time.Duration, err error) {
t.Helper()
if err == nil {
t.Fatal("expected connect to a black-hole host to fail")
}
if elapsed < 200*time.Millisecond {
t.Skipf("TEST-NET-1 fast-failed (no black-hole route) in %v: %v", elapsed, err)
}
if elapsed > 5*time.Second {
t.Fatalf("connect_timeout not honored: bailed after %v (want < 5s): %v", elapsed, err)
}
}
func TestConnectTimeoutQwpIngestBlackhole(t *testing.T) {
// Default initial-connect mode is OFF: the constructor dials once and
// surfaces the failure, so connect_timeout bounds that single dial.
start := time.Now()
_, err := LineSenderFromConf(context.Background(),
"ws::addr="+blackholeAddr+";connect_timeout=500;")
assertBoundedConnectFailure(t, time.Since(start), err)
}
func TestConnectTimeoutQwpQueryBlackhole(t *testing.T) {
start := time.Now()
_, err := QwpQueryClientFromConf(context.Background(),
"ws::addr="+blackholeAddr+";connect_timeout=500;auth_timeout_ms=15000;failover=off;target=any;")
elapsed := time.Since(start)
assertBoundedConnectFailure(t, elapsed, err)
// A connect-phase timeout must read as a connect failure, never be relabeled
// as an auth_timeout overage (Java QwpQueryClientConnectTimeoutTest).
if strings.Contains(err.Error(), "auth_timeout") {
t.Fatalf("connect-phase timeout misreported as auth_timeout: %v", err)
}
}
// TestConnectTimeoutHttpUsesPrivateTransport covers the HTTP connect_timeout
// branch deterministically: when connect_timeout is set, the sender must build
// a private transport (carrying the net.Dialer timeout) rather than share the
// global one — the global dialer must never be mutated for a single sender.
// (protocol_version=2 skips the version-detect request, so this touches no
// network; the dialer-bounds-connect behavior itself is proven by the QWP
// black-hole tests, which share the same net.Dialer{Timeout} mechanism.)
func TestConnectTimeoutHttpUsesPrivateTransport(t *testing.T) {
c, err := confFromStr("http::addr=192.0.2.1:9009;protocol_version=2;connect_timeout=500;")
if err != nil {
t.Fatalf("conf: %v", err)
}
ls, err := newHttpLineSender(context.Background(), c)
if err != nil {
t.Fatalf("build: %v", err)
}
defer ls.Close(context.Background())
v2, ok := ls.(*httpLineSenderV2)
if !ok {
t.Fatalf("unexpected sender type %T", ls)
}
if v2.globalTransport != nil {
t.Error("a connect_timeout HTTP sender must use a private transport, not the shared global")
}
}
// TestConnectTimeoutHttpCustomTransportNotMutated covers the http_sender.go
// guard (`&& conf.httpTransport == nil`): a caller-supplied transport is owned by
// the caller, so connect_timeout must NOT overwrite its DialContext even when set
// — doing so would silently replace the caller's custom dialer. We prove the
// custom DialContext survives by invoking it after construction: the sentinel
// fires only if the field was left intact (a net.Dialer would have replaced it).
func TestConnectTimeoutHttpCustomTransportNotMutated(t *testing.T) {
var dialed atomic.Bool
sentinel := errors.New("sentinel dialer")
custom := newHttpTransport()
custom.DialContext = func(context.Context, string, string) (net.Conn, error) {
dialed.Store(true)
return nil, sentinel
}
// protocol_version=2 skips the version-detect request, so no network is
// touched at build time. httpTransport != nil + connect_timeout set is the
// guarded path.
c, err := confFromStr("http::addr=192.0.2.1:9009;protocol_version=2;connect_timeout=500;")
if err != nil {
t.Fatalf("conf: %v", err)
}
c.httpTransport = custom
ls, err := newHttpLineSender(context.Background(), c)
if err != nil {
t.Fatalf("build: %v", err)
}
defer ls.Close(context.Background())
v2, ok := ls.(*httpLineSenderV2)
if !ok {
t.Fatalf("unexpected sender type %T", ls)
}
tr, ok := v2.client.Transport.(*http.Transport)
if !ok {
t.Fatalf("transport is %T, want *http.Transport", v2.client.Transport)
}
if tr != custom {
t.Fatal("sender did not use the caller-supplied custom transport")
}
// The sender must not own (and later close) a caller-supplied transport.
if v2.ownedTransport != nil {
t.Error("a caller-supplied transport must not be adopted as ownedTransport")
}
// Invoke the transport's dialer: it must still be the caller's sentinel, not a
// connect_timeout net.Dialer that replaced it.
if _, err := tr.DialContext(context.Background(), "tcp", "127.0.0.1:9"); !errors.Is(err, sentinel) {
t.Errorf("DialContext err=%v, want sentinel — connect_timeout overwrote the custom dialer", err)
}
if !dialed.Load() {
t.Error("custom DialContext was not invoked — connect_timeout replaced it")
}
}
// TestConnectTimeoutAcceptedThroughFullConstruction pins that connect_timeout
// reaches a working sender through the whole construction path, not just the
// connect-string parser. It is a COMMON key: HTTP wires it to a private dialer,
// TCP leaves it inert, and neither may reject it. The guard is against
// re-adding connect_timeout to rejectQwpOnlyOptions, which makes the parser
// accept the key while sender construction rejects it — the contradiction the
// earlier build had, where the same conf string parsed but failed to build.
//
// The other HTTP connect_timeout tests build via newHttpLineSender directly,
// which skips the sanitizer where the bug lived; these go through the public
// entry points so the sanitizer runs.
func TestConnectTimeoutAcceptedThroughFullConstruction(t *testing.T) {
ctx := context.Background()
// HTTP end to end through the public conf-string API: parse, sanitize,
// build. protocol_version=2 skips the version-detect request so no network
// is touched. This is the exact form that previously parsed but failed to
// build with "connect_timeout is only available in the QWP client".
t.Run("http/conf-string", func(t *testing.T) {
s, err := LineSenderFromConf(ctx,
"http::addr=localhost:9000;protocol_version=2;connect_timeout=5000;")
if err != nil {
t.Fatalf("HTTP conf-string rejected connect_timeout: %v", err)
}
_ = s.Close(ctx)
})
// HTTP through the option path, mirroring TestQwpOnlyOptionsRejectedOnHttpAndTcp
// but asserting acceptance. WithProtocolVersion(2) skips the dial.
t.Run("http/option", func(t *testing.T) {
s, err := NewLineSender(ctx, WithHttp(), WithAddress("localhost:9000"),
WithProtocolVersion(2), WithConnectTimeout(5*time.Second))
if err != nil {
t.Fatalf("HTTP option path rejected connect_timeout: %v", err)
}
_ = s.Close(ctx)
})
// TCP tolerates the key (inert, matching the Java client). The sanitizer
// must accept it; assert that directly since full TCP construction dials.
t.Run("tcp/sanitizer", func(t *testing.T) {
conf := newLineSenderConfig(tcpSenderType)
WithAddress("localhost:9009")(conf)
WithConnectTimeout(5 * time.Second)(conf)
if err := sanitizeTcpConf(conf); err != nil {
t.Fatalf("TCP sanitizer rejected connect_timeout: %v", err)
}
})
}