Skip to content

Commit 70cede8

Browse files
authored
test(agent): improve TestAgent_Dial tests (#11013)
Refs #11008
1 parent b212bd4 commit 70cede8

File tree

1 file changed

+43
-22
lines changed

1 file changed

+43
-22
lines changed

agent/agent_test.go

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,32 +1547,33 @@ func TestAgent_Dial(t *testing.T) {
15471547
t.Run(c.name, func(t *testing.T) {
15481548
t.Parallel()
15491549

1550-
// Setup listener
1550+
// The purpose of this test is to ensure that a client can dial a
1551+
// listener in the workspace over tailnet.
15511552
l := c.setup(t)
1552-
defer l.Close()
1553-
go func() {
1554-
for {
1555-
c, err := l.Accept()
1556-
if err != nil {
1557-
return
1558-
}
1553+
done := make(chan struct{})
1554+
defer func() {
1555+
l.Close()
1556+
<-done
1557+
}()
15591558

1560-
go testAccept(t, c)
1561-
}
1559+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
1560+
defer cancel()
1561+
1562+
go func() {
1563+
defer close(done)
1564+
c, err := l.Accept()
1565+
assert.NoError(t, err, "accept connection")
1566+
defer c.Close()
1567+
testAccept(ctx, t, c)
15621568
}()
15631569

15641570
//nolint:dogsled
1565-
conn, _, _, _, _ := setupAgent(t, agentsdk.Manifest{}, 0)
1566-
require.True(t, conn.AwaitReachable(context.Background()))
1567-
conn1, err := conn.DialContext(context.Background(), l.Addr().Network(), l.Addr().String())
1571+
agentConn, _, _, _, _ := setupAgent(t, agentsdk.Manifest{}, 0)
1572+
require.True(t, agentConn.AwaitReachable(ctx))
1573+
conn, err := agentConn.DialContext(ctx, l.Addr().Network(), l.Addr().String())
15681574
require.NoError(t, err)
1569-
defer conn1.Close()
1570-
conn2, err := conn.DialContext(context.Background(), l.Addr().Network(), l.Addr().String())
1571-
require.NoError(t, err)
1572-
defer conn2.Close()
1573-
testDial(t, conn2)
1574-
testDial(t, conn1)
1575-
time.Sleep(150 * time.Millisecond)
1575+
defer conn.Close()
1576+
testDial(ctx, t, conn)
15761577
})
15771578
}
15781579
}
@@ -2002,22 +2003,41 @@ func setupAgent(t *testing.T, metadata agentsdk.Manifest, ptyTimeout time.Durati
20022003

20032004
var dialTestPayload = []byte("dean-was-here123")
20042005

2005-
func testDial(t *testing.T, c net.Conn) {
2006+
func testDial(ctx context.Context, t *testing.T, c net.Conn) {
20062007
t.Helper()
20072008

2009+
if deadline, ok := ctx.Deadline(); ok {
2010+
err := c.SetDeadline(deadline)
2011+
assert.NoError(t, err)
2012+
defer func() {
2013+
err := c.SetDeadline(time.Time{})
2014+
assert.NoError(t, err)
2015+
}()
2016+
}
2017+
20082018
assertWritePayload(t, c, dialTestPayload)
20092019
assertReadPayload(t, c, dialTestPayload)
20102020
}
20112021

2012-
func testAccept(t *testing.T, c net.Conn) {
2022+
func testAccept(ctx context.Context, t *testing.T, c net.Conn) {
20132023
t.Helper()
20142024
defer c.Close()
20152025

2026+
if deadline, ok := ctx.Deadline(); ok {
2027+
err := c.SetDeadline(deadline)
2028+
assert.NoError(t, err)
2029+
defer func() {
2030+
err := c.SetDeadline(time.Time{})
2031+
assert.NoError(t, err)
2032+
}()
2033+
}
2034+
20162035
assertReadPayload(t, c, dialTestPayload)
20172036
assertWritePayload(t, c, dialTestPayload)
20182037
}
20192038

20202039
func assertReadPayload(t *testing.T, r io.Reader, payload []byte) {
2040+
t.Helper()
20212041
b := make([]byte, len(payload)+16)
20222042
n, err := r.Read(b)
20232043
assert.NoError(t, err, "read payload")
@@ -2026,6 +2046,7 @@ func assertReadPayload(t *testing.T, r io.Reader, payload []byte) {
20262046
}
20272047

20282048
func assertWritePayload(t *testing.T, w io.Writer, payload []byte) {
2049+
t.Helper()
20292050
n, err := w.Write(payload)
20302051
assert.NoError(t, err, "write payload")
20312052
assert.Equal(t, len(payload), n, "payload length does not match")

0 commit comments

Comments
 (0)