aboutsummaryrefslogtreecommitdiff
path: root/reuseport/reuseport_test.go
blob: 8ebf3d84175a1958bcc5a2595de8df51f7b4c91d (plain)
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
package reuseport

import (
	"net"
	"testing"
)

func TestTCP4(t *testing.T) {
	t.Parallel()

	testNewListener(t, "tcp4", "localhost:10081")
}

func TestTCP6(t *testing.T) {
	t.Parallel()

	// Run this test only if tcp6 interface exists.
	if hasLocalIPv6(t) {
		testNewListener(t, "tcp6", "[::1]:10082")
	}
}

func hasLocalIPv6(t *testing.T) bool {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		t.Fatalf("cannot obtain local interfaces: %v", err)
	}
	for _, a := range addrs {
		if a.String() == "::1/128" {
			return true
		}
	}
	return false
}

func testNewListener(t *testing.T, network, addr string) {
	ln1, err := Listen(network, addr)
	if err != nil {
		t.Fatalf("cannot create listener %v", err)
	}

	ln2, err := Listen(network, addr)
	if err != nil {
		t.Fatalf("cannot create listener %v", err)
	}

	_ = ln1.Close()
	_ = ln2.Close()
}