Skip to content

Commit 4f6d40e

Browse files
committed
fix: fixed data race in bird testing
1 parent e489d6b commit 4f6d40e

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

pkg/bird/bird_test.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net"
55
"os"
66
"strings"
7+
"sync"
78
"testing"
89
"time"
910

@@ -13,16 +14,20 @@ import (
1314
func TestBirdConn(t *testing.T) {
1415
unixSocket := "test.sock"
1516

16-
// Delete socket
1717
t.Log("Removing existing socket")
1818
_ = os.Remove(unixSocket)
1919

20+
var wg sync.WaitGroup
21+
wg.Add(1)
22+
2023
go func() {
21-
time.Sleep(time.Millisecond * 10) // Wait for the server to start
24+
defer wg.Done()
25+
26+
time.Sleep(10 * time.Millisecond)
2227
resp, _, err := RunCommand("bird command test\n", unixSocket)
28+
2329
assert.Nil(t, err)
2430

25-
// Print bird output as multiple lines
2631
for _, line := range strings.Split(strings.Trim(resp, "\n"), "\n") {
2732
t.Logf("BIRD response (multiline): %s", line)
2833
}
@@ -31,29 +36,25 @@ func TestBirdConn(t *testing.T) {
3136
t.Log("Starting fake BIRD socket server")
3237
l, err := net.Listen("unix", unixSocket)
3338
assert.Nil(t, err)
39+
defer l.Close()
3440

35-
defer func() {
36-
_ = l.Close()
37-
}()
3841
t.Logf("Accepting connection on %s", unixSocket)
3942
conn, err := l.Accept()
40-
if err != nil {
41-
return
42-
}
43-
defer func() {
44-
_ = conn.Close()
45-
}()
43+
assert.Nil(t, err)
44+
defer conn.Close()
4645

4746
_, err = conn.Write([]byte("0001 Fake BIRD response 1\n"))
4847
assert.Nil(t, err)
4948

5049
buf := make([]byte, 1024)
51-
n, err := conn.Read(buf[:])
50+
n, err := conn.Read(buf)
5251
assert.Nil(t, err)
5352
assert.Equal(t, "bird command test\n", string(buf[:n]))
5453

5554
_, err = conn.Write([]byte("0001 Fake BIRD response 2\n"))
5655
assert.Nil(t, err)
56+
57+
wg.Wait()
5758
}
5859

5960
func TestBirdProtocolParseOne(t *testing.T) {

0 commit comments

Comments
 (0)