chore: Use standardized test timeouts and delays (#3291)

This commit is contained in:
Mathias Fredriksson
2022-08-01 15:45:05 +03:00
committed by GitHub
parent 3d0febdd90
commit 4730c589fe
25 changed files with 198 additions and 109 deletions
+4 -4
View File
@@ -7,7 +7,6 @@ import (
"os"
"os/exec"
"testing"
"time"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
@@ -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) {
@@ -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
@@ -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)
@@ -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
+2 -1
View File
@@ -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) {
@@ -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)
+2 -2
View File
@@ -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) {
@@ -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() {
+6 -5
View File
@@ -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) {
@@ -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()
@@ -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()
@@ -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()
@@ -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 {
@@ -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!") {
+2 -2
View File
@@ -5,7 +5,6 @@ import (
"net/url"
"runtime"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -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
@@ -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)
+29 -39
View File
@@ -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.
@@ -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{
@@ -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)
})
@@ -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)
@@ -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)
@@ -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)
@@ -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{
@@ -299,7 +277,7 @@ func TestServer(t *testing.T) {
},
},
}
_, err = client.HasFirstUser(ctx)
_, err := client.HasFirstUser(ctx)
require.NoError(t, err)
cancelFunc()
@@ -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)
@@ -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
}
+4 -3
View File
@@ -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) {
@@ -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() {
@@ -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)
@@ -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,