- Notifications
You must be signed in to change notification settings - Fork928
chore: Speed up port-forward tests#3062
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -142,21 +142,22 @@ func TestPortForward(t *testing.T) { | ||
}, | ||
} | ||
// Setup agent once to be shared between test-cases (avoid expensive | ||
// non-parallel setup). | ||
var ( | ||
client = coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true}) | ||
user = coderdtest.CreateFirstUser(t, client) | ||
_, workspace = runAgent(t, client, user.UserID) | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Note this change: We now share coderd/agent between tests. I didn't see any reason not-to. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. this will be an interesting test of how useful our logging messages are, since we're sharing a logger among multiple things happening at once (just like in a real deployment 😰 ) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. That's an interesting point I did not think of. We'll also be sharing the parent | ||
for _, c := range cases { //nolint:paralleltest // the `c := c` confuses the linter | ||
c := c | ||
//Delay paralleltests here because setupLocal reserves | ||
// a free open port which is not guaranteed to be free | ||
// between the listener closing and port-forward ready. | ||
t.Run(c.name, func(t *testing.T) { | ||
t.Run("OnePort", func(t *testing.T) { | ||
p1 := setupTestListener(t, c.setupRemote(t)) | ||
// Create a flag that forwards from local to listener 1. | ||
localAddress, localFlag := c.setupLocal(t) | ||
@@ -176,6 +177,8 @@ func TestPortForward(t *testing.T) { | ||
}() | ||
waitForPortForwardReady(t, buf) | ||
t.Parallel() // Port is reserved, enable parallel execution. | ||
// Open two connections simultaneously and test them out of | ||
// sync. | ||
d := net.Dialer{Timeout: 3 * time.Second} | ||
@@ -196,11 +199,8 @@ func TestPortForward(t *testing.T) { | ||
//nolint:paralleltest | ||
t.Run("TwoPorts", func(t *testing.T) { | ||
var ( | ||
p1 = setupTestListener(t, c.setupRemote(t)) | ||
p2 = setupTestListener(t, c.setupRemote(t)) | ||
) | ||
// Create a flags for listener 1 and listener 2. | ||
@@ -223,6 +223,8 @@ func TestPortForward(t *testing.T) { | ||
}() | ||
waitForPortForwardReady(t, buf) | ||
t.Parallel() // Port is reserved, enable parallel execution. | ||
// Open a connection to both listener 1 and 2 simultaneously and | ||
// then test them out of order. | ||
d := net.Dialer{Timeout: 3 * time.Second} | ||
@@ -246,10 +248,6 @@ func TestPortForward(t *testing.T) { | ||
//nolint:paralleltest | ||
t.Run("TCP2Unix", func(t *testing.T) { | ||
var ( | ||
// Find the TCP and Unix cases so we can use their setupLocal and | ||
// setupRemote methods respectively. | ||
tcpCase = cases[0] | ||
@@ -278,6 +276,8 @@ func TestPortForward(t *testing.T) { | ||
}() | ||
waitForPortForwardReady(t, buf) | ||
t.Parallel() // Port is reserved, enable parallel execution. | ||
// Open two connections simultaneously and test them out of | ||
// sync. | ||
d := net.Dialer{Timeout: 3 * time.Second} | ||
@@ -299,9 +299,6 @@ func TestPortForward(t *testing.T) { | ||
//nolint:paralleltest | ||
t.Run("All", func(t *testing.T) { | ||
var ( | ||
// These aren't fixed size because we exclude Unix on Windows. | ||
dials = []addr{} | ||
flags = []string{} | ||
@@ -339,6 +336,8 @@ func TestPortForward(t *testing.T) { | ||
}() | ||
waitForPortForwardReady(t, buf) | ||
t.Parallel() // Port is reserved, enable parallel execution. | ||
// Open connections to all items in the "dial" array. | ||
var ( | ||
d = net.Dialer{Timeout: 3 * time.Second} | ||
@@ -425,6 +424,8 @@ func runAgent(t *testing.T, client *codersdk.Client, userID uuid.UUID) ([]coders | ||
// setupTestListener starts accepting connections and echoing a single packet. | ||
// Returns the listener and the listen port or Unix path. | ||
func setupTestListener(t *testing.T, l net.Listener) string { | ||
t.Helper() | ||
// Wait for listener to completely exit before releasing. | ||
done := make(chan struct{}) | ||
t.Cleanup(func() { | ||
@@ -440,6 +441,7 @@ func setupTestListener(t *testing.T, l net.Listener) string { | ||
for { | ||
c, err := l.Accept() | ||
if err != nil { | ||
_ = l.Close() | ||
return | ||
} | ||
@@ -479,6 +481,7 @@ func testAccept(t *testing.T, c net.Conn) { | ||
} | ||
func assertReadPayload(t *testing.T, r io.Reader, payload []byte) { | ||
t.Helper() | ||
b := make([]byte, len(payload)+16) | ||
n, err := r.Read(b) | ||
assert.NoError(t, err, "read payload") | ||
@@ -487,12 +490,14 @@ func assertReadPayload(t *testing.T, r io.Reader, payload []byte) { | ||
} | ||
func assertWritePayload(t *testing.T, w io.Writer, payload []byte) { | ||
t.Helper() | ||
n, err := w.Write(payload) | ||
assert.NoError(t, err, "write payload") | ||
assert.Equal(t, len(payload), n, "payload length does not match") | ||
} | ||
func waitForPortForwardReady(t *testing.T, output *threadSafeBuffer) { | ||
t.Helper() | ||
for i := 0; i < 100; i++ { | ||
time.Sleep(250 * time.Millisecond) | ||