Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit ba3d347

Browse files
committed
Use t.Cleanup in ws test server
Resolves #166 (comment)
1 parent 693d6c0 commit ba3d347

File tree

3 files changed

+12
-19
lines changed

3 files changed

+12
-19
lines changed

common/connection_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import (
3939

4040
func TestConnection(t *testing.T) {
4141
server := ws.NewServerWithEcho(t)
42-
defer server.Cleanup()
4342

4443
t.Run("connect", func(t *testing.T) {
4544
ctx := context.Background()
@@ -54,7 +53,6 @@ func TestConnection(t *testing.T) {
5453

5554
func TestConnectionClosureAbnormal(t *testing.T) {
5655
server := ws.NewServerWithClosureAbnormal(t)
57-
defer server.Cleanup()
5856

5957
t.Run("closure abnormal", func(t *testing.T) {
6058
ctx := context.Background()
@@ -72,7 +70,6 @@ func TestConnectionClosureAbnormal(t *testing.T) {
7270

7371
func TestConnectionSendRecv(t *testing.T) {
7472
server := ws.NewServerWithCDPHandler(t, ws.CDPDefaultHandler, nil)
75-
defer server.Cleanup()
7673

7774
t.Run("send command with empty reply", func(t *testing.T) {
7875
ctx := context.Background()
@@ -136,7 +133,6 @@ func TestConnectionCreateSession(t *testing.T) {
136133
}
137134

138135
server := ws.NewServerWithCDPHandler(t, handler, &cmdsReceived)
139-
defer server.Cleanup()
140136

141137
t.Run("create session for target", func(t *testing.T) {
142138
ctx := context.Background()

common/session_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ func TestSessionCreateSession(t *testing.T) {
7373
}
7474

7575
server := ws.NewServerWithCDPHandler(t, handler, &cmdsReceived)
76-
defer server.Cleanup()
7776

7877
t.Run("send and recv session commands", func(t *testing.T) {
7978
ctx := context.Background()

tests/ws/server.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ type Server struct {
8686
Dialer *k6netext.Dialer
8787
HTTPTransport *http.Transport
8888
Context context.Context
89-
Cleanup func()
9089
}
9190

9291
// NewServerWithClosureAbnormal creates a WS test server with abnormal closure behavior
@@ -109,13 +108,12 @@ func NewServer(t testing.TB, path string, handler http.Handler) *Server {
109108
mux.Handle("/", httpbin.New().Handler())
110109

111110
// Initialize the HTTP server and get its details
112-
httpSrv := httptest.NewServer(mux)
113-
httpURL, err := url.Parse(httpSrv.URL)
111+
server := httptest.NewServer(mux)
112+
url, err := url.Parse(server.URL)
114113
require.NoError(t, err)
115-
httpIP := net.ParseIP(httpURL.Hostname())
116-
require.NotNil(t, httpIP)
117-
118-
httpDomainValue, err := k6lib.NewHostAddress(httpIP, "")
114+
ip := net.ParseIP(url.Hostname())
115+
require.NotNil(t, ip)
116+
domain, err := k6lib.NewHostAddress(ip, "")
119117
require.NoError(t, err)
120118

121119
// Set up the dialer with shorter timeouts and the custom domains
@@ -125,7 +123,7 @@ func NewServer(t testing.TB, path string, handler http.Handler) *Server {
125123
DualStack: true,
126124
}, k6netext.NewResolver(net.LookupIP, 0, k6types.DNSfirst, k6types.DNSpreferIPv4))
127125
dialer.Hosts = map[string]*k6lib.HostAddress{
128-
WebSocketServerURL: httpDomainValue,
126+
WebSocketServerURL: domain,
129127
}
130128

131129
// Pre-configure the HTTP client transport with the dialer and TLS config (incl. HTTP2 support)
@@ -134,17 +132,17 @@ func NewServer(t testing.TB, path string, handler http.Handler) *Server {
134132
}
135133
require.NoError(t, http2.ConfigureTransport(transport))
136134

137-
ctx, ctxCancel := context.WithCancel(context.Background())
135+
ctx, cancel := context.WithCancel(context.Background())
136+
t.Cleanup(func() {
137+
server.Close()
138+
cancel()
139+
})
138140
return &Server{
139141
Mux: mux,
140-
ServerHTTP: httpSrv,
142+
ServerHTTP: server,
141143
Dialer: dialer,
142144
HTTPTransport: transport,
143145
Context: ctx,
144-
Cleanup: func() {
145-
httpSrv.Close()
146-
ctxCancel()
147-
},
148146
}
149147
}
150148

0 commit comments

Comments
 (0)