transport_test.go 3.35 KB
Newer Older
Steven Allen's avatar
Steven Allen committed
1 2 3
package tcpreuse

import (
Steven Allen's avatar
Steven Allen committed
4
	"net"
Steven Allen's avatar
Steven Allen committed
5 6 7 8 9
	"testing"

	ma "github.com/multiformats/go-multiaddr"
)

Steven Allen's avatar
Steven Allen committed
10
func TestNoneAndSingle(t *testing.T) {
Steven Allen's avatar
Steven Allen committed
11 12 13 14 15 16 17 18
	var trA Transport
	var trB Transport
	laddr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/0")
	listenerA, err := trA.Listen(laddr)
	if err != nil {
		t.Fatal(err)
	}
	defer listenerA.Close()
Steven Allen's avatar
Steven Allen committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

	done := make(chan struct{})
	go func() {
		defer close(done)
		c, err := listenerA.Accept()
		if err != nil {
			t.Fatal(err)
		}
		c.Close()
	}()

	c, err := trB.Dial(listenerA.Multiaddr())
	if err != nil {
		t.Fatal(err)
	}
	<-done
	c.Close()

Steven Allen's avatar
Steven Allen committed
37 38 39 40 41 42
	listenerB, err := trB.Listen(laddr)
	if err != nil {
		t.Fatal(err)
	}
	defer listenerB.Close()

Steven Allen's avatar
Steven Allen committed
43
	done = make(chan struct{})
Steven Allen's avatar
Steven Allen committed
44 45 46 47 48 49 50 51 52
	go func() {
		defer close(done)
		c, err := listenerA.Accept()
		if err != nil {
			t.Fatal(err)
		}
		c.Close()
	}()

Steven Allen's avatar
Steven Allen committed
53
	c, err = trB.Dial(listenerA.Multiaddr())
Steven Allen's avatar
Steven Allen committed
54 55 56
	if err != nil {
		t.Fatal(err)
	}
Steven Allen's avatar
Steven Allen committed
57 58 59 60 61
	actual := c.LocalAddr().(*net.TCPAddr).Port
	expected := listenerB.Addr().(*net.TCPAddr).Port
	if actual != expected {
		t.Errorf("expected to use port %d, used port %d", expected, actual)
	}
Steven Allen's avatar
Steven Allen committed
62 63 64
	<-done
	c.Close()
}
Steven Allen's avatar
Steven Allen committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

func TestTwoLocal(t *testing.T) {
	var trA Transport
	var trB Transport
	laddr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/0")
	listenerA, err := trA.Listen(laddr)
	if err != nil {
		t.Fatal(err)
	}
	defer listenerA.Close()

	listenerB1, err := trB.Listen(laddr)
	if err != nil {
		t.Fatal(err)
	}
	defer listenerB1.Close()

	listenerB2, err := trB.Listen(laddr)
	if err != nil {
		t.Fatal(err)
	}
	defer listenerB2.Close()

	done := make(chan struct{})
	go func() {
		defer close(done)
		c, err := listenerA.Accept()
		if err != nil {
			t.Fatal(err)
		}
		c.Close()
	}()

	c, err := trB.Dial(listenerA.Multiaddr())
	if err != nil {
		t.Fatal(err)
	}
	localPort := c.LocalAddr().(*net.TCPAddr).Port
	if localPort != listenerB1.Addr().(*net.TCPAddr).Port &&
		localPort != listenerB2.Addr().(*net.TCPAddr).Port {
		t.Fatal("didn't dial from one of our listener ports")
	}
	<-done
	c.Close()
}
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

func TestLocalAndUnspecified(t *testing.T) {
	var trA Transport
	var trB Transport
	laddr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/0")
	unspec, _ := ma.NewMultiaddr("/ip4/0.0.0.0/tcp/0")
	listenerA, err := trA.Listen(laddr)
	if err != nil {
		t.Fatal(err)
	}
	defer listenerA.Close()

	listenerB1, err := trB.Listen(laddr)
	if err != nil {
		t.Fatal(err)
	}
	defer listenerB1.Close()

	listenerB2, err := trB.Listen(unspec)
	if err != nil {
		t.Fatal(err)
	}
	defer listenerB2.Close()

	done := make(chan struct{})
	go func() {
		defer close(done)
		c, err := listenerA.Accept()
		if err != nil {
			t.Fatal(err)
		}
		c.Close()
	}()

	c, err := trB.Dial(listenerA.Multiaddr())
	if err != nil {
		t.Fatal(err)
	}
	actual := c.LocalAddr().(*net.TCPAddr).Port
	expected := listenerB1.Addr().(*net.TCPAddr).Port
	if actual != expected {
		t.Errorf("expected to use port %d, used port %d", expected, actual)
	}
	<-done
	c.Close()

	// Closing the listener should reset the dialer.
	listenerB1.Close()

	done = make(chan struct{})
	go func() {
		defer close(done)
		c, err := listenerA.Accept()
		if err != nil {
			t.Fatal(err)
		}
		c.Close()
	}()

	c, err = trB.Dial(listenerA.Multiaddr())
	if err != nil {
		t.Fatal(err)
	}
	actual = c.LocalAddr().(*net.TCPAddr).Port
	expected = listenerB2.Addr().(*net.TCPAddr).Port
	if actual != expected {
		t.Errorf("expected to use port %d, used port %d", expected, actual)
	}
	<-done
	c.Close()
}