peer_request_queue_test.go 3.04 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"

Jeromy's avatar
Jeromy committed
11 12 13 14
	"github.com/ipfs/go-bitswap/wantlist"
	cid "github.com/ipfs/go-cid"
	u "github.com/ipfs/go-ipfs-util"
	"github.com/libp2p/go-testutil"
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

		c := cid.NewCidV0(u.Hash([]byte(letter)))
48
		prq.Push(partner, &wantlist.Entry{Cid: c, Priority: math.MaxInt32 - index})
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 65 66
		for _, entry := range received.Entries {
			out = append(out, entry.Cid.String())
		}
67 68 69 70
	}

	// Entries popped should already be in correct order
	for i, expected := range vowels {
71 72
		exp := cid.NewCidV0(u.Hash([]byte(expected))).String()
		if out[i] != exp {
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
			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++ {
89
		elcid := cid.NewCidV0(u.Hash([]byte(fmt.Sprint(i))))
90 91 92 93
		prq.Push(a, &wantlist.Entry{Cid: elcid})
		prq.Push(b, &wantlist.Entry{Cid: elcid})
		prq.Push(c, &wantlist.Entry{Cid: elcid})
		prq.Push(d, &wantlist.Entry{Cid: elcid})
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	}

	// 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
119 120 121
	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)
122
			tasks[i].Done(tasks[i].Entries)
Jeromy's avatar
Jeromy committed
123 124 125 126 127 128

			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
129 130
	}
}