wantlist_test.go 4.79 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3 4 5
package wantlist

import (
	"testing"

6 7
	pb "gitlab.dms3.io/dms3/go-bitswap/message/pb"
	cid "gitlab.dms3.io/dms3/go-cid"
Jeromy's avatar
Jeromy committed
8 9
)

10
var testcids []cid.Cid
Jeromy's avatar
Jeromy committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

func init() {
	strs := []string{
		"QmQL8LqkEgYXaDHdNYCG2mmpow7Sp8Z8Kt3QS688vyBeC7",
		"QmcBDsdjgSXU7BP4A4V8LJCXENE5xVwnhrhRGVTJr9YCVj",
		"QmQakgd2wDxc3uUF4orGdEm28zUT9Mmimp5pyPG2SFS9Gj",
	}
	for _, s := range strs {
		c, err := cid.Decode(s)
		if err != nil {
			panic(err)
		}
		testcids = append(testcids, c)
	}

}

type wli interface {
29
	Contains(cid.Cid) (Entry, bool)
Jeromy's avatar
Jeromy committed
30 31
}

32
func assertHasCid(t *testing.T, w wli, c cid.Cid) {
Jeromy's avatar
Jeromy committed
33 34 35 36 37 38 39 40 41 42 43 44
	e, ok := w.Contains(c)
	if !ok {
		t.Fatal("expected to have ", c)
	}
	if !e.Cid.Equals(c) {
		t.Fatal("returned entry had wrong cid value")
	}
}

func TestBasicWantlist(t *testing.T) {
	wl := New()

dirkmc's avatar
dirkmc committed
45
	if !wl.Add(testcids[0], 5, pb.Message_Wantlist_Block) {
46 47
		t.Fatal("expected true")
	}
Jeromy's avatar
Jeromy committed
48
	assertHasCid(t, wl, testcids[0])
dirkmc's avatar
dirkmc committed
49
	if !wl.Add(testcids[1], 4, pb.Message_Wantlist_Block) {
50 51
		t.Fatal("expected true")
	}
Jeromy's avatar
Jeromy committed
52 53 54 55 56 57 58
	assertHasCid(t, wl, testcids[0])
	assertHasCid(t, wl, testcids[1])

	if wl.Len() != 2 {
		t.Fatal("should have had two items")
	}

dirkmc's avatar
dirkmc committed
59
	if wl.Add(testcids[1], 4, pb.Message_Wantlist_Block) {
60 61
		t.Fatal("add shouldnt report success on second add")
	}
Jeromy's avatar
Jeromy committed
62 63 64 65 66 67 68
	assertHasCid(t, wl, testcids[0])
	assertHasCid(t, wl, testcids[1])

	if wl.Len() != 2 {
		t.Fatal("should have had two items")
	}

dirkmc's avatar
dirkmc committed
69
	if !wl.RemoveType(testcids[0], pb.Message_Wantlist_Block) {
70 71 72
		t.Fatal("should have gotten true")
	}

Jeromy's avatar
Jeromy committed
73 74 75 76 77 78
	assertHasCid(t, wl, testcids[1])
	if _, has := wl.Contains(testcids[0]); has {
		t.Fatal("shouldnt have this cid")
	}
}

dirkmc's avatar
dirkmc committed
79 80
func TestAddHaveThenBlock(t *testing.T) {
	wl := New()
Jeromy's avatar
Jeromy committed
81

dirkmc's avatar
dirkmc committed
82 83 84 85 86 87
	wl.Add(testcids[0], 5, pb.Message_Wantlist_Have)
	wl.Add(testcids[0], 5, pb.Message_Wantlist_Block)

	e, ok := wl.Contains(testcids[0])
	if !ok {
		t.Fatal("expected to have ", testcids[0])
88
	}
dirkmc's avatar
dirkmc committed
89 90
	if e.WantType != pb.Message_Wantlist_Block {
		t.Fatal("expected to be ", pb.Message_Wantlist_Block)
91
	}
dirkmc's avatar
dirkmc committed
92 93 94 95 96 97 98 99 100 101 102
}

func TestAddBlockThenHave(t *testing.T) {
	wl := New()

	wl.Add(testcids[0], 5, pb.Message_Wantlist_Block)
	wl.Add(testcids[0], 5, pb.Message_Wantlist_Have)

	e, ok := wl.Contains(testcids[0])
	if !ok {
		t.Fatal("expected to have ", testcids[0])
103
	}
dirkmc's avatar
dirkmc committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	if e.WantType != pb.Message_Wantlist_Block {
		t.Fatal("expected to be ", pb.Message_Wantlist_Block)
	}
}

func TestAddHaveThenRemoveBlock(t *testing.T) {
	wl := New()

	wl.Add(testcids[0], 5, pb.Message_Wantlist_Have)
	wl.RemoveType(testcids[0], pb.Message_Wantlist_Block)

	_, ok := wl.Contains(testcids[0])
	if ok {
		t.Fatal("expected not to have ", testcids[0])
	}
}

func TestAddBlockThenRemoveHave(t *testing.T) {
	wl := New()

	wl.Add(testcids[0], 5, pb.Message_Wantlist_Block)
	wl.RemoveType(testcids[0], pb.Message_Wantlist_Have)

	e, ok := wl.Contains(testcids[0])
	if !ok {
		t.Fatal("expected to have ", testcids[0])
	}
	if e.WantType != pb.Message_Wantlist_Block {
		t.Fatal("expected to be ", pb.Message_Wantlist_Block)
	}
}

func TestAddHaveThenRemoveAny(t *testing.T) {
	wl := New()

	wl.Add(testcids[0], 5, pb.Message_Wantlist_Have)
	wl.Remove(testcids[0])

	_, ok := wl.Contains(testcids[0])
	if ok {
		t.Fatal("expected not to have ", testcids[0])
	}
}

func TestAddBlockThenRemoveAny(t *testing.T) {
	wl := New()

	wl.Add(testcids[0], 5, pb.Message_Wantlist_Block)
	wl.Remove(testcids[0])

	_, ok := wl.Contains(testcids[0])
	if ok {
		t.Fatal("expected not to have ", testcids[0])
	}
}

func TestAbsort(t *testing.T) {
	wl := New()
	wl.Add(testcids[0], 5, pb.Message_Wantlist_Block)
	wl.Add(testcids[1], 4, pb.Message_Wantlist_Have)
	wl.Add(testcids[2], 3, pb.Message_Wantlist_Have)

	wl2 := New()
	wl2.Add(testcids[0], 2, pb.Message_Wantlist_Have)
	wl2.Add(testcids[1], 1, pb.Message_Wantlist_Block)

	wl.Absorb(wl2)

	e, ok := wl.Contains(testcids[0])
	if !ok {
		t.Fatal("expected to have ", testcids[0])
	}
	if e.Priority != 5 {
		t.Fatal("expected priority 5")
	}
	if e.WantType != pb.Message_Wantlist_Block {
		t.Fatal("expected type ", pb.Message_Wantlist_Block)
	}

	e, ok = wl.Contains(testcids[1])
	if !ok {
		t.Fatal("expected to have ", testcids[1])
	}
	if e.Priority != 1 {
		t.Fatal("expected priority 1")
	}
	if e.WantType != pb.Message_Wantlist_Block {
		t.Fatal("expected type ", pb.Message_Wantlist_Block)
	}

	e, ok = wl.Contains(testcids[2])
	if !ok {
		t.Fatal("expected to have ", testcids[2])
	}
	if e.Priority != 3 {
		t.Fatal("expected priority 3")
	}
	if e.WantType != pb.Message_Wantlist_Have {
		t.Fatal("expected type ", pb.Message_Wantlist_Have)
	}
}

206
func TestSortEntries(t *testing.T) {
dirkmc's avatar
dirkmc committed
207 208 209 210 211 212
	wl := New()

	wl.Add(testcids[0], 3, pb.Message_Wantlist_Block)
	wl.Add(testcids[1], 5, pb.Message_Wantlist_Have)
	wl.Add(testcids[2], 4, pb.Message_Wantlist_Have)

213 214 215
	entries := wl.Entries()
	SortEntries(entries)

dirkmc's avatar
dirkmc committed
216 217 218 219
	if !entries[0].Cid.Equals(testcids[1]) ||
		!entries[1].Cid.Equals(testcids[2]) ||
		!entries[2].Cid.Equals(testcids[0]) {
		t.Fatal("wrong order")
220
	}
Jeromy's avatar
Jeromy committed
221
}