Skip to content

Commit b232717

Browse files
committed
chore: Standardize waits and intervals between tests
1 parent 74c8766 commit b232717

18 files changed

+131
-86
lines changed

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/internal/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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/stretchr/testify/require"
1515

1616
"github.com/coder/coder/cli/cliui"
17+
"github.com/coder/coder/internal/testutil"
1718
"github.com/coder/coder/pty"
1819
"github.com/coder/coder/pty/ptytest"
1920
)
@@ -193,7 +194,7 @@ func TestPasswordTerminalState(t *testing.T) {
193194
require.Eventually(t, func() bool {
194195
echo, err := ptyWithFlags.EchoEnabled()
195196
return err == nil && !echo
196-
}, 5*time.Second, 50*time.Millisecond, "echo is on while reading password")
197+
}, testutil.WaitShort, testutil.IntervalMedium, "echo is on while reading password")
197198

198199
err = process.Signal(os.Interrupt)
199200
require.NoError(t, err)
@@ -203,7 +204,7 @@ func TestPasswordTerminalState(t *testing.T) {
203204
require.Eventually(t, func() bool {
204205
echo, err := ptyWithFlags.EchoEnabled()
205206
return err == nil && echo
206-
}, 5*time.Second, 50*time.Millisecond, "echo is off after reading password")
207+
}, testutil.WaitShort, testutil.IntervalMedium, "echo is off after reading password")
207208
}
208209

209210
// nolint:unused

cli/create_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/coder/coder/cli/clitest"
1414
"github.com/coder/coder/coderd/coderdtest"
1515
"github.com/coder/coder/codersdk"
16+
"github.com/coder/coder/internal/testutil"
1617
"github.com/coder/coder/provisioner/echo"
1718
"github.com/coder/coder/provisionersdk/proto"
1819
"github.com/coder/coder/pty/ptytest"
@@ -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/portforward_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/coder/coder/cli/clitest"
2222
"github.com/coder/coder/coderd/coderdtest"
2323
"github.com/coder/coder/codersdk"
24+
"github.com/coder/coder/internal/testutil"
2425
"github.com/coder/coder/provisioner/echo"
2526
"github.com/coder/coder/provisionersdk/proto"
2627
)
@@ -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,14 +5,14 @@ import (
55
"net/url"
66
"runtime"
77
"testing"
8-
"time"
98

109
"github.com/stretchr/testify/assert"
1110
"github.com/stretchr/testify/require"
1211

1312
"github.com/coder/coder/cli/clitest"
1413
"github.com/coder/coder/coderd/database/postgres"
1514
"github.com/coder/coder/codersdk"
15+
"github.com/coder/coder/internal/testutil"
1616
"github.com/coder/coder/pty/ptytest"
1717
)
1818

@@ -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: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ import (
2626
"go.uber.org/goleak"
2727

2828
"github.com/coder/coder/cli/clitest"
29+
"github.com/coder/coder/cli/config"
2930
"github.com/coder/coder/coderd/database/postgres"
3031
"github.com/coder/coder/coderd/telemetry"
3132
"github.com/coder/coder/codersdk"
33+
"github.com/coder/coder/internal/testutil"
3234
"github.com/coder/coder/pty/ptytest"
3335
)
3436

@@ -56,13 +58,7 @@ func TestServer(t *testing.T) {
5658
go func() {
5759
errC <- root.ExecuteContext(ctx)
5860
}()
59-
var rawURL string
60-
require.Eventually(t, func() bool {
61-
rawURL, err = cfg.URL().Read()
62-
return err == nil && rawURL != ""
63-
}, time.Minute, 50*time.Millisecond)
64-
accessURL, err := url.Parse(rawURL)
65-
require.NoError(t, err)
61+
accessURL := waitAccessURL(t, cfg)
6662
client := codersdk.New(accessURL)
6763

6864
_, err = client.CreateFirstUser(ctx, codersdk.CreateFirstUserRequest{
@@ -95,10 +91,7 @@ func TestServer(t *testing.T) {
9591
go func() {
9692
errC <- root.ExecuteContext(ctx)
9793
}()
98-
require.Eventually(t, func() bool {
99-
accessURLRaw, err := cfg.URL().Read()
100-
return accessURLRaw != "" && err == nil
101-
}, 3*time.Minute, 250*time.Millisecond)
94+
_ = waitAccessURL(t, cfg)
10295
cancelFunc()
10396
require.ErrorIs(t, <-errC, context.Canceled)
10497
})
@@ -133,11 +126,7 @@ func TestServer(t *testing.T) {
133126
}()
134127

135128
// Just wait for startup
136-
require.Eventually(t, func() bool {
137-
var err error
138-
_, err = cfg.URL().Read()
139-
return err == nil
140-
}, 15*time.Second, 25*time.Millisecond)
129+
_ = waitAccessURL(t, cfg)
141130

142131
cancelFunc()
143132
require.ErrorIs(t, <-errC, context.Canceled)
@@ -213,14 +202,7 @@ func TestServer(t *testing.T) {
213202
}()
214203

215204
// Verify HTTPS
216-
var accessURLRaw string
217-
require.Eventually(t, func() bool {
218-
var err error
219-
accessURLRaw, err = cfg.URL().Read()
220-
return accessURLRaw != "" && err == nil
221-
}, 15*time.Second, 25*time.Millisecond)
222-
accessURL, err := url.Parse(accessURLRaw)
223-
require.NoError(t, err)
205+
accessURL := waitAccessURL(t, cfg)
224206
require.Equal(t, "https", accessURL.Scheme)
225207
client := codersdk.New(accessURL)
226208
client.HTTPClient = &http.Client{
@@ -258,11 +240,7 @@ func TestServer(t *testing.T) {
258240
go func() {
259241
serverErr <- root.ExecuteContext(ctx)
260242
}()
261-
require.Eventually(t, func() bool {
262-
var err error
263-
_, err = cfg.URL().Read()
264-
return err == nil
265-
}, 15*time.Second, 25*time.Millisecond)
243+
_ = waitAccessURL(t, cfg)
266244
currentProcess, err := os.FindProcess(os.Getpid())
267245
require.NoError(t, err)
268246
err = currentProcess.Signal(os.Interrupt)
@@ -368,3 +346,17 @@ func generateTLSCertificate(t testing.TB) (certPath, keyPath string) {
368346
require.NoError(t, err)
369347
return certFile.Name(), keyFile.Name()
370348
}
349+
350+
func waitAccessURL(t *testing.T, cfg config.Root) *url.URL {
351+
var err error
352+
var rawURL string
353+
require.Eventually(t, func() bool {
354+
rawURL, err = cfg.URL().Read()
355+
return err == nil && rawURL != ""
356+
}, testutil.WaitLong, testutil.IntervalFast)
357+
358+
accessURL, err := url.Parse(rawURL)
359+
require.NoError(t, err)
360+
361+
return accessURL
362+
}

cli/ssh_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/coder/coder/cli/clitest"
2727
"github.com/coder/coder/coderd/coderdtest"
2828
"github.com/coder/coder/codersdk"
29+
"github.com/coder/coder/internal/testutil"
2930
"github.com/coder/coder/provisioner/echo"
3031
"github.com/coder/coder/provisionersdk/proto"
3132
"github.com/coder/coder/pty/ptytest"
@@ -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,

coderd/coderd_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/coderd/telemetry"
3939
"github.com/coder/coder/coderd/turnconn"
4040
"github.com/coder/coder/codersdk"
41+
"github.com/coder/coder/internal/testutil"
4142
"github.com/coder/coder/provisioner/echo"
4243
"github.com/coder/coder/provisionersdk/proto"
4344
)
@@ -157,7 +158,7 @@ func TestAuthorizeAllEndpoints(t *testing.T) {
157158
require.Eventually(t, func() bool {
158159
provisionerds, err := client.ProvisionerDaemons(ctx)
159160
return assert.NoError(t, err) && len(provisionerds) > 0
160-
}, time.Second*10, time.Second)
161+
}, testutil.WaitLong, testutil.IntervalSlow)
161162

162163
provisionerds, err := client.ProvisionerDaemons(ctx)
163164
require.NoError(t, err, "fetch provisioners")

coderd/devtunnel/tunnel_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"cdr.dev/slog/sloggers/slogtest"
2727
"github.com/coder/coder/coderd/devtunnel"
28+
"github.com/coder/coder/internal/testutil"
2829
)
2930

3031
const (
@@ -85,15 +86,15 @@ func TestTunnel(t *testing.T) {
8586
_, _ = io.Copy(io.Discard, res.Body)
8687

8788
return res.StatusCode == http.StatusAccepted
88-
}, time.Minute, time.Second)
89+
}, testutil.WaitShort, testutil.IntervalSlow)
8990

9091
assert.NoError(t, server.Close())
9192
cancelTun()
9293

9394
select {
9495
case <-errCh:
95-
case <-time.After(10 * time.Second):
96-
t.Error("tunnel did not close after 10 seconds")
96+
case <-time.After(testutil.WaitLong):
97+
t.Errorf("tunnel did not close after %s", testutil.WaitLong)
9798
}
9899
}
99100

@@ -226,7 +227,7 @@ func (f *fakeTunnelServer) requestHTTP() (*http.Response, error) {
226227
}
227228
client := &http.Client{
228229
Transport: transport,
229-
Timeout: 10 * time.Second,
230+
Timeout: testutil.WaitLong,
230231
}
231232
return client.Get(fmt.Sprintf("http://[%s]:8090", clientIP))
232233
}

coderd/httpmw/ratelimit_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import (
44
"net/http"
55
"net/http/httptest"
66
"testing"
7-
"time"
87

98
"github.com/go-chi/chi/v5"
109
"github.com/stretchr/testify/require"
1110

1211
"github.com/coder/coder/coderd/httpmw"
12+
"github.com/coder/coder/internal/testutil"
1313
)
1414

1515
func TestRateLimit(t *testing.T) {
@@ -27,6 +27,6 @@ func TestRateLimit(t *testing.T) {
2727
rec := httptest.NewRecorder()
2828
rtr.ServeHTTP(rec, req)
2929
return rec.Result().StatusCode == http.StatusTooManyRequests
30-
}, 5*time.Second, time.Millisecond)
30+
}, testutil.WaitShort, testutil.IntervalFast)
3131
})
3232
}

coderd/provisionerdaemons_test.go

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

109
"github.com/stretchr/testify/assert"
1110
"github.com/stretchr/testify/require"
1211

1312
"github.com/coder/coder/coderd/coderdtest"
1413
"github.com/coder/coder/codersdk"
14+
"github.com/coder/coder/internal/testutil"
1515
"github.com/coder/coder/provisionersdk"
1616
)
1717

@@ -41,7 +41,7 @@ func TestProvisionerDaemons(t *testing.T) {
4141
var err error
4242
version, err = client.TemplateVersion(context.Background(), version.ID)
4343
return assert.NoError(t, err) && version.Job.Error != ""
44-
}, 5*time.Second, 25*time.Millisecond)
44+
}, testutil.WaitShort, testutil.IntervalFast)
4545
})
4646
}
4747

0 commit comments

Comments
 (0)