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
|
package http_test
import (
"context"
"io"
"net"
"net/http"
"strings"
"testing"
"time"
httpServer "go.sudomsg.com/kit/http"
)
// helper to find free TCP ports
func newListener(tb testing.TB) net.Listener {
tb.Helper()
ln, err := net.Listen("tcp", "localhost:0")
if err != nil {
tb.Fatalf("failed to open listener: %v", err)
}
return ln
}
func TestOpenConfigListeners(t *testing.T) {
t.Parallel()
t.Run("successful config", func(t *testing.T) {
t.Parallel()
ctx := t.Context()
cfg := []httpServer.ServerConfig{
{Network: "tcp", Address: "localhost:0"},
{Network: "tcp", Address: "localhost:0"},
}
lns, err := httpServer.OpenConfigListners(ctx, cfg)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer lns.CloseAll()
if got := len(lns); got != len(cfg) {
t.Errorf("expected %d listeners, got %d", len(cfg), got)
}
})
t.Run("port conflict triggers cleanup", func(t *testing.T) {
t.Parallel()
ctx := t.Context()
conflict := newListener(t)
defer conflict.Close()
cfg := []httpServer.ServerConfig{
{Network: "tcp", Address: "localhost:0"},
{Network: "tcp", Address: conflict.Addr().String()}, // will fail
}
lns, err := httpServer.OpenConfigListners(ctx, cfg)
if err == nil {
defer lns.CloseAll()
t.Fatal("expected error due to conflict, got nil")
}
})
}
func TestCloseAll(t *testing.T) {
t.Run("closes all listeners", func(t *testing.T) {
t.Parallel()
ln1 := newListener(t)
ln2 := newListener(t)
ls := httpServer.Listeners{ln1, ln2}
err := ls.CloseAll()
if err != nil {
t.Errorf("unexpected error from CloseAll: %v", err)
}
for _, ln := range ls {
if _, err := ln.Accept(); err == nil {
t.Error("expected listener to be closed, but Accept succeeded")
}
}
})
}
func TestRunHTTPServers(t *testing.T) {
t.Parallel()
t.Run("basic serve and shutdown", func(t *testing.T) {
t.Parallel()
ctx := t.Context()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "hello")
})
ln := newListener(t)
addr := ln.Addr().String()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
// Wait for server to be ready
time.Sleep(200 * time.Millisecond)
r, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://"+addr, nil)
resp, err := http.DefaultClient.Do(r)
if err != nil {
t.Errorf("http.Do error: %v", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if got := strings.TrimSpace(string(body)); got != "hello" {
t.Errorf("unexpected response body: %q", got)
}
cancel() // shutdown the server
}()
err := httpServer.RunHTTPServers(ctx, httpServer.Listeners{ln}, handler)
if err != nil {
t.Fatalf("RunHTTPServers failed: %v", err)
}
})
}
func TestInvalidConfig(t *testing.T) {
t.Parallel()
tests := []struct {
name string
config httpServer.ServerConfig
}{
{
name: "invalid network",
config: httpServer.ServerConfig{Network: "invalid", Address: "localhost:0"},
},
{
name: "invalid address",
config: httpServer.ServerConfig{Network: "tcp", Address: "::::"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ctx := t.Context()
_, err := httpServer.OpenConfigListners(ctx, []httpServer.ServerConfig{tt.config})
if err == nil {
t.Fatal("OpenConfigListners() expected error, got nil")
}
})
}
}
|