func TestCancelOnDied(t *testing.T) { const ( cidr = "10.0.4.0/26" container1 = "abcdef" ) router := gossip.NewTestRouter(0.0) alloc1, subnet := makeAllocator("01:00:00:02:00:00", cidr, 2) alloc1.SetInterfaces(router.Connect(alloc1.ourName, alloc1)) alloc1.Start() doneChan := make(chan bool) f := func() { _, ok := alloc1.Allocate(container1, subnet, true, returnFalse) doneChan <- ok == nil } // Attempt two allocations in parallel, to check that this is handled correctly go f() go f() // Nothing should happen, because we declared the quorum as 2 time.Sleep(100 * time.Millisecond) AssertNothingSent(t, doneChan) alloc1.ContainerDied(container1) // Check that the two allocations both exit with an error if <-doneChan || <-doneChan { require.FailNow(t, "Error: got result from Allocate") } }
func TestCancel(t *testing.T) { const cidr = "10.0.4.0/26" router := gossip.NewTestRouter(0.0) alloc1, subnet := makeAllocator("01:00:00:02:00:00", cidr, 2) alloc1.SetInterfaces(router.Connect(alloc1.ourName, alloc1)) alloc2, _ := makeAllocator("02:00:00:02:00:00", cidr, 2) alloc2.SetInterfaces(router.Connect(alloc2.ourName, alloc2)) alloc1.claimRingForTesting(alloc1, alloc2) alloc2.claimRingForTesting(alloc1, alloc2) alloc1.Start() alloc2.Start() // tell peers about each other alloc1.OnGossipBroadcast(alloc2.ourName, alloc2.Encode()) // Get some IPs, so each allocator has some space res1, _ := alloc1.Allocate("foo", subnet, true, returnFalse) common.Log.Debugf("res1 = %s", res1.String()) res2, _ := alloc2.Allocate("bar", subnet, true, returnFalse) common.Log.Debugf("res2 = %s", res2.String()) if res1 == res2 { require.FailNow(t, "Error: got same ips!") } // Now we're going to pause alloc2 and ask alloc1 // for an allocation unpause := alloc2.pause() // Use up all the IPs that alloc1 owns, so the allocation after this will prompt a request to alloc2 for i := 0; alloc1.NumFreeAddresses(subnet.HostRange()) > 0; i++ { alloc1.Allocate(fmt.Sprintf("tmp%d", i), subnet, true, returnFalse) } cancelChan := make(chan bool, 1) doneChan := make(chan bool) go func() { _, ok := alloc1.Allocate("baz", subnet, true, func() bool { select { case <-cancelChan: return true default: return false } }) doneChan <- ok == nil }() time.Sleep(100 * time.Millisecond) AssertNothingSent(t, doneChan) cancelChan <- true unpause() if <-doneChan { require.FailNow(t, "Error: got result from Allocate") } }
func makeNetwork(size int) ([]*Nameserver, *gossip.TestRouter) { gossipRouter := gossip.NewTestRouter(0.0) nameservers := make([]*Nameserver, size) for i := 0; i < size; i++ { name, _ := mesh.PeerNameFromString(fmt.Sprintf("%02d:00:00:02:00:00", i)) nameserver := makeNameserver(name) nameserver.SetGossip(gossipRouter.Connect(nameserver.ourName, nameserver)) nameserver.Start() nameservers[i] = nameserver } return nameservers, gossipRouter }
func makeNetworkOfAllocators(size int, cidr string) ([]*Allocator, *gossip.TestRouter, address.Range) { gossipRouter := gossip.NewTestRouter(0.0) allocs := make([]*Allocator, size) var subnet address.Range for i := 0; i < size; i++ { var alloc *Allocator alloc, subnet = makeAllocator(fmt.Sprintf("%02d:00:00:02:00:00", i), cidr, uint(size/2+1)) alloc.SetInterfaces(gossipRouter.Connect(alloc.ourName, alloc)) alloc.Start() allocs[i] = alloc } allocs[size-1].gossip.GossipBroadcast(allocs[size-1].Gossip()) gossipRouter.Flush() return allocs, gossipRouter, subnet }