sessionwants_test.go 4.36 KB
Newer Older
1 2 3 4 5
package session

import (
	"testing"

6 7
	"gitlab.dms3.io/dms3/go-bitswap/internal/testutil"
	cid "gitlab.dms3.io/dms3/go-cid"
8 9
)

dirkmc's avatar
dirkmc committed
10
func TestEmptySessionWants(t *testing.T) {
11
	sw := newSessionWants(broadcastLiveWantsLimit)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

	// Expect these functions to return nothing on a new sessionWants
	lws := sw.PrepareBroadcast()
	if len(lws) > 0 {
		t.Fatal("expected no broadcast wants")
	}
	lws = sw.LiveWants()
	if len(lws) > 0 {
		t.Fatal("expected no live wants")
	}
	if sw.HasLiveWants() {
		t.Fatal("expected not to have live wants")
	}
	rw := sw.RandomLiveWant()
	if rw.Defined() {
		t.Fatal("expected no random want")
	}
dirkmc's avatar
dirkmc committed
29
}
30

dirkmc's avatar
dirkmc committed
31
func TestSessionWants(t *testing.T) {
32
	sw := newSessionWants(5)
dirkmc's avatar
dirkmc committed
33 34 35 36 37 38 39 40 41 42 43
	cids := testutil.GenerateCids(10)
	others := testutil.GenerateCids(1)

	// Add 10 new wants
	//  toFetch    Live
	// 9876543210
	sw.BlocksRequested(cids)

	// Get next wants with a limit of 5
	// The first 5 cids should go move into the live queue
	//  toFetch   Live
44
	//   98765    43210
45
	nextw := sw.GetNextWants()
46 47 48
	if len(nextw) != 5 {
		t.Fatal("expected 5 next wants")
	}
dirkmc's avatar
dirkmc committed
49
	lws := sw.PrepareBroadcast()
50
	if len(lws) != 5 {
dirkmc's avatar
dirkmc committed
51
		t.Fatal("expected 5 broadcast wants", len(lws))
52 53 54 55 56 57 58 59
	}
	lws = sw.LiveWants()
	if len(lws) != 5 {
		t.Fatal("expected 5 live wants")
	}
	if !sw.HasLiveWants() {
		t.Fatal("expected to have live wants")
	}
dirkmc's avatar
dirkmc committed
60
	rw := sw.RandomLiveWant()
61 62 63 64 65
	if !rw.Defined() {
		t.Fatal("expected random want")
	}

	// Two wanted blocks and one other block are received.
dirkmc's avatar
dirkmc committed
66 67 68 69
	// The wanted blocks should be removed from the live wants queue
	// (the other block CID should be ignored)
	//  toFetch   Live
	//   98765    432__
70 71 72 73 74 75 76 77 78
	recvdCids := []cid.Cid{cids[0], cids[1], others[0]}
	sw.BlocksReceived(recvdCids)
	lws = sw.LiveWants()
	if len(lws) != 3 {
		t.Fatal("expected 3 live wants")
	}

	// Ask for next wants with a limit of 5
	// Should move 2 wants from toFetch queue to live wants
dirkmc's avatar
dirkmc committed
79 80
	//  toFetch   Live
	//   987__    65432
81
	nextw = sw.GetNextWants()
82 83 84 85 86 87 88 89 90
	if len(nextw) != 2 {
		t.Fatal("expected 2 next wants")
	}
	lws = sw.LiveWants()
	if len(lws) != 5 {
		t.Fatal("expected 5 live wants")
	}

	// One wanted block and one dup block are received.
dirkmc's avatar
dirkmc committed
91 92 93 94
	// The wanted block should be removed from the live
	// wants queue.
	//  toFetch   Live
	//   987      654_2
95 96 97 98 99 100 101 102
	recvdCids = []cid.Cid{cids[0], cids[3]}
	sw.BlocksReceived(recvdCids)
	lws = sw.LiveWants()
	if len(lws) != 4 {
		t.Fatal("expected 4 live wants")
	}

	// One block in the toFetch queue should be cancelled
dirkmc's avatar
dirkmc committed
103 104
	//  toFetch   Live
	//   9_7      654_2
105 106 107 108 109 110
	sw.CancelPending([]cid.Cid{cids[8]})
	lws = sw.LiveWants()
	if len(lws) != 4 {
		t.Fatal("expected 4 live wants")
	}
}
111 112 113 114 115 116 117 118

func TestPrepareBroadcast(t *testing.T) {
	sw := newSessionWants(3)
	cids := testutil.GenerateCids(10)

	// Add 6 new wants
	//  toFetch    Live
	//  543210
119
	sw.BlocksRequested(cids[:6])
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

	// Get next wants with a limit of 3
	// The first 3 cids should go move into the live queue
	//  toFetch   Live
	//  543       210
	sw.GetNextWants()

	// Broadcast should contain wants in order
	for i := 0; i < 10; i++ {
		ws := sw.PrepareBroadcast()
		if len(ws) != 3 {
			t.Fatal("should broadcast all live wants")
		}
		for idx, c := range ws {
			if !c.Equals(cids[idx]) {
				t.Fatal("broadcast should always return wants in order")
			}
		}
	}

	// One block received
	// Remove a cid from the live queue
142
	sw.BlocksReceived(cids[:1])
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
	//  toFetch    Live
	//  543        21_

	// Add 4 new wants
	//  toFetch    Live
	//  9876543    21
	sw.BlocksRequested(cids[6:])

	// 2 Wants sent
	//  toFetch    Live
	//  98765      4321
	sw.WantsSent(cids[3:5])

	// Broadcast should contain wants in order
	cids = cids[1:]
	for i := 0; i < 10; i++ {
		ws := sw.PrepareBroadcast()
		if len(ws) != 3 {
			t.Fatal("should broadcast live wants up to limit", len(ws), len(cids))
		}
		for idx, c := range ws {
			if !c.Equals(cids[idx]) {
				t.Fatal("broadcast should always return wants in order")
			}
		}
	}
}
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

// Test that even after GC broadcast returns correct wants
func TestPrepareBroadcastAfterGC(t *testing.T) {
	sw := newSessionWants(5)
	cids := testutil.GenerateCids(liveWantsOrderGCLimit * 2)

	sw.BlocksRequested(cids)

	// Trigger a sessionWants internal GC of the live wants
	sw.BlocksReceived(cids[:liveWantsOrderGCLimit+1])
	cids = cids[:liveWantsOrderGCLimit+1]

	// Broadcast should contain wants in order
	ws := sw.PrepareBroadcast()
	for i, c := range ws {
		if !c.Equals(cids[i]) {
			t.Fatal("broadcast should always return wants in order")
		}
	}
}