sessionpeermanager_test.go 6.89 KB
Newer Older
1 2 3 4
package sessionpeermanager

import (
	"context"
5
	"errors"
hannahhoward's avatar
hannahhoward committed
6
	"math/rand"
7
	"sync"
8 9 10 11 12 13 14 15 16 17 18 19 20 21
	"testing"
	"time"

	"github.com/ipfs/go-bitswap/testutil"

	cid "github.com/ipfs/go-cid"
	ifconnmgr "github.com/libp2p/go-libp2p-interface-connmgr"
	inet "github.com/libp2p/go-libp2p-net"
	peer "github.com/libp2p/go-libp2p-peer"
)

type fakePeerNetwork struct {
	peers       []peer.ID
	connManager ifconnmgr.ConnManager
22 23
	completed   chan struct{}
	connect     chan struct{}
24 25 26 27 28 29
}

func (fpn *fakePeerNetwork) ConnectionManager() ifconnmgr.ConnManager {
	return fpn.connManager
}

30 31 32 33 34 35 36
func (fpn *fakePeerNetwork) ConnectTo(ctx context.Context, p peer.ID) error {
	select {
	case fpn.connect <- struct{}{}:
		return nil
	case <-ctx.Done():
		return errors.New("Timeout Occurred")
	}
37 38
}

39 40 41 42 43 44 45
func (fpn *fakePeerNetwork) FindProvidersAsync(ctx context.Context, c cid.Cid, num int) <-chan peer.ID {
	peerCh := make(chan peer.ID)
	go func() {
		for _, p := range fpn.peers {
			select {
			case peerCh <- p:
			case <-ctx.Done():
46
				close(peerCh)
47 48 49
				return
			}
		}
50 51 52 53 54 55
		close(peerCh)

		select {
		case fpn.completed <- struct{}{}:
		case <-ctx.Done():
		}
56 57 58 59 60 61
	}()
	return peerCh
}

type fakeConnManager struct {
	taggedPeers []peer.ID
62
	wait        sync.WaitGroup
63 64 65
}

func (fcm *fakeConnManager) TagPeer(p peer.ID, tag string, n int) {
66
	fcm.wait.Add(1)
67 68
	fcm.taggedPeers = append(fcm.taggedPeers, p)
}
69

70
func (fcm *fakeConnManager) UntagPeer(p peer.ID, tag string) {
71
	defer fcm.wait.Done()
72 73 74 75 76 77 78 79
	for i := 0; i < len(fcm.taggedPeers); i++ {
		if fcm.taggedPeers[i] == p {
			fcm.taggedPeers[i] = fcm.taggedPeers[len(fcm.taggedPeers)-1]
			fcm.taggedPeers = fcm.taggedPeers[:len(fcm.taggedPeers)-1]
			return
		}
	}
}
80

81 82 83 84 85 86 87 88
func (*fakeConnManager) GetTagInfo(p peer.ID) *ifconnmgr.TagInfo { return nil }
func (*fakeConnManager) TrimOpenConns(ctx context.Context)       {}
func (*fakeConnManager) Notifee() inet.Notifiee                  { return nil }

func TestFindingMorePeers(t *testing.T) {
	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()
89 90 91
	completed := make(chan struct{})
	connect := make(chan struct{})

92 93
	peers := testutil.GeneratePeers(5)
	fcm := &fakeConnManager{}
94
	fpn := &fakePeerNetwork{peers, fcm, completed, connect}
95 96 97 98 99 100 101 102
	c := testutil.GenerateCids(1)[0]
	id := testutil.GenerateSessionID()

	sessionPeerManager := New(ctx, id, fpn)

	findCtx, findCancel := context.WithTimeout(ctx, 10*time.Millisecond)
	defer findCancel()
	sessionPeerManager.FindMorePeers(ctx, c)
103 104 105 106 107 108 109 110 111 112 113 114 115 116
	select {
	case <-completed:
	case <-findCtx.Done():
		t.Fatal("Did not finish finding providers")
	}
	for range peers {
		select {
		case <-connect:
		case <-findCtx.Done():
			t.Fatal("Did not connect to peer")
		}
	}
	time.Sleep(2 * time.Millisecond)

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	sessionPeers := sessionPeerManager.GetOptimizedPeers()
	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(fcm.taggedPeers) != len(peers) {
		t.Fatal("Peers were not tagged!")
	}
}

func TestRecordingReceivedBlocks(t *testing.T) {
	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()
	p := testutil.GeneratePeers(1)[0]
	fcm := &fakeConnManager{}
137
	fpn := &fakePeerNetwork{nil, fcm, nil, nil}
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	c := testutil.GenerateCids(1)[0]
	id := testutil.GenerateSessionID()

	sessionPeerManager := New(ctx, id, fpn)
	sessionPeerManager.RecordPeerResponse(p, c)
	time.Sleep(10 * time.Millisecond)
	sessionPeers := sessionPeerManager.GetOptimizedPeers()
	if len(sessionPeers) != 1 {
		t.Fatal("did not add peer on receive")
	}
	if sessionPeers[0] != p {
		t.Fatal("incorrect peer added on receive")
	}
	if len(fcm.taggedPeers) != 1 {
		t.Fatal("Peers was not tagged!")
	}
}

hannahhoward's avatar
hannahhoward committed
156 157
func TestOrderingPeers(t *testing.T) {
	ctx := context.Background()
158
	ctx, cancel := context.WithTimeout(ctx, 30*time.Millisecond)
hannahhoward's avatar
hannahhoward committed
159 160
	defer cancel()
	peers := testutil.GeneratePeers(100)
161 162
	completed := make(chan struct{})
	connect := make(chan struct{})
hannahhoward's avatar
hannahhoward committed
163
	fcm := &fakeConnManager{}
164
	fpn := &fakePeerNetwork{peers, fcm, completed, connect}
hannahhoward's avatar
hannahhoward committed
165 166 167 168 169 170
	c := testutil.GenerateCids(1)
	id := testutil.GenerateSessionID()
	sessionPeerManager := New(ctx, id, fpn)

	// add all peers to session
	sessionPeerManager.FindMorePeers(ctx, c[0])
171 172 173 174 175 176 177 178 179 180 181 182 183
	select {
	case <-completed:
	case <-ctx.Done():
		t.Fatal("Did not finish finding providers")
	}
	for range peers {
		select {
		case <-connect:
		case <-ctx.Done():
			t.Fatal("Did not connect to peer")
		}
	}
	time.Sleep(2 * time.Millisecond)
hannahhoward's avatar
hannahhoward committed
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

	// record broadcast
	sessionPeerManager.RecordPeerRequests(nil, c)

	// record receives
	peer1 := peers[rand.Intn(100)]
	peer2 := peers[rand.Intn(100)]
	peer3 := peers[rand.Intn(100)]
	time.Sleep(1 * time.Millisecond)
	sessionPeerManager.RecordPeerResponse(peer1, c[0])
	time.Sleep(1 * time.Millisecond)
	sessionPeerManager.RecordPeerResponse(peer2, c[0])
	time.Sleep(1 * time.Millisecond)
	sessionPeerManager.RecordPeerResponse(peer3, c[0])

	sessionPeers := sessionPeerManager.GetOptimizedPeers()
	if len(sessionPeers) != maxOptimizedPeers {
		t.Fatal("Should not return more than the max of optimized peers")
	}

	// should prioritize peers which have received blocks
	if (sessionPeers[0] != peer3) || (sessionPeers[1] != peer2) || (sessionPeers[2] != peer1) {
		t.Fatal("Did not prioritize peers that received blocks")
	}

	// Receive a second time from same node
	sessionPeerManager.RecordPeerResponse(peer3, c[0])

	// call again
	nextSessionPeers := sessionPeerManager.GetOptimizedPeers()
	if len(nextSessionPeers) != maxOptimizedPeers {
		t.Fatal("Should not return more than the max of optimized peers")
	}

	// should not duplicate
	if (nextSessionPeers[0] != peer3) || (nextSessionPeers[1] != peer2) || (nextSessionPeers[2] != peer1) {
		t.Fatal("Did dedup peers which received multiple blocks")
	}

	// should randomize other peers
	totalSame := 0
	for i := 3; i < maxOptimizedPeers; i++ {
		if sessionPeers[i] == nextSessionPeers[i] {
			totalSame++
		}
	}
	if totalSame >= maxOptimizedPeers-3 {
		t.Fatal("should not return the same random peers each time")
	}
}
234 235 236 237 238
func TestUntaggingPeers(t *testing.T) {
	ctx := context.Background()
	ctx, cancel := context.WithTimeout(ctx, 10*time.Millisecond)
	defer cancel()
	peers := testutil.GeneratePeers(5)
239 240
	completed := make(chan struct{})
	connect := make(chan struct{})
241
	fcm := &fakeConnManager{}
242
	fpn := &fakePeerNetwork{peers, fcm, completed, connect}
243 244 245 246 247 248
	c := testutil.GenerateCids(1)[0]
	id := testutil.GenerateSessionID()

	sessionPeerManager := New(ctx, id, fpn)

	sessionPeerManager.FindMorePeers(ctx, c)
249 250 251 252 253 254 255 256 257 258 259 260 261 262
	select {
	case <-completed:
	case <-ctx.Done():
		t.Fatal("Did not finish finding providers")
	}
	for range peers {
		select {
		case <-connect:
		case <-ctx.Done():
			t.Fatal("Did not connect to peer")
		}
	}
	time.Sleep(2 * time.Millisecond)

263 264 265 266
	if len(fcm.taggedPeers) != len(peers) {
		t.Fatal("Peers were not tagged!")
	}
	<-ctx.Done()
267 268
	fcm.wait.Wait()

269 270 271 272
	if len(fcm.taggedPeers) != 0 {
		t.Fatal("Peers were not untagged!")
	}
}