reuseport_test.go 851 Bytes
Newer Older
Fazlul Shahriar's avatar
Fazlul Shahriar committed
1 2
// +build !plan9

Steven Allen's avatar
Steven Allen committed
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
package tcpreuse

import (
	"net"
	"syscall"
	"testing"
)

type netTimeoutErr struct {
	timeout bool
}

func (e netTimeoutErr) Error() string {
	return ""
}

func (e netTimeoutErr) Timeout() bool {
	return e.timeout
}

func (e netTimeoutErr) Temporary() bool {
	panic("not checked")
}

func TestReuseError(t *testing.T) {
	var nte1 net.Error = &netTimeoutErr{true}
	var nte2 net.Error = &netTimeoutErr{false}

	cases := map[error]bool{
		nil:                   false,
		syscall.EADDRINUSE:    true,
		syscall.EADDRNOTAVAIL: true,
		syscall.ECONNREFUSED:  false,

		nte1: false,
		nte2: true, // this ones a little weird... we should check neterror.Temporary() too

		// test 'default' to true
		syscall.EBUSY: true,
	}

	for k, v := range cases {
Steven Allen's avatar
Steven Allen committed
45
		if reuseErrShouldRetry(k) != v {
Steven Allen's avatar
Steven Allen committed
46 47 48 49 50
			t.Fatalf("expected %t for %#v", v, k)
		}
	}

}