Skip to content

Commit 4730c58

Browse files
authored
chore: Use standardized test timeouts and delays (#3291)
1 parent 3d0febd commit 4730c58

25 files changed

+198
-109
lines changed

.github/workflows/coder.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,11 @@ jobs:
269269
echo ::set-output name=cover::false
270270
fi
271271
set -x
272-
gotestsum --junitfile="gotests.xml" --packages="./..." -- -parallel=8 -timeout=5m -short -failfast $COVERAGE_FLAGS
272+
test_timeout=5m
273+
if [[ "${{ matrix.os }}" == windows* ]]; then
274+
test_timeout=10m
275+
fi
276+
gotestsum --junitfile="gotests.xml" --packages="./..." -- -parallel=8 -timeout=$test_timeout -short -failfast $COVERAGE_FLAGS
273277
274278
- name: Upload DataDog Trace
275279
if: github.actor != 'dependabot[bot]' && !github.event.pull_request.head.repo.fork

agent/agent_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/coder/coder/peerbroker/proto"
3939
"github.com/coder/coder/provisionersdk"
4040
"github.com/coder/coder/pty/ptytest"
41+
"github.com/coder/coder/testutil"
4142
)
4243

4344
func TestMain(m *testing.M) {
@@ -257,7 +258,7 @@ func TestAgent(t *testing.T) {
257258
}
258259
gotContent = string(content)
259260
return true
260-
}, 15*time.Second, 100*time.Millisecond)
261+
}, testutil.WaitMedium, testutil.IntervalMedium)
261262
require.Equal(t, content, strings.TrimSpace(gotContent))
262263
})
263264

agent/reaper/reaper_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/stretchr/testify/require"
1313

1414
"github.com/coder/coder/agent/reaper"
15+
"github.com/coder/coder/testutil"
1516
)
1617

1718
func TestReap(t *testing.T) {
@@ -52,10 +53,9 @@ func TestReap(t *testing.T) {
5253

5354
expectedPIDs := []int{cmd.Process.Pid, cmd2.Process.Pid}
5455

55-
deadline := time.NewTimer(time.Second * 5)
5656
for i := 0; i < len(expectedPIDs); i++ {
5757
select {
58-
case <-deadline.C:
58+
case <-time.After(testutil.WaitShort):
5959
t.Fatalf("Timed out waiting for process")
6060
case pid := <-pids:
6161
require.Contains(t, expectedPIDs, pid)

cli/cliui/prompt_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os"
88
"os/exec"
99
"testing"
10-
"time"
1110

1211
"github.com/spf13/cobra"
1312
"github.com/stretchr/testify/assert"
@@ -16,6 +15,7 @@ import (
1615
"github.com/coder/coder/cli/cliui"
1716
"github.com/coder/coder/pty"
1817
"github.com/coder/coder/pty/ptytest"
18+
"github.com/coder/coder/testutil"
1919
)
2020

2121
func TestPrompt(t *testing.T) {
@@ -61,7 +61,7 @@ func TestPrompt(t *testing.T) {
6161
// Copy all data written out to a buffer. When we close the ptty, we can
6262
// no longer read from the ptty.Output(), but we can read what was
6363
// written to the buffer.
64-
dataRead, doneReading := context.WithTimeout(context.Background(), time.Second*2)
64+
dataRead, doneReading := context.WithTimeout(context.Background(), testutil.WaitShort)
6565
go func() {
6666
// This will throw an error sometimes. The underlying ptty
6767
// has its own cleanup routines in t.Cleanup. Instead of
@@ -193,7 +193,7 @@ func TestPasswordTerminalState(t *testing.T) {
193193
require.Eventually(t, func() bool {
194194
echo, err := ptyWithFlags.EchoEnabled()
195195
return err == nil && !echo
196-
}, 5*time.Second, 50*time.Millisecond, "echo is on while reading password")
196+
}, testutil.WaitShort, testutil.IntervalMedium, "echo is on while reading password")
197197

198198
err = process.Signal(os.Interrupt)
199199
require.NoError(t, err)
@@ -203,7 +203,7 @@ func TestPasswordTerminalState(t *testing.T) {
203203
require.Eventually(t, func() bool {
204204
echo, err := ptyWithFlags.EchoEnabled()
205205
return err == nil && echo
206-
}, 5*time.Second, 50*time.Millisecond, "echo is off after reading password")
206+
}, testutil.WaitShort, testutil.IntervalMedium, "echo is off after reading password")
207207
}
208208

209209
// nolint:unused

cli/create_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/coder/coder/provisioner/echo"
1717
"github.com/coder/coder/provisionersdk/proto"
1818
"github.com/coder/coder/pty/ptytest"
19+
"github.com/coder/coder/testutil"
1920
)
2021

2122
func TestCreate(t *testing.T) {
@@ -88,7 +89,7 @@ func TestCreate(t *testing.T) {
8889

8990
member := coderdtest.CreateAnotherUser(t, client, user.OrganizationID)
9091
clitest.SetupConfig(t, member, root)
91-
cmdCtx, done := context.WithTimeout(context.Background(), 10*time.Second)
92+
cmdCtx, done := context.WithTimeout(context.Background(), testutil.WaitLong)
9293
go func() {
9394
defer done()
9495
err := cmd.ExecuteContext(cmdCtx)

cli/list_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package cli_test
33
import (
44
"context"
55
"testing"
6-
"time"
76

87
"github.com/stretchr/testify/assert"
98

109
"github.com/coder/coder/cli/clitest"
1110
"github.com/coder/coder/coderd/coderdtest"
1211
"github.com/coder/coder/pty/ptytest"
12+
"github.com/coder/coder/testutil"
1313
)
1414

1515
func TestList(t *testing.T) {
@@ -29,7 +29,7 @@ func TestList(t *testing.T) {
2929
cmd.SetIn(pty.Input())
3030
cmd.SetOut(pty.Output())
3131

32-
ctx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second)
32+
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
3333
defer cancelFunc()
3434
done := make(chan any)
3535
go func() {

cli/portforward_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/coder/coder/codersdk"
2424
"github.com/coder/coder/provisioner/echo"
2525
"github.com/coder/coder/provisionersdk/proto"
26+
"github.com/coder/coder/testutil"
2627
)
2728

2829
func TestPortForward(t *testing.T) {
@@ -170,7 +171,7 @@ func TestPortForward(t *testing.T) {
170171

171172
// Open two connections simultaneously and test them out of
172173
// sync.
173-
d := net.Dialer{Timeout: 3 * time.Second}
174+
d := net.Dialer{Timeout: testutil.WaitShort}
174175
c1, err := d.DialContext(ctx, c.network, localAddress)
175176
require.NoError(t, err, "open connection 1 to 'local' listener")
176177
defer c1.Close()
@@ -216,7 +217,7 @@ func TestPortForward(t *testing.T) {
216217

217218
// Open a connection to both listener 1 and 2 simultaneously and
218219
// then test them out of order.
219-
d := net.Dialer{Timeout: 3 * time.Second}
220+
d := net.Dialer{Timeout: testutil.WaitShort}
220221
c1, err := d.DialContext(ctx, c.network, localAddress1)
221222
require.NoError(t, err, "open connection 1 to 'local' listener 1")
222223
defer c1.Close()
@@ -269,7 +270,7 @@ func TestPortForward(t *testing.T) {
269270

270271
// Open two connections simultaneously and test them out of
271272
// sync.
272-
d := net.Dialer{Timeout: 3 * time.Second}
273+
d := net.Dialer{Timeout: testutil.WaitShort}
273274
c1, err := d.DialContext(ctx, tcpCase.network, localAddress)
274275
require.NoError(t, err, "open connection 1 to 'local' listener")
275276
defer c1.Close()
@@ -329,7 +330,7 @@ func TestPortForward(t *testing.T) {
329330

330331
// Open connections to all items in the "dial" array.
331332
var (
332-
d = net.Dialer{Timeout: 3 * time.Second}
333+
d = net.Dialer{Timeout: testutil.WaitShort}
333334
conns = make([]net.Conn, len(dials))
334335
)
335336
for i, a := range dials {
@@ -488,7 +489,7 @@ func assertWritePayload(t *testing.T, w io.Writer, payload []byte) {
488489
func waitForPortForwardReady(t *testing.T, output *threadSafeBuffer) {
489490
t.Helper()
490491
for i := 0; i < 100; i++ {
491-
time.Sleep(250 * time.Millisecond)
492+
time.Sleep(testutil.IntervalMedium)
492493

493494
data := output.String()
494495
if strings.Contains(data, "Ready!") {

cli/resetpassword_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"net/url"
66
"runtime"
77
"testing"
8-
"time"
98

109
"github.com/stretchr/testify/assert"
1110
"github.com/stretchr/testify/require"
@@ -14,6 +13,7 @@ import (
1413
"github.com/coder/coder/coderd/database/postgres"
1514
"github.com/coder/coder/codersdk"
1615
"github.com/coder/coder/pty/ptytest"
16+
"github.com/coder/coder/testutil"
1717
)
1818

1919
// nolint:paralleltest
@@ -53,7 +53,7 @@ func TestResetPassword(t *testing.T) {
5353
require.Eventually(t, func() bool {
5454
rawURL, err = cfg.URL().Read()
5555
return err == nil && rawURL != ""
56-
}, 15*time.Second, 25*time.Millisecond)
56+
}, testutil.WaitLong, testutil.IntervalFast)
5757
accessURL, err := url.Parse(rawURL)
5858
require.NoError(t, err)
5959
client := codersdk.New(accessURL)

cli/server_test.go

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ import (
2525
"go.uber.org/goleak"
2626

2727
"github.com/coder/coder/cli/clitest"
28+
"github.com/coder/coder/cli/config"
2829
"github.com/coder/coder/coderd/database/postgres"
2930
"github.com/coder/coder/coderd/telemetry"
3031
"github.com/coder/coder/codersdk"
3132
"github.com/coder/coder/pty/ptytest"
33+
"github.com/coder/coder/testutil"
3234
)
3335

3436
// This cannot be ran in parallel because it uses a signal.
@@ -55,13 +57,7 @@ func TestServer(t *testing.T) {
5557
go func() {
5658
errC <- root.ExecuteContext(ctx)
5759
}()
58-
var rawURL string
59-
require.Eventually(t, func() bool {
60-
rawURL, err = cfg.URL().Read()
61-
return err == nil && rawURL != ""
62-
}, time.Minute, 50*time.Millisecond)
63-
accessURL, err := url.Parse(rawURL)
64-
require.NoError(t, err)
60+
accessURL := waitAccessURL(t, cfg)
6561
client := codersdk.New(accessURL)
6662

6763
_, err = client.CreateFirstUser(ctx, codersdk.CreateFirstUserRequest{
@@ -94,10 +90,11 @@ func TestServer(t *testing.T) {
9490
go func() {
9591
errC <- root.ExecuteContext(ctx)
9692
}()
93+
//nolint:gocritic // Embedded postgres take a while to fire up.
9794
require.Eventually(t, func() bool {
98-
accessURLRaw, err := cfg.URL().Read()
99-
return accessURLRaw != "" && err == nil
100-
}, 3*time.Minute, 250*time.Millisecond)
95+
rawURL, err := cfg.URL().Read()
96+
return err == nil && rawURL != ""
97+
}, 3*time.Minute, testutil.IntervalFast, "failed to get access URL")
10198
cancelFunc()
10299
require.ErrorIs(t, <-errC, context.Canceled)
103100
})
@@ -135,11 +132,7 @@ func TestServer(t *testing.T) {
135132
}()
136133

137134
// Just wait for startup
138-
require.Eventually(t, func() bool {
139-
var err error
140-
_, err = cfg.URL().Read()
141-
return err == nil
142-
}, 15*time.Second, 25*time.Millisecond)
135+
_ = waitAccessURL(t, cfg)
143136

144137
cancelFunc()
145138
require.ErrorIs(t, <-errC, context.Canceled)
@@ -169,11 +162,7 @@ func TestServer(t *testing.T) {
169162
}()
170163

171164
// Just wait for startup
172-
require.Eventually(t, func() bool {
173-
var err error
174-
_, err = cfg.URL().Read()
175-
return err == nil
176-
}, 15*time.Second, 25*time.Millisecond)
165+
_ = waitAccessURL(t, cfg)
177166

178167
cancelFunc()
179168
require.ErrorIs(t, <-errC, context.Canceled)
@@ -201,11 +190,7 @@ func TestServer(t *testing.T) {
201190
}()
202191

203192
// Just wait for startup
204-
require.Eventually(t, func() bool {
205-
var err error
206-
_, err = cfg.URL().Read()
207-
return err == nil
208-
}, 15*time.Second, 25*time.Millisecond)
193+
_ = waitAccessURL(t, cfg)
209194

210195
cancelFunc()
211196
require.ErrorIs(t, <-errC, context.Canceled)
@@ -281,14 +266,7 @@ func TestServer(t *testing.T) {
281266
}()
282267

283268
// Verify HTTPS
284-
var accessURLRaw string
285-
require.Eventually(t, func() bool {
286-
var err error
287-
accessURLRaw, err = cfg.URL().Read()
288-
return accessURLRaw != "" && err == nil
289-
}, 15*time.Second, 25*time.Millisecond)
290-
accessURL, err := url.Parse(accessURLRaw)
291-
require.NoError(t, err)
269+
accessURL := waitAccessURL(t, cfg)
292270
require.Equal(t, "https", accessURL.Scheme)
293271
client := codersdk.New(accessURL)
294272
client.HTTPClient = &http.Client{
@@ -299,7 +277,7 @@ func TestServer(t *testing.T) {
299277
},
300278
},
301279
}
302-
_, err = client.HasFirstUser(ctx)
280+
_, err := client.HasFirstUser(ctx)
303281
require.NoError(t, err)
304282

305283
cancelFunc()
@@ -326,11 +304,7 @@ func TestServer(t *testing.T) {
326304
go func() {
327305
serverErr <- root.ExecuteContext(ctx)
328306
}()
329-
require.Eventually(t, func() bool {
330-
var err error
331-
_, err = cfg.URL().Read()
332-
return err == nil
333-
}, 15*time.Second, 25*time.Millisecond)
307+
_ = waitAccessURL(t, cfg)
334308
currentProcess, err := os.FindProcess(os.Getpid())
335309
require.NoError(t, err)
336310
err = currentProcess.Signal(os.Interrupt)
@@ -436,3 +410,19 @@ func generateTLSCertificate(t testing.TB) (certPath, keyPath string) {
436410
require.NoError(t, err)
437411
return certFile.Name(), keyFile.Name()
438412
}
413+
414+
func waitAccessURL(t *testing.T, cfg config.Root) *url.URL {
415+
t.Helper()
416+
417+
var err error
418+
var rawURL string
419+
require.Eventually(t, func() bool {
420+
rawURL, err = cfg.URL().Read()
421+
return err == nil && rawURL != ""
422+
}, testutil.WaitLong, testutil.IntervalFast, "failed to get access URL")
423+
424+
accessURL, err := url.Parse(rawURL)
425+
require.NoError(t, err, "failed to parse access URL")
426+
427+
return accessURL
428+
}

cli/ssh_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/coder/coder/provisioner/echo"
3030
"github.com/coder/coder/provisionersdk/proto"
3131
"github.com/coder/coder/pty/ptytest"
32+
"github.com/coder/coder/testutil"
3233
)
3334

3435
func setupWorkspaceForSSH(t *testing.T) (*codersdk.Client, codersdk.Workspace, string) {
@@ -77,7 +78,7 @@ func TestSSH(t *testing.T) {
7778
cmd.SetErr(pty.Output())
7879
cmd.SetOut(pty.Output())
7980

80-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
81+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
8182
defer cancel()
8283

8384
cmdDone := tGo(t, func() {
@@ -124,7 +125,7 @@ func TestSSH(t *testing.T) {
124125
}
125126
}()
126127

127-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
128+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
128129
defer cancel()
129130

130131
cmd, root := clitest.New(t, "ssh", "--stdio", workspace.Name)
@@ -215,7 +216,7 @@ func TestSSH(t *testing.T) {
215216
}
216217
})
217218

218-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
219+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
219220
defer cancel()
220221

221222
cmd, root := clitest.New(t,

0 commit comments

Comments
 (0)