-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_test.go
More file actions
255 lines (218 loc) · 5.86 KB
/
proxy_test.go
File metadata and controls
255 lines (218 loc) · 5.86 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
package pgmux
import (
"context"
"fmt"
"net"
"testing"
"time"
"github.com/jackc/pgproto3/v2"
)
// mockRouter implements Router for testing
type mockRouter struct {
routes map[string]*BackendConfig
}
func (m *mockRouter) Route(ctx context.Context, username string) (*BackendConfig, error) {
if config, ok := m.routes[username]; ok {
return config, nil
}
return nil, ErrUserNotFound
}
func TestProxyServerStart(t *testing.T) {
router := &mockRouter{
routes: map[string]*BackendConfig{
"test_user": {
Host: "localhost",
Port: 5432,
User: "postgres",
},
},
}
proxy := NewProxyServer("127.0.0.1:0", router) // Use port 0 for random port
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Start proxy in background
errChan := make(chan error, 1)
go func() {
errChan <- proxy.Start(ctx)
}()
// Give proxy time to start
time.Sleep(100 * time.Millisecond)
// Try to connect
conn, err := net.Dial("tcp", proxy.listenAddr)
if err == nil {
conn.Close()
t.Error("Expected connection to fail without proper PostgreSQL handshake")
}
// Shutdown
cancel()
select {
case err := <-errChan:
if err != nil {
t.Errorf("Start() returned error: %v", err)
}
case <-time.After(1 * time.Second):
t.Error("Proxy didn't shut down in time")
}
}
func TestProxyServerInvalidPort(t *testing.T) {
router := &mockRouter{}
proxy := NewProxyServer("invalid:port", router)
ctx := context.Background()
err := proxy.Start(ctx)
if err == nil {
t.Error("Expected error for invalid listen address")
}
}
func TestProxyServerMaxConnectionsDefault(t *testing.T) {
router := &mockRouter{}
p := NewProxyServer("127.0.0.1:0", router)
if got := p.maxConnections(); got != defaultMaxConnections {
t.Errorf("unconfigured: expected %d, got %d", defaultMaxConnections, got)
}
p.WithLimits(&Limits{MaxConnections: 0})
if got := p.maxConnections(); got != defaultMaxConnections {
t.Errorf("zero: expected %d, got %d", defaultMaxConnections, got)
}
p.WithLimits(&Limits{MaxConnections: -5})
if got := p.maxConnections(); got != defaultMaxConnections {
t.Errorf("negative: expected %d, got %d", defaultMaxConnections, got)
}
p.WithLimits(&Limits{MaxConnections: 50})
if got := p.maxConnections(); got != 50 {
t.Errorf("custom: expected 50, got %d", got)
}
}
func TestProxyServerMaxConnections(t *testing.T) {
addr := pickFreePort(t)
router := &mockRouter{routes: map[string]*BackendConfig{}}
proxy := NewProxyServer(addr, router).WithLimits(&Limits{MaxConnections: 2})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
errChan := make(chan error, 1)
go func() { errChan <- proxy.Start(ctx) }()
conn1 := dialUntilReady(t, addr)
defer conn1.Close()
conn2, err := net.Dial("tcp", addr)
if err != nil {
t.Fatalf("conn2 dial: %v", err)
}
defer conn2.Close()
// Let both handlers reach ReceiveStartupMessage so the semaphore is full.
time.Sleep(100 * time.Millisecond)
// Third connection should be rejected with SQLSTATE 53300.
conn3, err := net.Dial("tcp", addr)
if err != nil {
t.Fatalf("conn3 dial: %v", err)
}
defer conn3.Close()
assertRejectedOverCapacity(t, conn3)
// Free a slot and confirm a new connection is accepted.
conn1.Close()
time.Sleep(100 * time.Millisecond)
conn4, err := net.Dial("tcp", addr)
if err != nil {
t.Fatalf("conn4 dial: %v", err)
}
defer conn4.Close()
assertNotRejected(t, conn4)
cancel()
select {
case <-errChan:
case <-time.After(2 * time.Second):
t.Error("proxy didn't shut down in time")
}
}
func pickFreePort(t *testing.T) string {
t.Helper()
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("pick free port: %v", err)
}
addr := l.Addr().String()
l.Close()
return addr
}
func dialUntilReady(t *testing.T, addr string) net.Conn {
t.Helper()
deadline := time.Now().Add(2 * time.Second)
for {
conn, err := net.Dial("tcp", addr)
if err == nil {
return conn
}
if time.Now().After(deadline) {
t.Fatalf("proxy never became reachable at %s: %v", addr, err)
}
time.Sleep(10 * time.Millisecond)
}
}
func assertRejectedOverCapacity(t *testing.T, conn net.Conn) {
t.Helper()
conn.SetReadDeadline(time.Now().Add(2 * time.Second))
frontend := pgproto3.NewFrontend(pgproto3.NewChunkReader(conn), conn)
msg, err := frontend.Receive()
if err != nil {
t.Fatalf("expected ErrorResponse, got receive error: %v", err)
}
errResp, ok := msg.(*pgproto3.ErrorResponse)
if !ok {
t.Fatalf("expected *pgproto3.ErrorResponse, got %T", msg)
}
if errResp.Code != "53300" {
t.Errorf("expected SQLSTATE 53300, got %q (message: %q)", errResp.Code, errResp.Message)
}
}
func assertNotRejected(t *testing.T, conn net.Conn) {
t.Helper()
// The handler is blocked on ReceiveStartupMessage, so no bytes should arrive.
// A read timeout is the success case; any 53300 ErrorResponse is a failure.
conn.SetReadDeadline(time.Now().Add(300 * time.Millisecond))
frontend := pgproto3.NewFrontend(pgproto3.NewChunkReader(conn), conn)
msg, err := frontend.Receive()
if err != nil {
return // expected: read timeout
}
if errResp, ok := msg.(*pgproto3.ErrorResponse); ok && errResp.Code == "53300" {
t.Errorf("expected connection to be accepted, got SQLSTATE 53300")
}
}
func TestIsConnectionClosed(t *testing.T) {
tests := []struct {
name string
err error
expected bool
}{
{
name: "nil error",
err: nil,
expected: false,
},
{
name: "regular error",
err: fmt.Errorf("some error"),
expected: false,
},
{
name: "read op error",
err: &net.OpError{
Op: "read",
},
expected: true,
},
{
name: "write op error",
err: &net.OpError{
Op: "write",
},
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isConnectionClosed(tt.err)
if result != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, result)
}
})
}
}