blockpresencemanager_test.go 5.74 KB
Newer Older
dirkmc's avatar
dirkmc committed
1 2 3 4 5 6
package blockpresencemanager

import (
	"fmt"
	"testing"

7 8
	"gitlab.dms3.io/dms3/go-bitswap/internal/testutil"
	peer "gitlab.dms3.io/p2p/go-p2p-core/peer"
dirkmc's avatar
dirkmc committed
9

10
	cid "gitlab.dms3.io/dms3/go-cid"
dirkmc's avatar
dirkmc committed
11 12 13 14 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 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 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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
)

const (
	expHasFalseMsg         = "Expected PeerHasBlock to return false"
	expHasTrueMsg          = "Expected PeerHasBlock to return true"
	expDoesNotHaveFalseMsg = "Expected PeerDoesNotHaveBlock to return false"
	expDoesNotHaveTrueMsg  = "Expected PeerDoesNotHaveBlock to return true"
)

func TestBlockPresenceManager(t *testing.T) {
	bpm := New()

	p := testutil.GeneratePeers(1)[0]
	cids := testutil.GenerateCids(2)
	c0 := cids[0]
	c1 := cids[1]

	// Nothing stored yet, both PeerHasBlock and PeerDoesNotHaveBlock should
	// return false
	if bpm.PeerHasBlock(p, c0) {
		t.Fatal(expHasFalseMsg)
	}
	if bpm.PeerDoesNotHaveBlock(p, c0) {
		t.Fatal(expDoesNotHaveFalseMsg)
	}

	// HAVE cid0 / DONT_HAVE cid1
	bpm.ReceiveFrom(p, []cid.Cid{c0}, []cid.Cid{c1})

	// Peer has received HAVE for cid0
	if !bpm.PeerHasBlock(p, c0) {
		t.Fatal(expHasTrueMsg)
	}
	if bpm.PeerDoesNotHaveBlock(p, c0) {
		t.Fatal(expDoesNotHaveFalseMsg)
	}

	// Peer has received DONT_HAVE for cid1
	if !bpm.PeerDoesNotHaveBlock(p, c1) {
		t.Fatal(expDoesNotHaveTrueMsg)
	}
	if bpm.PeerHasBlock(p, c1) {
		t.Fatal(expHasFalseMsg)
	}

	// HAVE cid1 / DONT_HAVE cid0
	bpm.ReceiveFrom(p, []cid.Cid{c1}, []cid.Cid{c0})

	// DONT_HAVE cid0 should NOT over-write earlier HAVE cid0
	if bpm.PeerDoesNotHaveBlock(p, c0) {
		t.Fatal(expDoesNotHaveFalseMsg)
	}
	if !bpm.PeerHasBlock(p, c0) {
		t.Fatal(expHasTrueMsg)
	}

	// HAVE cid1 should over-write earlier DONT_HAVE cid1
	if !bpm.PeerHasBlock(p, c1) {
		t.Fatal(expHasTrueMsg)
	}
	if bpm.PeerDoesNotHaveBlock(p, c1) {
		t.Fatal(expDoesNotHaveFalseMsg)
	}

	// Remove cid0
	bpm.RemoveKeys([]cid.Cid{c0})

	// Nothing stored, both PeerHasBlock and PeerDoesNotHaveBlock should
	// return false
	if bpm.PeerHasBlock(p, c0) {
		t.Fatal(expHasFalseMsg)
	}
	if bpm.PeerDoesNotHaveBlock(p, c0) {
		t.Fatal(expDoesNotHaveFalseMsg)
	}

	// Remove cid1
	bpm.RemoveKeys([]cid.Cid{c1})

	// Nothing stored, both PeerHasBlock and PeerDoesNotHaveBlock should
	// return false
	if bpm.PeerHasBlock(p, c1) {
		t.Fatal(expHasFalseMsg)
	}
	if bpm.PeerDoesNotHaveBlock(p, c1) {
		t.Fatal(expDoesNotHaveFalseMsg)
	}
}

func TestAddRemoveMulti(t *testing.T) {
	bpm := New()

	peers := testutil.GeneratePeers(2)
	p0 := peers[0]
	p1 := peers[1]
	cids := testutil.GenerateCids(3)
	c0 := cids[0]
	c1 := cids[1]
	c2 := cids[2]

	// p0: HAVE cid0, cid1 / DONT_HAVE cid1, cid2
	// p1: HAVE cid1, cid2 / DONT_HAVE cid0
	bpm.ReceiveFrom(p0, []cid.Cid{c0, c1}, []cid.Cid{c1, c2})
	bpm.ReceiveFrom(p1, []cid.Cid{c1, c2}, []cid.Cid{c0})

	// Peer 0 should end up with
	// - HAVE cid0
	// - HAVE cid1
	// - DONT_HAVE cid2
	if !bpm.PeerHasBlock(p0, c0) {
		t.Fatal(expHasTrueMsg)
	}
	if !bpm.PeerHasBlock(p0, c1) {
		t.Fatal(expHasTrueMsg)
	}
	if !bpm.PeerDoesNotHaveBlock(p0, c2) {
		t.Fatal(expDoesNotHaveTrueMsg)
	}

	// Peer 1 should end up with
	// - HAVE cid1
	// - HAVE cid2
	// - DONT_HAVE cid0
	if !bpm.PeerHasBlock(p1, c1) {
		t.Fatal(expHasTrueMsg)
	}
	if !bpm.PeerHasBlock(p1, c2) {
		t.Fatal(expHasTrueMsg)
	}
	if !bpm.PeerDoesNotHaveBlock(p1, c0) {
		t.Fatal(expDoesNotHaveTrueMsg)
	}

	// Remove cid1 and cid2. Should end up with
	// Peer 0: HAVE cid0
	// Peer 1: DONT_HAVE cid0
	bpm.RemoveKeys([]cid.Cid{c1, c2})
	if !bpm.PeerHasBlock(p0, c0) {
		t.Fatal(expHasTrueMsg)
	}
	if !bpm.PeerDoesNotHaveBlock(p1, c0) {
		t.Fatal(expDoesNotHaveTrueMsg)
	}

	// The other keys should have been cleared, so both HasBlock() and
	// DoesNotHaveBlock() should return false
	if bpm.PeerHasBlock(p0, c1) {
		t.Fatal(expHasFalseMsg)
	}
	if bpm.PeerDoesNotHaveBlock(p0, c1) {
		t.Fatal(expDoesNotHaveFalseMsg)
	}
	if bpm.PeerHasBlock(p0, c2) {
		t.Fatal(expHasFalseMsg)
	}
	if bpm.PeerDoesNotHaveBlock(p0, c2) {
		t.Fatal(expDoesNotHaveFalseMsg)
	}
	if bpm.PeerHasBlock(p1, c1) {
		t.Fatal(expHasFalseMsg)
	}
	if bpm.PeerDoesNotHaveBlock(p1, c1) {
		t.Fatal(expDoesNotHaveFalseMsg)
	}
	if bpm.PeerHasBlock(p1, c2) {
		t.Fatal(expHasFalseMsg)
	}
	if bpm.PeerDoesNotHaveBlock(p1, c2) {
		t.Fatal(expDoesNotHaveFalseMsg)
	}
}

func TestAllPeersDoNotHaveBlock(t *testing.T) {
	bpm := New()

	peers := testutil.GeneratePeers(3)
	p0 := peers[0]
	p1 := peers[1]
	p2 := peers[2]

	cids := testutil.GenerateCids(3)
	c0 := cids[0]
	c1 := cids[1]
	c2 := cids[2]

	//      c0  c1  c2
	//  p0   ?  N   N
	//  p1   N  Y   ?
	//  p2   Y  Y   N
	bpm.ReceiveFrom(p0, []cid.Cid{}, []cid.Cid{c1, c2})
	bpm.ReceiveFrom(p1, []cid.Cid{c1}, []cid.Cid{c0})
	bpm.ReceiveFrom(p2, []cid.Cid{c0, c1}, []cid.Cid{c2})

	type testcase struct {
		peers []peer.ID
		ks    []cid.Cid
		exp   []cid.Cid
	}

	testcases := []testcase{
		testcase{[]peer.ID{p0}, []cid.Cid{c0}, []cid.Cid{}},
		testcase{[]peer.ID{p1}, []cid.Cid{c0}, []cid.Cid{c0}},
		testcase{[]peer.ID{p2}, []cid.Cid{c0}, []cid.Cid{}},

		testcase{[]peer.ID{p0}, []cid.Cid{c1}, []cid.Cid{c1}},
		testcase{[]peer.ID{p1}, []cid.Cid{c1}, []cid.Cid{}},
		testcase{[]peer.ID{p2}, []cid.Cid{c1}, []cid.Cid{}},

		testcase{[]peer.ID{p0}, []cid.Cid{c2}, []cid.Cid{c2}},
		testcase{[]peer.ID{p1}, []cid.Cid{c2}, []cid.Cid{}},
		testcase{[]peer.ID{p2}, []cid.Cid{c2}, []cid.Cid{c2}},

		// p0 recieved DONT_HAVE for c1 & c2 (but not for c0)
		testcase{[]peer.ID{p0}, []cid.Cid{c0, c1, c2}, []cid.Cid{c1, c2}},
		testcase{[]peer.ID{p0, p1}, []cid.Cid{c0, c1, c2}, []cid.Cid{}},
		// Both p0 and p2 received DONT_HAVE for c2
		testcase{[]peer.ID{p0, p2}, []cid.Cid{c0, c1, c2}, []cid.Cid{c2}},
		testcase{[]peer.ID{p0, p1, p2}, []cid.Cid{c0, c1, c2}, []cid.Cid{}},
	}

	for i, tc := range testcases {
		if !testutil.MatchKeysIgnoreOrder(
			bpm.AllPeersDoNotHaveBlock(tc.peers, tc.ks),
			tc.exp,
		) {
			t.Fatal(fmt.Sprintf("test case %d failed: expected matching keys", i))
		}
	}
}