(t *testing.T)
| 27 | ) |
| 28 | |
| 29 | func TestGetRandomPorts(t *testing.T) { |
| 30 | Convey("get 1 port", t, func() { |
| 31 | ports, err := GetRandomPorts("127.0.0.1", 1, 10000, 1) |
| 32 | So(ports, ShouldHaveLength, 1) |
| 33 | So(err, ShouldBeNil) |
| 34 | }) |
| 35 | |
| 36 | // make one unusable port here |
| 37 | net.Listen("tcp", "127.0.0.1:2001") |
| 38 | Convey("get too many ports", t, func() { |
| 39 | ports, err := GetRandomPorts("127.0.0.1", 2000, 2010, 100) |
| 40 | So(len(ports), ShouldBeBetween, 0, 12) |
| 41 | So(err, ShouldEqual, ErrNotEnoughPorts) |
| 42 | }) |
| 43 | |
| 44 | Convey("port range error", t, func() { |
| 45 | ports, err := GetRandomPorts("127.0.0.1", 3000, 2010, 1) |
| 46 | So(ports, ShouldBeEmpty) |
| 47 | So(err, ShouldEqual, ErrNotEnoughPorts) |
| 48 | }) |
| 49 | |
| 50 | Convey("port range start 0", t, func() { |
| 51 | ports, err := GetRandomPorts("127.0.0.1", 0, 65535, 1) |
| 52 | So(ports, ShouldHaveLength, 1) |
| 53 | So(err, ShouldBeNil) |
| 54 | }) |
| 55 | |
| 56 | Convey("port range error", t, func() { |
| 57 | ports, _ := GetRandomPorts("127.0.0.1", 0, 65535, 0) |
| 58 | So(ports, ShouldBeEmpty) |
| 59 | }) |
| 60 | |
| 61 | Convey("previously allocated should be unavailable", t, func() { |
| 62 | ports, err := GetRandomPorts("127.0.0.1", 1, 10000, 1) |
| 63 | So(ports, ShouldHaveLength, 1) |
| 64 | So(err, ShouldBeNil) |
| 65 | lastAllocated := ports[0] |
| 66 | ports, err = GetRandomPorts("127.0.0.1", lastAllocated, lastAllocated, 1) |
| 67 | So(err, ShouldNotBeNil) |
| 68 | // any address should be banned to allocate too |
| 69 | ports, err = GetRandomPorts("0.0.0.0", lastAllocated, lastAllocated, 1) |
| 70 | So(err, ShouldNotBeNil) |
| 71 | // but another bind address should be available, should be use 127.0.0.2, but mac does not support 127.0.0.2 |
| 72 | ports, err = GetRandomPorts("", lastAllocated, lastAllocated, 1) |
| 73 | So(err, ShouldBeNil) |
| 74 | So(ports, ShouldHaveLength, 1) |
| 75 | So(ports[0], ShouldEqual, lastAllocated) |
| 76 | }) |
| 77 | } |
| 78 | |
| 79 | func TestWaitForPorts(t *testing.T) { |
| 80 | Convey("test wait for ports", t, func() { |
nothing calls this directly
no test coverage detected