當前位置: 首頁>>代碼示例>>Golang>>正文


Golang gossip.NewTestRouter函數代碼示例

本文整理匯總了Golang中github.com/weaveworks/weave/testing/gossip.NewTestRouter函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewTestRouter函數的具體用法?Golang NewTestRouter怎麽用?Golang NewTestRouter使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewTestRouter函數的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestCancelOnDied

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")
	}
}
開發者ID:n054,項目名稱:weave,代碼行數:32,代碼來源:allocator_test.go

示例2: TestCancel

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")
	}
}
開發者ID:n054,項目名稱:weave,代碼行數:59,代碼來源:allocator_test.go

示例3: makeNetwork

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
}
開發者ID:brb,項目名稱:weave,代碼行數:14,代碼來源:nameserver_test.go

示例4: makeNetworkOfAllocators

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
}
開發者ID:kingbirdzheng,項目名稱:weave,代碼行數:18,代碼來源:testutils_test.go


注:本文中的github.com/weaveworks/weave/testing/gossip.NewTestRouter函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。