peer_request_queue_test.go 3.11 KB
Newer Older
Brian Tiger Chow's avatar
Brian Tiger Chow committed
1 2 3
package decision

import (
4
	"fmt"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
5 6 7 8 9 10
	"math"
	"math/rand"
	"sort"
	"strings"
	"testing"

11
	"github.com/ipfs/go-ipfs/exchange/bitswap/wantlist"
Steven Allen's avatar
Steven Allen committed
12 13 14
	u "gx/ipfs/QmPsAfmDBnZN3kZGSuNwvCNDZiHneERSKmRcFyG3UkvcT3/go-ipfs-util"
	"gx/ipfs/QmeDA8gNhvRTsbrjEieay5wezupJDiky8xvCzDABbsGzmp/go-testutil"
	cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
)

func TestPushPop(t *testing.T) {
	prq := newPRQ()
	partner := testutil.RandPeerIDFatal(t)
	alphabet := strings.Split("abcdefghijklmnopqrstuvwxyz", "")
	vowels := strings.Split("aeiou", "")
	consonants := func() []string {
		var out []string
		for _, letter := range alphabet {
			skip := false
			for _, vowel := range vowels {
				if letter == vowel {
					skip = true
				}
			}
			if !skip {
				out = append(out, letter)
			}
		}
		return out
	}()
	sort.Strings(alphabet)
	sort.Strings(vowels)
	sort.Strings(consonants)

	// add a bunch of blocks. cancel some. drain the queue. the queue should only have the kept entries

	for _, index := range rand.Perm(len(alphabet)) { // add blocks for all letters
		letter := alphabet[index]
		t.Log(partner.String())
46 47 48

		c := cid.NewCidV0(u.Hash([]byte(letter)))
		prq.Push(&wantlist.Entry{Cid: c, Priority: math.MaxInt32 - index}, partner)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
49 50
	}
	for _, consonant := range consonants {
51 52
		c := cid.NewCidV0(u.Hash([]byte(consonant)))
		prq.Remove(c, partner)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
53 54
	}

Jeromy's avatar
Jeromy committed
55 56
	prq.fullThaw()

57 58 59 60 61
	var out []string
	for {
		received := prq.Pop()
		if received == nil {
			break
Brian Tiger Chow's avatar
Brian Tiger Chow committed
62
		}
63

64
		out = append(out, received.Entry.Cid.String())
65 66 67 68
	}

	// Entries popped should already be in correct order
	for i, expected := range vowels {
69 70
		exp := cid.NewCidV0(u.Hash([]byte(expected))).String()
		if out[i] != exp {
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
			t.Fatal("received", out[i], "expected", expected)
		}
	}
}

// This test checks that peers wont starve out other peers
func TestPeerRepeats(t *testing.T) {
	prq := newPRQ()
	a := testutil.RandPeerIDFatal(t)
	b := testutil.RandPeerIDFatal(t)
	c := testutil.RandPeerIDFatal(t)
	d := testutil.RandPeerIDFatal(t)

	// Have each push some blocks

	for i := 0; i < 5; i++ {
87 88 89 90 91
		elcid := cid.NewCidV0(u.Hash([]byte(fmt.Sprint(i))))
		prq.Push(&wantlist.Entry{Cid: elcid}, a)
		prq.Push(&wantlist.Entry{Cid: elcid}, b)
		prq.Push(&wantlist.Entry{Cid: elcid}, c)
		prq.Push(&wantlist.Entry{Cid: elcid}, d)
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	}

	// now, pop off four entries, there should be one from each
	var targets []string
	var tasks []*peerRequestTask
	for i := 0; i < 4; i++ {
		t := prq.Pop()
		targets = append(targets, t.Target.Pretty())
		tasks = append(tasks, t)
	}

	expected := []string{a.Pretty(), b.Pretty(), c.Pretty(), d.Pretty()}
	sort.Strings(expected)
	sort.Strings(targets)

	t.Log(targets)
	t.Log(expected)
	for i, s := range targets {
		if expected[i] != s {
			t.Fatal("unexpected peer", s, expected[i])
		}
	}

	// Now, if one of the tasks gets finished, the next task off the queue should
	// be for the same peer
Jeromy's avatar
Jeromy committed
117 118 119 120 121 122 123 124 125 126
	for blockI := 0; blockI < 4; blockI++ {
		for i := 0; i < 4; i++ {
			// its okay to mark the same task done multiple times here (JUST FOR TESTING)
			tasks[i].Done()

			ntask := prq.Pop()
			if ntask.Target != tasks[i].Target {
				t.Fatal("Expected task from peer with lowest active count")
			}
		}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
127 128
	}
}