Skip to content

Commit 0887b6e

Browse files
committed
test(agent): improve TestAgent_Dial tests
Refs #11008
1 parent dbadae5 commit 0887b6e

File tree

1 file changed

+51
-10
lines changed

1 file changed

+51
-10
lines changed

agent/agent_test.go

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"errors"
78
"fmt"
89
"io"
910
"math/rand"
@@ -1549,30 +1550,50 @@ func TestAgent_Dial(t *testing.T) {
15491550

15501551
// Setup listener
15511552
l := c.setup(t)
1552-
defer l.Close()
1553+
closed := make(chan struct{})
1554+
defer func() {
1555+
l.Close()
1556+
<-closed
1557+
}()
1558+
1559+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
1560+
defer cancel()
1561+
15531562
go func() {
1563+
var wg sync.WaitGroup
1564+
defer func() {
1565+
wg.Wait()
1566+
close(closed)
1567+
}()
15541568
for {
15551569
c, err := l.Accept()
15561570
if err != nil {
1571+
if !errors.Is(err, net.ErrClosed) && !errors.Is(err, udp.ErrClosedListener) {
1572+
assert.NoError(t, err, "accept connection")
1573+
continue
1574+
}
15571575
return
15581576
}
15591577

1560-
go testAccept(t, c)
1578+
wg.Add(1)
1579+
go func() {
1580+
defer wg.Done()
1581+
testAccept(ctx, t, c)
1582+
}()
15611583
}
15621584
}()
15631585

15641586
//nolint:dogsled
15651587
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())
1588+
require.True(t, conn.AwaitReachable(ctx))
1589+
conn1, err := conn.DialContext(ctx, l.Addr().Network(), l.Addr().String())
15681590
require.NoError(t, err)
15691591
defer conn1.Close()
1570-
conn2, err := conn.DialContext(context.Background(), l.Addr().Network(), l.Addr().String())
1592+
conn2, err := conn.DialContext(ctx, l.Addr().Network(), l.Addr().String())
15711593
require.NoError(t, err)
15721594
defer conn2.Close()
1573-
testDial(t, conn2)
1574-
testDial(t, conn1)
1575-
time.Sleep(150 * time.Millisecond)
1595+
testDial(ctx, t, conn2)
1596+
testDial(ctx, t, conn1)
15761597
})
15771598
}
15781599
}
@@ -2002,22 +2023,41 @@ func setupAgent(t *testing.T, metadata agentsdk.Manifest, ptyTimeout time.Durati
20022023

20032024
var dialTestPayload = []byte("dean-was-here123")
20042025

2005-
func testDial(t *testing.T, c net.Conn) {
2026+
func testDial(ctx context.Context, t *testing.T, c net.Conn) {
20062027
t.Helper()
20072028

2029+
if deadline, ok := ctx.Deadline(); ok {
2030+
err := c.SetDeadline(deadline)
2031+
assert.NoError(t, err)
2032+
defer func() {
2033+
err := c.SetDeadline(time.Time{})
2034+
assert.NoError(t, err)
2035+
}()
2036+
}
2037+
20082038
assertWritePayload(t, c, dialTestPayload)
20092039
assertReadPayload(t, c, dialTestPayload)
20102040
}
20112041

2012-
func testAccept(t *testing.T, c net.Conn) {
2042+
func testAccept(ctx context.Context, t *testing.T, c net.Conn) {
20132043
t.Helper()
20142044
defer c.Close()
20152045

2046+
if deadline, ok := ctx.Deadline(); ok {
2047+
err := c.SetDeadline(deadline)
2048+
assert.NoError(t, err)
2049+
defer func() {
2050+
err := c.SetDeadline(time.Time{})
2051+
assert.NoError(t, err)
2052+
}()
2053+
}
2054+
20162055
assertReadPayload(t, c, dialTestPayload)
20172056
assertWritePayload(t, c, dialTestPayload)
20182057
}
20192058

20202059
func assertReadPayload(t *testing.T, r io.Reader, payload []byte) {
2060+
t.Helper()
20212061
b := make([]byte, len(payload)+16)
20222062
n, err := r.Read(b)
20232063
assert.NoError(t, err, "read payload")
@@ -2026,6 +2066,7 @@ func assertReadPayload(t *testing.T, r io.Reader, payload []byte) {
20262066
}
20272067

20282068
func assertWritePayload(t *testing.T, w io.Writer, payload []byte) {
2069+
t.Helper()
20292070
n, err := w.Write(payload)
20302071
assert.NoError(t, err, "write payload")
20312072
assert.Equal(t, len(payload), n, "payload length does not match")

0 commit comments

Comments
 (0)