forked from kenn-io/agentsview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattach_timeout_test.go
More file actions
212 lines (199 loc) · 5.54 KB
/
Copy pathattach_timeout_test.go
File metadata and controls
212 lines (199 loc) · 5.54 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
package duckdb
import (
"errors"
"net"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestResolveAttachTimeout(t *testing.T) {
tests := []struct {
name string
configured time.Duration
want time.Duration
}{
{"zero selects default", 0, DefaultAttachTimeout},
{"negative disables", -1, 0},
{"negative duration disables", -5 * time.Second, 0},
{"positive used as-is", 3 * time.Second, 3 * time.Second},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, resolveAttachTimeout(tt.configured))
})
}
}
func TestQuackDialAddress(t *testing.T) {
tests := []struct {
name string
rawURL string
want string
wantOK bool
}{
{"native host:port", "quack:127.0.0.1:9494", "127.0.0.1:9494", true},
{"native localhost", "quack:localhost:8765", "localhost:8765", true},
{
"native with userinfo",
"quack:user:pass@192.0.2.5:9494",
"192.0.2.5:9494",
true,
},
{
"native with query and fragment",
"quack:127.0.0.1:9494?foo=bar#frag",
"127.0.0.1:9494",
true,
},
{"native ipv6", "quack:[::1]:9494", "[::1]:9494", true},
{"native missing port", "quack:example.com", "", false},
{
"http default port",
"quack:http://example.com/db",
"example.com:80",
true,
},
{
"https default port",
"quack:https://example.com/db",
"example.com:443",
true,
},
{
"http explicit port",
"quack:http://example.com:9000/db",
"example.com:9000",
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, ok := quackDialAddress(tt.rawURL)
assert.Equal(t, tt.wantOK, ok)
if tt.wantOK {
assert.Equal(t, tt.want, got)
}
})
}
}
// closedLoopbackPort returns a loopback host:port that nothing is listening on
// by opening and immediately closing a listener.
func closedLoopbackPort(t *testing.T) string {
t.Helper()
ln, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err, "allocate probe listener")
addr := ln.Addr().String()
require.NoError(t, ln.Close(), "close probe listener")
return addr
}
// unresponsiveListener accepts connections and holds them open without ever
// writing a byte, simulating a server stuck in (for example) an SSL handshake.
func unresponsiveListener(t *testing.T) net.Listener {
t.Helper()
ln, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err, "start unresponsive listener")
var (
mu sync.Mutex
conns []net.Conn
)
go func() {
for {
conn, acceptErr := ln.Accept()
if acceptErr != nil {
return
}
// Never respond; just keep the connection open.
mu.Lock()
conns = append(conns, conn)
mu.Unlock()
}
}()
t.Cleanup(func() {
_ = ln.Close()
mu.Lock()
defer mu.Unlock()
for _, c := range conns {
_ = c.Close()
}
})
return ln
}
func TestPreflightQuackDial(t *testing.T) {
t.Run("closed port fails fast", func(t *testing.T) {
addr := closedLoopbackPort(t)
start := time.Now()
err := preflightQuackDial("quack:"+addr, time.Second)
require.Error(t, err)
assert.Less(t, time.Since(start), time.Second)
assert.Contains(t, err.Error(), "connecting to quack endpoint")
})
t.Run("listening port passes", func(t *testing.T) {
ln := unresponsiveListener(t)
err := preflightQuackDial("quack:"+ln.Addr().String(), time.Second)
assert.NoError(t, err)
})
t.Run("undeterminable address skips check", func(t *testing.T) {
// No port: no dial target can be derived, so preflight is a no-op.
assert.NoError(t, preflightQuackDial("quack:example.com", time.Second))
})
}
func TestRunWithAttachTimeout(t *testing.T) {
t.Run("times out on unresponsive server", func(t *testing.T) {
ln := unresponsiveListener(t)
timeout := 150 * time.Millisecond
start := time.Now()
err := runWithAttachTimeout(
"quack:"+ln.Addr().String(), timeout, func() error {
// Faithfully reproduce the hang: connect (accepted) then
// block on a read that never returns because the server
// never responds.
conn, dialErr := net.Dial("tcp", ln.Addr().String())
if dialErr != nil {
return dialErr
}
defer conn.Close()
buf := make([]byte, 1)
_, readErr := conn.Read(buf)
return readErr
},
)
elapsed := time.Since(start)
require.Error(t, err)
assert.Contains(t, err.Error(), "timed out")
assert.Contains(t, err.Error(), "attach_timeout")
assert.Less(t, elapsed, time.Second, "should give up near the timeout")
assert.GreaterOrEqual(t, elapsed, timeout)
})
t.Run("returns attach result when it completes", func(t *testing.T) {
assert.NoError(t, runWithAttachTimeout("quack:host", time.Second, func() error {
return nil
}))
sentinel := errors.New("attach boom")
err := runWithAttachTimeout("quack:host", time.Second, func() error {
return sentinel
})
assert.ErrorIs(t, err, sentinel)
})
t.Run("disabled timeout runs inline", func(t *testing.T) {
sentinel := errors.New("inline boom")
err := runWithAttachTimeout("quack:host", 0, func() error {
return sentinel
})
assert.ErrorIs(t, err, sentinel)
})
}
func TestOpenQuackClientPreflightFailsFast(t *testing.T) {
// A closed loopback port makes the TCP preflight fail before the quack
// extension is even loaded, so an unreachable endpoint returns promptly
// instead of hanging.
addr := closedLoopbackPort(t)
start := time.Now()
client, err := openQuackClient(
"quack:"+addr, "token", false, 2*time.Second,
)
require.Error(t, err)
assert.Nil(t, client)
assert.Less(t, time.Since(start), 2*time.Second)
assert.Contains(t, err.Error(), "connecting to quack endpoint")
}