forked from libp2p/go-reuseport
-
Notifications
You must be signed in to change notification settings - Fork 3
/
reuse_test.go
52 lines (45 loc) · 1.21 KB
/
reuse_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package reuseport
import (
"context"
"net"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func testDialFromListeningPort(t *testing.T, network, host string) {
lc := net.ListenConfig{
Control: Control,
}
ctx := context.Background()
ll, err := lc.Listen(ctx, network, host+":0")
if err != nil && strings.Contains(err.Error(), "cannot assign requested address") {
t.Skip(err)
}
require.NoError(t, err)
rl, err := lc.Listen(ctx, network, host+":0")
require.NoError(t, err)
d := net.Dialer{
LocalAddr: ll.Addr(),
Control: Control,
}
c, err := d.Dial(network, rl.Addr().String())
require.NoError(t, err)
c.Close()
}
func TestDialFromListeningPort(t *testing.T) {
testDialFromListeningPort(t, "tcp", "localhost")
}
func TestDialFromListeningPortTcp6(t *testing.T) {
testDialFromListeningPort(t, "tcp6", "[::1]")
}
func TestListenPacketWildcardAddress(t *testing.T) {
pc, err := ListenPacket("udp", ":0")
require.NoError(t, err)
pc.Close()
}
func TestErrorWhenDialUnresolvable(t *testing.T) {
_, err := Dial("asd", "127.0.0.1:1234", "127.0.0.1:1234")
require.ErrorIs(t, err, net.UnknownNetworkError("asd"))
_, err = Dial("tcp", "a.b.c.d:1234", "a.b.c.d:1235")
require.Error(t, err)
}