Exemple #1
0
func init() {
	pullInterval = time.Duration(500) * time.Millisecond
	algo.SetDigestWaitTime(pullInterval / 5)
	algo.SetRequestWaitTime(pullInterval)
	algo.SetResponseWaitTime(pullInterval)
}
Exemple #2
0
func TestPull(t *testing.T) {
	t1 := time.Now()
	// Scenario: Turn off forwarding and use only pull-based gossip.
	// First phase: Ensure full membership view for all nodes
	// Second phase: Disseminate 10 messages and ensure all nodes got them

	testLock.Lock()
	defer testLock.Unlock()

	shortenedWaitTime := time.Duration(500) * time.Millisecond
	algo.SetDigestWaitTime(shortenedWaitTime / 5)
	algo.SetRequestWaitTime(shortenedWaitTime)
	algo.SetResponseWaitTime(shortenedWaitTime)

	defer func() {
		algo.SetDigestWaitTime(time.Duration(1) * time.Second)
		algo.SetRequestWaitTime(time.Duration(1) * time.Second)
		algo.SetResponseWaitTime(time.Duration(2) * time.Second)
	}()

	stopped := int32(0)
	go waitForTestCompletion(&stopped, t)

	n := 5
	msgsCount2Send := 10
	boot := newGossipInstanceWithOnlyPull(0, 100)
	peers := make([]Gossip, n)
	wg := sync.WaitGroup{}
	for i := 1; i <= n; i++ {
		wg.Add(1)
		go func(i int) {
			pI := newGossipInstanceWithOnlyPull(i, 100, 0)
			peers[i-1] = pI
			wg.Done()
		}(i)
	}
	wg.Wait()

	knowAll := func() bool {
		for i := 1; i <= n; i++ {
			neighborCount := len(peers[i-1].GetPeers())
			if n != neighborCount {
				return false
			}
		}
		return true
	}

	waitUntilOrFail(t, knowAll)

	receivedMessages := make([]int, n)
	wg = sync.WaitGroup{}
	for i := 1; i <= n; i++ {
		go func(i int) {
			acceptChan, _ := peers[i-1].Accept(acceptData, false)
			go func(index int, ch <-chan *proto.GossipMessage) {
				wg.Add(1)
				defer wg.Done()
				for j := 0; j < msgsCount2Send; j++ {
					<-ch
					receivedMessages[index]++
				}
			}(i-1, acceptChan)
		}(i)
	}

	for i := 1; i <= msgsCount2Send; i++ {
		boot.Gossip(createDataMsg(uint64(i), []byte{}, ""))
	}
	time.Sleep(time.Duration(3) * time.Second)

	waitUntilOrFailBlocking(t, wg.Wait)

	receivedAll := func() bool {
		for i := 0; i < n; i++ {
			if msgsCount2Send != receivedMessages[i] {
				return false
			}
		}
		return true
	}
	waitUntilOrFail(t, receivedAll)

	stop := func() {
		stopPeers(append(peers, boot))
	}

	waitUntilOrFailBlocking(t, stop)

	fmt.Println("Took", time.Since(t1))
	atomic.StoreInt32(&stopped, int32(1))
	ensureGoroutineExit(t)
}