Merge pull request #8671 from shuai-z/master

Port number 49153(BeginPortRange) would be returned twice, causing dupli...
This commit is contained in:
Michael Crosby 2014-10-23 17:37:38 -07:00
commit 0e6242122d
2 changed files with 18 additions and 7 deletions

View File

@ -14,7 +14,8 @@ type portMap struct {
func newPortMap() *portMap {
return &portMap{
p: map[int]struct{}{},
p: map[int]struct{}{},
last: EndPortRange,
}
}
@ -135,12 +136,6 @@ func ReleaseAll() error {
}
func (pm *portMap) findPort() (int, error) {
if pm.last == 0 {
pm.p[BeginPortRange] = struct{}{}
pm.last = BeginPortRange
return BeginPortRange, nil
}
for port := pm.last + 1; port != pm.last; port++ {
if port > EndPortRange {
port = BeginPortRange

View File

@ -214,3 +214,19 @@ func TestPortAllocation(t *testing.T) {
t.Fatal("Requesting a dynamic port should never allocate a used port")
}
}
func TestNoDuplicateBPR(t *testing.T) {
defer reset()
if port, err := RequestPort(defaultIP, "tcp", BeginPortRange); err != nil {
t.Fatal(err)
} else if port != BeginPortRange {
t.Fatalf("Expected port %d got %d", BeginPortRange, port)
}
if port, err := RequestPort(defaultIP, "tcp", 0); err != nil {
t.Fatal(err)
} else if port == BeginPortRange {
t.Fatalf("Acquire(0) allocated the same port twice: %d", port)
}
}