Commit 08c0f7dd authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #27711 from xiangpengzhao/port-allocator-test

Automatic merge from submit-queue Cover port_allocator_test with more conditions The test cases of port_allocator_test should cover more conditions, such as `rangeAllocator.used.Bit`.
parents 2c7e1317 9ef7475c
...@@ -37,7 +37,9 @@ func TestRangeAllocatorEmpty(t *testing.T) { ...@@ -37,7 +37,9 @@ func TestRangeAllocatorEmpty(t *testing.T) {
func TestRangeAllocatorFullyAllocated(t *testing.T) { func TestRangeAllocatorFullyAllocated(t *testing.T) {
r := &net.PortRange{} r := &net.PortRange{}
r.Set("1-1") r.Set("1-1")
a := newPortRangeAllocator(*r) pra := newPortRangeAllocator(*r)
a := pra.(*rangeAllocator)
p, err := a.AllocateNext() p, err := a.AllocateNext()
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
...@@ -46,12 +48,26 @@ func TestRangeAllocatorFullyAllocated(t *testing.T) { ...@@ -46,12 +48,26 @@ func TestRangeAllocatorFullyAllocated(t *testing.T) {
t.Fatalf("unexpected allocated port: %d", p) 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() _, err = a.AllocateNext()
if err == nil { if err == nil {
t.Fatalf("expected error because of fully-allocated range") t.Fatalf("expected error because of fully-allocated range")
} }
a.Release(p) 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() p, err = a.AllocateNext()
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
...@@ -59,6 +75,12 @@ func TestRangeAllocatorFullyAllocated(t *testing.T) { ...@@ -59,6 +75,12 @@ func TestRangeAllocatorFullyAllocated(t *testing.T) {
if p != 1 { if p != 1 {
t.Fatalf("unexpected allocated port: %d", p) 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() _, err = a.AllocateNext()
if err == nil { if err == nil {
...@@ -69,7 +91,8 @@ func TestRangeAllocatorFullyAllocated(t *testing.T) { ...@@ -69,7 +91,8 @@ func TestRangeAllocatorFullyAllocated(t *testing.T) {
func TestRangeAllocator_RandomishAllocation(t *testing.T) { func TestRangeAllocator_RandomishAllocation(t *testing.T) {
r := &net.PortRange{} r := &net.PortRange{}
r.Set("1-100") r.Set("1-100")
a := newPortRangeAllocator(*r) pra := newPortRangeAllocator(*r)
a := pra.(*rangeAllocator)
// allocate all the ports // allocate all the ports
var err error var err error
...@@ -79,11 +102,26 @@ func TestRangeAllocator_RandomishAllocation(t *testing.T) { ...@@ -79,11 +102,26 @@ func TestRangeAllocator_RandomishAllocation(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) 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 // release them all
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
a.Release(ports[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 // allocate the ports again
...@@ -93,6 +131,15 @@ func TestRangeAllocator_RandomishAllocation(t *testing.T) { ...@@ -93,6 +131,15 @@ func TestRangeAllocator_RandomishAllocation(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) 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) { if reflect.DeepEqual(ports, rports) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment