8000 plumbing: transport, Ensure goroutines are finished in test · go-git/go-git@9fbc38a · GitHub
[go: up one dir, main page]

Skip to content

Commit 9fbc38a

Browse files
committed
plumbing: transport, Ensure goroutines are finished in test
1 parent 7a5e048 commit 9fbc38a

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

plumbing/transport/http/common_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,17 @@ func setupServer(t testing.TB, smart bool) (base string, port int) {
253253
}
254254
}
255255

256-
t.Cleanup(func() { require.NoError(t, server.Close()) })
256+
done := make(chan struct{})
257257

258258
go func() {
259+
defer func() { close(done) }()
259260
require.ErrorIs(t, server.Serve(l), http.ErrServerClosed)
260261
}()
261262

263+
t.Cleanup(func() {
264+
require.NoError(t, server.Close())
265+
<-done
266+
})
267+
262268
return
263269
}

plumbing/transport/http/proxy_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ func (s *ProxySuite) TestAdvertisedReferencesHTTP() {
4141

4242
setupHTTPProxy(proxy, &proxiedRequests)
4343

44-
httpProxyAddr, proxyServer := setupProxyServer(s.T(), proxy, false, true)
45-
defer proxyServer.Close()
44+
httpProxyAddr := setupProxyServer(s.T(), proxy, false, true)
4645

4746
base, port := setupServer(s.T(), true)
4847

@@ -78,8 +77,7 @@ func (s *ProxySuite) TestAdvertisedReferencesHTTPS() {
7877
proxy := goproxy.NewProxyHttpServer()
7978
setupHTTPSProxy(proxy, &proxiedRequests)
8079

81-
httpsProxyAddr, tlsProxyServer := setupProxyServer(s.T(), proxy, true, true)
82-
defer tlsProxyServer.Close()
80+ httpsProxyAddr := setupProxyServer(s.T(), proxy, true, true)
8381

8482
endpoint, err := transport.NewEndpoint("https://github.com/git-fixtures/basic.git")
8583
s.Require().NoError(err)
@@ -113,7 +111,7 @@ func (s *ProxySuite) TestAdvertisedReferencesHTTPS() {
113111
var certs embed.FS
114112

115113
// Make sure you close the server after the test.
116-
func setupProxyServer(t testing.TB, handler http.Handler, isTls, schemaAddr bool) (string, *http.Server) {
114+
func setupProxyServer(t testing.TB, handler http.Handler, isTls, schemaAddr bool) string {
117115
schema := "http"
118116
if isTls {
119117
schema = "https"
@@ -157,7 +155,10 @@ func setupProxyServer(t testing.TB, handler http.Handler, isTls, schemaAddr bool
157155
proxyServer.TLSConfig = cfg
158156
}
159157

158+
done := make(chan struct{})
159+
160160
go func() {
161+
defer func() { close(done) }()
161162
var err error
162163
if isTls {
163164
err = proxyServer.ServeTLS(httpListener, "", "")
@@ -168,7 +169,12 @@ func setupProxyServer(t testing.TB, handler http.Handler, isTls, schemaAddr bool
168169
require.ErrorIs(t, err, http.ErrServerClosed)
169170
}()
170171

171-
return httpProxyAddr, &proxyServer
172+
t.Cleanup(func() {
173+
require.NoError(t, proxyServer.Close())
174+
<-done
175+
})
176+
177+
return httpProxyAddr
172178
}
173179

174180
func setupHTTPProxy(proxy *goproxy.ProxyHttpServer, proxiedRequests *int32) {

plumbing/transport/ssh/proxy_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,17 @@ func (s *ProxySuite) TestCommand() {
4444
})
4545
s.Require().NoError(err)
4646

47+
done := make(chan struct{})
4748
go func() {
49+
defer func() { close(done) }()
4850
s.Require().ErrorIs(socksServer.Serve(socksListener), net.ErrClosed)
4951
}()
5052

53+
defer func() {
54+
s.Require().NoError(socksListener.Close())
55+
<-done
56+
}()
57+
5158
socksProxyAddr := fmt.Sprintf("socks5://localhost:%d", socksListener.Addr().(*net.TCPAddr).Port)
5259

5360
base, port, _ := setupTest(s.T())

plumbing/transport/ssh/upload_pack_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,20 @@ func startServer(t testing.TB, opts ...ssh.Option) *net.TCPAddr {
8787
opt(server)
8888
}
8989

90+
done := make(chan struct{})
91+
9092
go func() {
91-
require.ErrorIs(t, server.Serve(l), ssh.ErrServerClosed)
93+
defer func() { close(done) }()
94+
require.ErrorIs(t, server.Serve(l), net.ErrClosed)
9295
}()
9396

94-
t.Cleanup(func() { require.NoError(t, server.Close()) })
97+
t.Cleanup(func() {
98+
// server.Serve(l) tracks the given listener, and server.Close() closes all tracked listeners.
99+
// If the test finishes too early and calls server.Close() before the listener is tracked,
100+
// server.Serve() may hang. Therefore, we should close the listener directly.
101+
require.NoError(t, l.Close())
102+
<-done
103+
})
95104

96105
return l.Addr().(*net.TCPAddr)
97106
}

0 commit comments

Comments
 (0)
0