Add fuzz-test for Claim
This commit is contained in:
1 parent
c3c40b15f1
commit
4c5920a50a
1 file changed
+45
-15
+45
-15
@@ -348,27 +348,18 @@ func TestAllocatorFuzz(t *testing.T) {
|
|||||||
return xs[:ls]
|
return xs[:ls]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do a Allocate and check the address
|
bumpPending := func() bool {
|
||||||
// is unique. Needs a unique container
|
|
||||||
// name.
|
|
||||||
allocate := func(name string) {
|
|
||||||
stateLock.Lock()
|
stateLock.Lock()
|
||||||
if len(addrs)+numPending >= maxAddresses {
|
if len(addrs)+numPending >= maxAddresses {
|
||||||
stateLock.Unlock()
|
stateLock.Unlock()
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
numPending++
|
numPending++
|
||||||
stateLock.Unlock()
|
stateLock.Unlock()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
allocIndex := rand.Int31n(nodes)
|
noteAllocation := func(allocIndex int32, name string, addr address.Address) {
|
||||||
alloc := allocs[allocIndex]
|
|
||||||
//common.Log.Infof("Allocate: asking allocator %d", allocIndex)
|
|
||||||
addr, err := alloc.Allocate(name, subnet, nil)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintf("Could not allocate addr"))
|
|
||||||
}
|
|
||||||
|
|
||||||
//common.Log.Infof("Allocate: got address %s for name %s", addr, name)
|
//common.Log.Infof("Allocate: got address %s for name %s", addr, name)
|
||||||
addrStr := addr.String()
|
addrStr := addr.String()
|
||||||
|
|
||||||
@@ -385,6 +376,26 @@ func TestAllocatorFuzz(t *testing.T) {
|
|||||||
numPending--
|
numPending--
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Do a Allocate and check the address
|
||||||
|
// is unique. Needs a unique container
|
||||||
|
// name.
|
||||||
|
allocate := func(name string) {
|
||||||
|
if !bumpPending() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
allocIndex := rand.Int31n(nodes)
|
||||||
|
alloc := allocs[allocIndex]
|
||||||
|
//common.Log.Infof("Allocate: asking allocator %d", allocIndex)
|
||||||
|
addr, err := alloc.Allocate(name, subnet, nil)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("Could not allocate addr"))
|
||||||
|
}
|
||||||
|
|
||||||
|
noteAllocation(allocIndex, name, addr)
|
||||||
|
}
|
||||||
|
|
||||||
// Free a random address.
|
// Free a random address.
|
||||||
free := func() {
|
free := func() {
|
||||||
stateLock.Lock()
|
stateLock.Lock()
|
||||||
@@ -445,6 +456,21 @@ func TestAllocatorFuzz(t *testing.T) {
|
|||||||
stateLock.Unlock()
|
stateLock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Claim a random address for a unique container name - may not succeed
|
||||||
|
claim := func(name string) {
|
||||||
|
if !bumpPending() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
allocIndex := rand.Int31n(nodes)
|
||||||
|
addressIndex := rand.Int31n(int32(subnet.Size()))
|
||||||
|
alloc := allocs[allocIndex]
|
||||||
|
addr := address.Add(subnet.Start, address.Offset(addressIndex))
|
||||||
|
err := alloc.Claim(name, addr)
|
||||||
|
if err == nil {
|
||||||
|
noteAllocation(allocIndex, name, addr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Run function _f_ _iterations_ times, in _concurrency_
|
// Run function _f_ _iterations_ times, in _concurrency_
|
||||||
// number of goroutines
|
// number of goroutines
|
||||||
doConcurrentIterations := func(iterations int, f func(int)) {
|
doConcurrentIterations := func(iterations int, f func(int)) {
|
||||||
@@ -484,9 +510,13 @@ func TestAllocatorFuzz(t *testing.T) {
|
|||||||
// free a random addr
|
// free a random addr
|
||||||
free()
|
free()
|
||||||
|
|
||||||
case 0.8 <= r && r < 1.0:
|
case 0.8 <= r && r < 0.95:
|
||||||
// ask for an existing name again, check we get same ip
|
// ask for an existing name again, check we get same ip
|
||||||
allocateAgain()
|
allocateAgain()
|
||||||
|
|
||||||
|
case 0.95 <= r && r < 1.0:
|
||||||
|
name := fmt.Sprintf("second%d", iteration)
|
||||||
|
claim(name)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user