sessionpeermanager_test.go 5.61 KB
Newer Older
1 2 3
package sessionpeermanager

import (
4
	"sync"
5 6
	"testing"

7
	"github.com/ipfs/go-bitswap/internal/testutil"
Raúl Kripalani's avatar
Raúl Kripalani committed
8
	peer "github.com/libp2p/go-libp2p-core/peer"
9 10
)

11
type fakePeerTagger struct {
12
	lk          sync.Mutex
13
	taggedPeers []peer.ID
14
	wait        sync.WaitGroup
15 16
}

17 18
func (fpt *fakePeerTagger) TagPeer(p peer.ID, tag string, n int) {
	fpt.wait.Add(1)
19 20 21

	fpt.lk.Lock()
	defer fpt.lk.Unlock()
22
	fpt.taggedPeers = append(fpt.taggedPeers, p)
23
}
24

25 26 27
func (fpt *fakePeerTagger) UntagPeer(p peer.ID, tag string) {
	defer fpt.wait.Done()

28 29
	fpt.lk.Lock()
	defer fpt.lk.Unlock()
30 31 32 33
	for i := 0; i < len(fpt.taggedPeers); i++ {
		if fpt.taggedPeers[i] == p {
			fpt.taggedPeers[i] = fpt.taggedPeers[len(fpt.taggedPeers)-1]
			fpt.taggedPeers = fpt.taggedPeers[:len(fpt.taggedPeers)-1]
34 35 36 37
			return
		}
	}
}
38

39 40 41 42 43 44
func (fpt *fakePeerTagger) count() int {
	fpt.lk.Lock()
	defer fpt.lk.Unlock()
	return len(fpt.taggedPeers)
}

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
// func TestFindingMorePeers(t *testing.T) {
// 	ctx := context.Background()
// 	ctx, cancel := context.WithCancel(ctx)
// 	defer cancel()
// 	completed := make(chan struct{})

// 	peers := testutil.GeneratePeers(5)
// 	fpt := &fakePeerTagger{}
// 	fppf := &fakePeerProviderFinder{peers, completed}
// 	c := testutil.GenerateCids(1)[0]
// 	id := testutil.GenerateSessionID()

// 	sessionPeerManager := New(ctx, id, fpt, fppf)

// 	findCtx, findCancel := context.WithTimeout(ctx, 10*time.Millisecond)
// 	defer findCancel()
// 	sessionPeerManager.FindMorePeers(ctx, c)
// 	select {
// 	case <-completed:
// 	case <-findCtx.Done():
// 		t.Fatal("Did not finish finding providers")
// 	}
// 	time.Sleep(2 * time.Millisecond)

// 	sessionPeers := getPeers(sessionPeerManager)
// 	if len(sessionPeers) != len(peers) {
// 		t.Fatal("incorrect number of peers found")
// 	}
// 	for _, p := range sessionPeers {
// 		if !testutil.ContainsPeer(peers, p) {
// 			t.Fatal("incorrect peer found through finding providers")
// 		}
// 	}
// 	if len(fpt.taggedPeers) != len(peers) {
// 		t.Fatal("Peers were not tagged!")
// 	}
// }

func TestAddPeers(t *testing.T) {
	peers := testutil.GeneratePeers(2)
	spm := New(1, &fakePeerTagger{})

	isNew := spm.AddPeer(peers[0])
	if !isNew {
		t.Fatal("Expected peer to be new")
90 91
	}

92 93 94
	isNew = spm.AddPeer(peers[0])
	if isNew {
		t.Fatal("Expected peer to no longer be new")
95 96
	}

97 98 99
	isNew = spm.AddPeer(peers[1])
	if !isNew {
		t.Fatal("Expected peer to be new")
100 101 102
	}
}

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
func TestRemovePeers(t *testing.T) {
	peers := testutil.GeneratePeers(2)
	spm := New(1, &fakePeerTagger{})

	existed := spm.RemovePeer(peers[0])
	if existed {
		t.Fatal("Expected peer not to exist")
	}

	spm.AddPeer(peers[0])
	spm.AddPeer(peers[1])

	existed = spm.RemovePeer(peers[0])
	if !existed {
		t.Fatal("Expected peer to exist")
118
	}
119 120 121
	existed = spm.RemovePeer(peers[1])
	if !existed {
		t.Fatal("Expected peer to exist")
122
	}
123 124 125
	existed = spm.RemovePeer(peers[0])
	if existed {
		t.Fatal("Expected peer not to have existed")
126 127 128
	}
}

129 130 131
func TestHasPeers(t *testing.T) {
	peers := testutil.GeneratePeers(2)
	spm := New(1, &fakePeerTagger{})
hannahhoward's avatar
hannahhoward committed
132

133 134
	if spm.HasPeers() {
		t.Fatal("Expected not to have peers yet")
hannahhoward's avatar
hannahhoward committed
135 136
	}

137 138 139
	spm.AddPeer(peers[0])
	if !spm.HasPeers() {
		t.Fatal("Expected to have peers")
140 141
	}

142 143 144
	spm.AddPeer(peers[1])
	if !spm.HasPeers() {
		t.Fatal("Expected to have peers")
145 146
	}

147 148 149
	spm.RemovePeer(peers[0])
	if !spm.HasPeers() {
		t.Fatal("Expected to have peers")
150 151
	}

152 153 154 155 156
	spm.RemovePeer(peers[1])
	if spm.HasPeers() {
		t.Fatal("Expected to no longer have peers")
	}
}
157

158 159 160
func TestHasPeer(t *testing.T) {
	peers := testutil.GeneratePeers(2)
	spm := New(1, &fakePeerTagger{})
hannahhoward's avatar
hannahhoward committed
161

162 163
	if spm.HasPeer(peers[0]) {
		t.Fatal("Expected not to have peer yet")
hannahhoward's avatar
hannahhoward committed
164 165
	}

166 167 168
	spm.AddPeer(peers[0])
	if !spm.HasPeer(peers[0]) {
		t.Fatal("Expected to have peer")
hannahhoward's avatar
hannahhoward committed
169 170
	}

171 172 173
	spm.AddPeer(peers[1])
	if !spm.HasPeer(peers[1]) {
		t.Fatal("Expected to have peer")
hannahhoward's avatar
hannahhoward committed
174
	}
175

176 177 178
	spm.RemovePeer(peers[0])
	if spm.HasPeer(peers[0]) {
		t.Fatal("Expected not to have peer")
179 180
	}

181 182 183 184
	if !spm.HasPeer(peers[1]) {
		t.Fatal("Expected to have peer")
	}
}
185

186 187 188
func TestPeers(t *testing.T) {
	peers := testutil.GeneratePeers(2)
	spm := New(1, &fakePeerTagger{})
189

190 191
	if len(spm.Peers()) > 0 {
		t.Fatal("Expected not to have peers yet")
192 193
	}

194 195 196
	spm.AddPeer(peers[0])
	if len(spm.Peers()) != 1 {
		t.Fatal("Expected to have one peer")
197 198
	}

199 200 201
	spm.AddPeer(peers[1])
	if len(spm.Peers()) != 2 {
		t.Fatal("Expected to have two peers")
202 203
	}

204 205 206
	spm.RemovePeer(peers[0])
	if len(spm.Peers()) != 1 {
		t.Fatal("Expected to have one peer")
207
	}
208
}
209

210 211 212
func TestPeersDiscovered(t *testing.T) {
	peers := testutil.GeneratePeers(2)
	spm := New(1, &fakePeerTagger{})
213

214 215
	if spm.PeersDiscovered() {
		t.Fatal("Expected not to have discovered peers yet")
216 217
	}

218 219 220 221
	spm.AddPeer(peers[0])
	if !spm.PeersDiscovered() {
		t.Fatal("Expected to have discovered peers")
	}
222

223 224 225
	spm.RemovePeer(peers[0])
	if !spm.PeersDiscovered() {
		t.Fatal("Expected to still have discovered peers")
226
	}
227
}
228

229 230 231 232
func TestPeerTagging(t *testing.T) {
	peers := testutil.GeneratePeers(2)
	fpt := &fakePeerTagger{}
	spm := New(1, fpt)
233

234 235 236 237
	spm.AddPeer(peers[0])
	if len(fpt.taggedPeers) != 1 {
		t.Fatal("Expected to have tagged one peer")
	}
238

239 240 241 242
	spm.AddPeer(peers[0])
	if len(fpt.taggedPeers) != 1 {
		t.Fatal("Expected to have tagged one peer")
	}
243

244 245 246
	spm.AddPeer(peers[1])
	if len(fpt.taggedPeers) != 2 {
		t.Fatal("Expected to have tagged two peers")
247
	}
248

249 250 251
	spm.RemovePeer(peers[1])
	if len(fpt.taggedPeers) != 1 {
		t.Fatal("Expected to have untagged peer")
252
	}
253 254
}

255 256
func TestShutdown(t *testing.T) {
	peers := testutil.GeneratePeers(2)
257
	fpt := &fakePeerTagger{}
258
	spm := New(1, fpt)
259

260 261 262 263
	spm.AddPeer(peers[0])
	spm.AddPeer(peers[1])
	if len(fpt.taggedPeers) != 2 {
		t.Fatal("Expected to have tagged two peers")
264 265
	}

266
	spm.Shutdown()
267

268 269
	if len(fpt.taggedPeers) != 0 {
		t.Fatal("Expected to have untagged all peers")
270 271
	}
}