Skip to content

Commit d2c1787

Browse files
committedDec 7, 2019
fix: make tests robust for go 1.13
The file descriptors aren't stable unless we re-exec this way.
1 parent 271a738 commit d2c1787

File tree

1 file changed

+76
-65
lines changed

1 file changed

+76
-65
lines changed
 

‎sockets_test.go

+76-65
Original file line numberDiff line numberDiff line change
@@ -3,86 +3,97 @@
33
package sockets
44

55
import (
6+
"fmt"
67
"io/ioutil"
78
"net"
89
"os"
10+
"os/exec"
911
"strconv"
1012
"testing"
1113
)
1214

1315
func TestActivation(t *testing.T) {
14-
lt, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.IPv6loopback})
15-
if err != nil {
16-
t.Fatal(err)
17-
}
18-
if c, err := lt.SyscallConn(); err == nil {
19-
var fd uintptr
20-
c.Control(func(nfd uintptr) {
21-
fd = nfd
22-
})
23-
if fd != 3 {
24-
t.Fatal("expected fd to be 3")
16+
testTcpAddr := os.Getenv("SOCKET_TEST_TCP_ADDR")
17+
testUdpAddr := os.Getenv("SOCKET_TEST_UDP_ADDR")
18+
if testTcpAddr == "" {
19+
lt, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.IPv6loopback})
20+
if err != nil {
21+
t.Fatal(err)
22+
}
23+
tcpAddr := lt.Addr()
24+
ltf, err := lt.File()
25+
lt.Close()
26+
if err != nil {
27+
t.Fatal(err)
2528
}
26-
} else {
27-
t.Fatal(err)
28-
}
2929

30-
lu, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv6loopback})
31-
if err != nil {
32-
t.Fatal(err)
33-
}
34-
if c, err := lu.SyscallConn(); err == nil {
35-
var fd uintptr
36-
c.Control(func(nfd uintptr) {
37-
fd = nfd
38-
})
39-
if fd != 4 {
40-
t.Fatal("expected fd to be 3")
30+
lu, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv6loopback})
31+
if err != nil {
32+
t.Fatal(err)
33+
}
34+
udpAddr := lu.LocalAddr()
35+
luf, err := lu.File()
36+
lu.Close()
37+
if err != nil {
38+
t.Fatal(err)
4139
}
42-
} else {
43-
t.Fatal(err)
44-
}
4540

46-
os.Setenv("LISTEN_FDS", "2")
47-
os.Setenv("LISTEN_PID", strconv.Itoa(os.Getpid()))
48-
os.Setenv("LISTEN_FDNAMES", "tcp:udp")
49-
tcp, err := TakeListeners("tcp")
50-
if err != nil {
51-
t.Fatal(err)
52-
}
53-
if len(tcp) != 1 {
54-
t.Fatalf("expected 1 listener, got %d", len(tcp))
55-
}
41+
cmd := exec.Command("/proc/self/exe", "-test.run", "TestActivation")
42+
cmd.ExtraFiles = append(cmd.ExtraFiles, ltf, luf)
43+
cmd.Stdout = os.Stdout
44+
cmd.Stderr = os.Stderr
45+
cmd.Env = append(
46+
cmd.Env,
47+
"LISTEN_FDS=2",
48+
"LISTEN_FDNAMES=tcp:udp",
49+
fmt.Sprintf("SOCKET_TEST_TCP_ADDR=%s", tcpAddr),
50+
fmt.Sprintf("SOCKET_TEST_UDP_ADDR=%s", udpAddr),
51+
)
52+
err = cmd.Run()
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
} else {
57+
// can't set this from the parent because we can't run anything between fork/exec.
58+
os.Setenv("LISTEN_PID", strconv.Itoa(os.Getpid()))
59+
tcp, err := TakeListeners("tcp")
60+
if err != nil {
61+
t.Fatal(err)
62+
}
63+
if len(tcp) != 1 {
64+
t.Fatalf("expected 1 listener, got %d", len(tcp))
65+
}
5666

57-
go func() {
58-
l := tcp[0]
59-
c, err := l.Accept()
67+
go func() {
68+
l := tcp[0]
69+
c, err := l.Accept()
70+
if err != nil {
71+
t.Error(err)
72+
return
73+
}
74+
c.Write([]byte("foobar"))
75+
c.Close()
76+
}()
77+
c, err := net.Dial("tcp6", testTcpAddr)
78+
if err != nil {
79+
t.Fatal(err)
80+
}
81+
b, err := ioutil.ReadAll(c)
6082
if err != nil {
6183
t.Error(err)
62-
return
6384
}
64-
c.Write([]byte("foobar"))
65-
c.Close()
66-
}()
67-
c, err := net.DialTCP("tcp", nil, lt.Addr().(*net.TCPAddr))
68-
if err != nil {
69-
t.Fatal(err)
70-
}
71-
b, err := ioutil.ReadAll(c)
72-
if err != nil {
73-
t.Error(err)
74-
}
75-
if string(b) != "foobar" {
76-
t.Error("wrong message")
77-
}
78-
udp, err := TakePacketConns("udp")
79-
if err != nil {
80-
t.Fatal(err)
81-
}
82-
if len(udp) != 1 {
83-
t.Fatalf("expected 1 listener, got %d", len(tcp))
84-
}
85-
if udp[0].LocalAddr().String() != lu.LocalAddr().String() {
86-
t.Fatal("got the wrong udp listener")
85+
if string(b) != "foobar" {
86+
t.Error("wrong message")
87+
}
88+
udp, err := TakePacketConns("udp")
89+
if err != nil {
90+
t.Fatal(err)
91+
}
92+
if len(udp) != 1 {
93+
t.Fatalf("expected 1 listener, got %d", len(tcp))
94+
}
95+
if udp[0].LocalAddr().String() != testUdpAddr {
96+
t.Fatal("got the wrong udp listener")
97+
}
8798
}
8899
}

0 commit comments

Comments
 (0)