Skip to content

chore: Use standardized test timeouts and delays #3291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/coder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,11 @@ jobs:
echo ::set-output name=cover::false
fi
set -x
gotestsum --junitfile="gotests.xml" --packages="./..." -- -parallel=8 -timeout=5m -short -failfast $COVERAGE_FLAGS
test_timeout=5m
if [[ "${{ matrix.os }}" == windows* ]]; then
test_timeout=10m
fi
gotestsum --junitfile="gotests.xml" --packages="./..." -- -parallel=8 -timeout=$test_timeout -short -failfast $COVERAGE_FLAGS

- name: Upload DataDog Trace
if: github.actor != 'dependabot[bot]' && !github.event.pull_request.head.repo.fork
Expand Down
3 changes: 2 additions & 1 deletion agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/coder/coder/peerbroker/proto"
"github.com/coder/coder/provisionersdk"
"github.com/coder/coder/pty/ptytest"
"github.com/coder/coder/testutil"
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -257,7 +258,7 @@ func TestAgent(t *testing.T) {
}
gotContent = string(content)
return true
}, 15*time.Second, 100*time.Millisecond)
}, testutil.WaitMedium, testutil.IntervalMedium)
require.Equal(t, content, strings.TrimSpace(gotContent))
})

Expand Down
4 changes: 2 additions & 2 deletions agent/reaper/reaper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/coder/coder/agent/reaper"
"github.com/coder/coder/testutil"
)

func TestReap(t *testing.T) {
Expand Down Expand Up @@ -52,10 +53,9 @@ func TestReap(t *testing.T) {

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

deadline := time.NewTimer(time.Second * 5)
for i := 0; i < len(expectedPIDs); i++ {
select {
case <-deadline.C:
case <-time.After(testutil.WaitShort):
t.Fatalf("Timed out waiting for process")
case pid := <-pids:
require.Contains(t, expectedPIDs, pid)
Expand Down
8 changes: 4 additions & 4 deletions cli/cliui/prompt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"os/exec"
"testing"
"time"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/pty"
"github.com/coder/coder/pty/ptytest"
"github.com/coder/coder/testutil"
)

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

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

// nolint:unused
Expand Down
3 changes: 2 additions & 1 deletion cli/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/coder/coder/provisioner/echo"
"github.com/coder/coder/provisionersdk/proto"
"github.com/coder/coder/pty/ptytest"
"github.com/coder/coder/testutil"
)

func TestCreate(t *testing.T) {
Expand Down Expand Up @@ -88,7 +89,7 @@ func TestCreate(t *testing.T) {

member := coderdtest.CreateAnotherUser(t, client, user.OrganizationID)
clitest.SetupConfig(t, member, root)
cmdCtx, done := context.WithTimeout(context.Background(), 10*time.Second)
cmdCtx, done := context.WithTimeout(context.Background(), testutil.WaitLong)
go func() {
defer done()
err := cmd.ExecuteContext(cmdCtx)
Expand Down
4 changes: 2 additions & 2 deletions cli/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package cli_test
import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/pty/ptytest"
"github.com/coder/coder/testutil"
)

func TestList(t *testing.T) {
Expand All @@ -29,7 +29,7 @@ func TestList(t *testing.T) {
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())

ctx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second)
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancelFunc()
done := make(chan any)
go func() {
Expand Down
11 changes: 6 additions & 5 deletions cli/portforward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/coder/coder/codersdk"
"github.com/coder/coder/provisioner/echo"
"github.com/coder/coder/provisionersdk/proto"
"github.com/coder/coder/testutil"
)

func TestPortForward(t *testing.T) {
Expand Down Expand Up @@ -170,7 +171,7 @@ func TestPortForward(t *testing.T) {

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

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

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

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

data := output.String()
if strings.Contains(data, "Ready!") {
Expand Down
4 changes: 2 additions & 2 deletions cli/resetpassword_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/url"
"runtime"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -14,6 +13,7 @@ import (
"github.com/coder/coder/coderd/database/postgres"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/pty/ptytest"
"github.com/coder/coder/testutil"
)

// nolint:paralleltest
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestResetPassword(t *testing.T) {
require.Eventually(t, func() bool {
rawURL, err = cfg.URL().Read()
return err == nil && rawURL != ""
}, 15*time.Second, 25*time.Millisecond)
}, testutil.WaitLong, testutil.IntervalFast)
accessURL, err := url.Parse(rawURL)
require.NoError(t, err)
client := codersdk.New(accessURL)
Expand Down
68 changes: 29 additions & 39 deletions cli/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import (
"go.uber.org/goleak"

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/cli/config"
"github.com/coder/coder/coderd/database/postgres"
"github.com/coder/coder/coderd/telemetry"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/pty/ptytest"
"github.com/coder/coder/testutil"
)

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

_, err = client.CreateFirstUser(ctx, codersdk.CreateFirstUserRequest{
Expand Down Expand Up @@ -94,10 +90,11 @@ func TestServer(t *testing.T) {
go func() {
errC <- root.ExecuteContext(ctx)
}()
//nolint:gocritic // Embedded postgres take a while to fire up.
require.Eventually(t, func() bool {
accessURLRaw, err := cfg.URL().Read()
return accessURLRaw != "" && err == nil
}, 3*time.Minute, 250*time.Millisecond)
rawURL, err := cfg.URL().Read()
return err == nil && rawURL != ""
}, 3*time.Minute, testutil.IntervalFast, "failed to get access URL")
cancelFunc()
require.ErrorIs(t, <-errC, context.Canceled)
})
Expand Down Expand Up @@ -135,11 +132,7 @@ func TestServer(t *testing.T) {
}()

// Just wait for startup
require.Eventually(t, func() bool {
var err error
_, err = cfg.URL().Read()
return err == nil
}, 15*time.Second, 25*time.Millisecond)
_ = waitAccessURL(t, cfg)

cancelFunc()
require.ErrorIs(t, <-errC, context.Canceled)
Expand Down Expand Up @@ -169,11 +162,7 @@ func TestServer(t *testing.T) {
}()

// Just wait for startup
require.Eventually(t, func() bool {
var err error
_, err = cfg.URL().Read()
return err == nil
}, 15*time.Second, 25*time.Millisecond)
_ = waitAccessURL(t, cfg)

cancelFunc()
require.ErrorIs(t, <-errC, context.Canceled)
Expand Down Expand Up @@ -201,11 +190,7 @@ func TestServer(t *testing.T) {
}()

// Just wait for startup
require.Eventually(t, func() bool {
var err error
_, err = cfg.URL().Read()
return err == nil
}, 15*time.Second, 25*time.Millisecond)
_ = waitAccessURL(t, cfg)

cancelFunc()
require.ErrorIs(t, <-errC, context.Canceled)
Expand Down Expand Up @@ -281,14 +266,7 @@ func TestServer(t *testing.T) {
}()

// Verify HTTPS
var accessURLRaw string
require.Eventually(t, func() bool {
var err error
accessURLRaw, err = cfg.URL().Read()
return accessURLRaw != "" && err == nil
}, 15*time.Second, 25*time.Millisecond)
accessURL, err := url.Parse(accessURLRaw)
require.NoError(t, err)
accessURL := waitAccessURL(t, cfg)
require.Equal(t, "https", accessURL.Scheme)
client := codersdk.New(accessURL)
client.HTTPClient = &http.Client{
Expand All @@ -299,7 +277,7 @@ func TestServer(t *testing.T) {
},
},
}
_, err = client.HasFirstUser(ctx)
_, err := client.HasFirstUser(ctx)
require.NoError(t, err)

cancelFunc()
Expand All @@ -326,11 +304,7 @@ func TestServer(t *testing.T) {
go func() {
serverErr <- root.ExecuteContext(ctx)
}()
require.Eventually(t, func() bool {
var err error
_, err = cfg.URL().Read()
return err == nil
}, 15*time.Second, 25*time.Millisecond)
_ = waitAccessURL(t, cfg)
currentProcess, err := os.FindProcess(os.Getpid())
require.NoError(t, err)
err = currentProcess.Signal(os.Interrupt)
Expand Down Expand Up @@ -436,3 +410,19 @@ func generateTLSCertificate(t testing.TB) (certPath, keyPath string) {
require.NoError(t, err)
return certFile.Name(), keyFile.Name()
}

func waitAccessURL(t *testing.T, cfg config.Root) *url.URL {
t.Helper()

var err error
var rawURL string
require.Eventually(t, func() bool {
rawURL, err = cfg.URL().Read()
return err == nil && rawURL != ""
}, testutil.WaitLong, testutil.IntervalFast, "failed to get access URL")

accessURL, err := url.Parse(rawURL)
require.NoError(t, err, "failed to parse access URL")

return accessURL
}
7 changes: 4 additions & 3 deletions cli/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/coder/coder/provisioner/echo"
"github.com/coder/coder/provisionersdk/proto"
"github.com/coder/coder/pty/ptytest"
"github.com/coder/coder/testutil"
)

func setupWorkspaceForSSH(t *testing.T) (*codersdk.Client, codersdk.Workspace, string) {
Expand Down Expand Up @@ -77,7 +78,7 @@ func TestSSH(t *testing.T) {
cmd.SetErr(pty.Output())
cmd.SetOut(pty.Output())

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

cmdDone := tGo(t, func() {
Expand Down Expand Up @@ -124,7 +125,7 @@ func TestSSH(t *testing.T) {
}
}()

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

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

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

cmd, root := clitest.New(t,
Expand Down
Loading