Skip to content

Commit

Permalink
Cover port_allocator_test with more conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangpengzhao committed Aug 23, 2016
1 parent 6e75fa9 commit 9ef7475
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions pkg/proxy/userspace/port_allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ func TestRangeAllocatorEmpty(t *testing.T) {
func TestRangeAllocatorFullyAllocated(t *testing.T) {
r := &net.PortRange{}
r.Set("1-1")
a := newPortRangeAllocator(*r)
pra := newPortRangeAllocator(*r)
a := pra.(*rangeAllocator)

p, err := a.AllocateNext()
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand All @@ -46,19 +48,39 @@ func TestRangeAllocatorFullyAllocated(t *testing.T) {
t.Fatalf("unexpected allocated port: %d", p)
}

a.lock.Lock()
if bit := a.used.Bit(p - a.Base); bit != 1 {
a.lock.Unlock()
t.Fatalf("unexpected used bit for allocated port: %d", p)
}
a.lock.Unlock()

_, err = a.AllocateNext()
if err == nil {
t.Fatalf("expected error because of fully-allocated range")
}

a.Release(p)
a.lock.Lock()
if bit := a.used.Bit(p - a.Base); bit != 0 {
a.lock.Unlock()
t.Fatalf("unexpected used bit for allocated port: %d", p)
}
a.lock.Unlock()

p, err = a.AllocateNext()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if p != 1 {
t.Fatalf("unexpected allocated port: %d", p)
}
a.lock.Lock()
if bit := a.used.Bit(p - a.Base); bit != 1 {
a.lock.Unlock()
t.Fatalf("unexpected used bit for allocated port: %d", p)
}
a.lock.Unlock()

_, err = a.AllocateNext()
if err == nil {
Expand All @@ -69,7 +91,8 @@ func TestRangeAllocatorFullyAllocated(t *testing.T) {
func TestRangeAllocator_RandomishAllocation(t *testing.T) {
r := &net.PortRange{}
r.Set("1-100")
a := newPortRangeAllocator(*r)
pra := newPortRangeAllocator(*r)
a := pra.(*rangeAllocator)

// allocate all the ports
var err error
Expand All @@ -79,11 +102,26 @@ func TestRangeAllocator_RandomishAllocation(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if ports[i] < 1 || ports[i] > 100 {
t.Fatalf("unexpected allocated port: %d", ports[i])
}
a.lock.Lock()
if bit := a.used.Bit(ports[i] - a.Base); bit != 1 {
a.lock.Unlock()
t.Fatalf("unexpected used bit for allocated port: %d", ports[i])
}
a.lock.Unlock()
}

// release them all
for i := 0; i < 100; i++ {
a.Release(ports[i])
a.lock.Lock()
if bit := a.used.Bit(ports[i] - a.Base); bit != 0 {
a.lock.Unlock()
t.Fatalf("unexpected used bit for allocated port: %d", ports[i])
}
a.lock.Unlock()
}

// allocate the ports again
Expand All @@ -93,6 +131,15 @@ func TestRangeAllocator_RandomishAllocation(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if rports[i] < 1 || rports[i] > 100 {
t.Fatalf("unexpected allocated port: %d", rports[i])
}
a.lock.Lock()
if bit := a.used.Bit(rports[i] - a.Base); bit != 1 {
a.lock.Unlock()
t.Fatalf("unexpected used bit for allocated port: %d", rports[i])
}
a.lock.Unlock()
}

if reflect.DeepEqual(ports, rports) {
Expand Down

0 comments on commit 9ef7475

Please sign in to comment.