mirror of
https://github.com/coder/coder.git
synced 2025-07-21 01:28:49 +00:00
fix: Fix cleanup in test helpers, prefer defer
in tests (#3113)
* fix: Change uses of t.Cleanup -> defer in test bodies Mixing t.Cleanup and defer can lead to unexpected order of execution. * fix: Ensure t.Cleanup is not aborted by require * chore: Add helper annotations
This commit is contained in:
committed by
GitHub
parent
c2cd51d8b8
commit
6916d34458
@ -40,9 +40,9 @@ func TestCache(t *testing.T) {
|
||||
cache := wsconncache.New(func(r *http.Request, id uuid.UUID) (*agent.Conn, error) {
|
||||
return setupAgent(t, agent.Metadata{}, 0), nil
|
||||
}, 0)
|
||||
t.Cleanup(func() {
|
||||
defer func() {
|
||||
_ = cache.Close()
|
||||
})
|
||||
}()
|
||||
conn1, _, err := cache.Acquire(httptest.NewRequest(http.MethodGet, "/", nil), uuid.Nil)
|
||||
require.NoError(t, err)
|
||||
conn2, _, err := cache.Acquire(httptest.NewRequest(http.MethodGet, "/", nil), uuid.Nil)
|
||||
@ -56,9 +56,9 @@ func TestCache(t *testing.T) {
|
||||
called.Add(1)
|
||||
return setupAgent(t, agent.Metadata{}, 0), nil
|
||||
}, time.Microsecond)
|
||||
t.Cleanup(func() {
|
||||
defer func() {
|
||||
_ = cache.Close()
|
||||
})
|
||||
}()
|
||||
conn, release, err := cache.Acquire(httptest.NewRequest(http.MethodGet, "/", nil), uuid.Nil)
|
||||
require.NoError(t, err)
|
||||
release()
|
||||
@ -74,9 +74,9 @@ func TestCache(t *testing.T) {
|
||||
cache := wsconncache.New(func(r *http.Request, id uuid.UUID) (*agent.Conn, error) {
|
||||
return setupAgent(t, agent.Metadata{}, 0), nil
|
||||
}, time.Microsecond)
|
||||
t.Cleanup(func() {
|
||||
defer func() {
|
||||
_ = cache.Close()
|
||||
})
|
||||
}()
|
||||
conn, release, err := cache.Acquire(httptest.NewRequest(http.MethodGet, "/", nil), uuid.Nil)
|
||||
require.NoError(t, err)
|
||||
time.Sleep(time.Millisecond)
|
||||
@ -87,9 +87,9 @@ func TestCache(t *testing.T) {
|
||||
t.Parallel()
|
||||
random, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
defer func() {
|
||||
_ = random.Close()
|
||||
})
|
||||
}()
|
||||
tcpAddr, valid := random.Addr().(*net.TCPAddr)
|
||||
require.True(t, valid)
|
||||
|
||||
@ -98,17 +98,17 @@ func TestCache(t *testing.T) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}),
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
defer func() {
|
||||
_ = server.Close()
|
||||
})
|
||||
}()
|
||||
go server.Serve(random)
|
||||
|
||||
cache := wsconncache.New(func(r *http.Request, id uuid.UUID) (*agent.Conn, error) {
|
||||
return setupAgent(t, agent.Metadata{}, 0), nil
|
||||
}, time.Microsecond)
|
||||
t.Cleanup(func() {
|
||||
defer func() {
|
||||
_ = cache.Close()
|
||||
})
|
||||
}()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
// Perform many requests in parallel to simulate
|
||||
@ -132,7 +132,7 @@ func TestCache(t *testing.T) {
|
||||
res := httptest.NewRecorder()
|
||||
proxy.ServeHTTP(res, req)
|
||||
res.Result().Body.Close()
|
||||
require.Equal(t, http.StatusOK, res.Result().StatusCode)
|
||||
assert.Equal(t, http.StatusOK, res.Result().StatusCode)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
Reference in New Issue
Block a user