reuseport_test.go 833 Bytes
Newer Older
Steven Allen's avatar
Steven Allen committed
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
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 {
		if ReuseErrShouldRetry(k) != v {
			t.Fatalf("expected %t for %#v", v, k)
		}
	}

}