sessionpeermanager_test.go 4.41 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 45
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")
46 47
	}

48 49 50
	isNew = spm.AddPeer(peers[0])
	if isNew {
		t.Fatal("Expected peer to no longer be new")
51 52
	}

53 54 55
	isNew = spm.AddPeer(peers[1])
	if !isNew {
		t.Fatal("Expected peer to be new")
56 57 58
	}
}

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
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")
74
	}
75 76 77
	existed = spm.RemovePeer(peers[1])
	if !existed {
		t.Fatal("Expected peer to exist")
78
	}
79 80 81
	existed = spm.RemovePeer(peers[0])
	if existed {
		t.Fatal("Expected peer not to have existed")
82 83 84
	}
}

85 86 87
func TestHasPeers(t *testing.T) {
	peers := testutil.GeneratePeers(2)
	spm := New(1, &fakePeerTagger{})
hannahhoward's avatar
hannahhoward committed
88

89 90
	if spm.HasPeers() {
		t.Fatal("Expected not to have peers yet")
hannahhoward's avatar
hannahhoward committed
91 92
	}

93 94 95
	spm.AddPeer(peers[0])
	if !spm.HasPeers() {
		t.Fatal("Expected to have peers")
96 97
	}

98 99 100
	spm.AddPeer(peers[1])
	if !spm.HasPeers() {
		t.Fatal("Expected to have peers")
101 102
	}

103 104 105
	spm.RemovePeer(peers[0])
	if !spm.HasPeers() {
		t.Fatal("Expected to have peers")
106 107
	}

108 109 110 111 112
	spm.RemovePeer(peers[1])
	if spm.HasPeers() {
		t.Fatal("Expected to no longer have peers")
	}
}
113

114 115 116
func TestHasPeer(t *testing.T) {
	peers := testutil.GeneratePeers(2)
	spm := New(1, &fakePeerTagger{})
hannahhoward's avatar
hannahhoward committed
117

118 119
	if spm.HasPeer(peers[0]) {
		t.Fatal("Expected not to have peer yet")
hannahhoward's avatar
hannahhoward committed
120 121
	}

122 123 124
	spm.AddPeer(peers[0])
	if !spm.HasPeer(peers[0]) {
		t.Fatal("Expected to have peer")
hannahhoward's avatar
hannahhoward committed
125 126
	}

127 128 129
	spm.AddPeer(peers[1])
	if !spm.HasPeer(peers[1]) {
		t.Fatal("Expected to have peer")
hannahhoward's avatar
hannahhoward committed
130
	}
131

132 133 134
	spm.RemovePeer(peers[0])
	if spm.HasPeer(peers[0]) {
		t.Fatal("Expected not to have peer")
135 136
	}

137 138 139 140
	if !spm.HasPeer(peers[1]) {
		t.Fatal("Expected to have peer")
	}
}
141

142 143 144
func TestPeers(t *testing.T) {
	peers := testutil.GeneratePeers(2)
	spm := New(1, &fakePeerTagger{})
145

146 147
	if len(spm.Peers()) > 0 {
		t.Fatal("Expected not to have peers yet")
148 149
	}

150 151 152
	spm.AddPeer(peers[0])
	if len(spm.Peers()) != 1 {
		t.Fatal("Expected to have one peer")
153 154
	}

155 156 157
	spm.AddPeer(peers[1])
	if len(spm.Peers()) != 2 {
		t.Fatal("Expected to have two peers")
158 159
	}

160 161 162
	spm.RemovePeer(peers[0])
	if len(spm.Peers()) != 1 {
		t.Fatal("Expected to have one peer")
163
	}
164
}
165

166 167 168
func TestPeersDiscovered(t *testing.T) {
	peers := testutil.GeneratePeers(2)
	spm := New(1, &fakePeerTagger{})
169

170 171
	if spm.PeersDiscovered() {
		t.Fatal("Expected not to have discovered peers yet")
172 173
	}

174 175 176 177
	spm.AddPeer(peers[0])
	if !spm.PeersDiscovered() {
		t.Fatal("Expected to have discovered peers")
	}
178

179 180 181
	spm.RemovePeer(peers[0])
	if !spm.PeersDiscovered() {
		t.Fatal("Expected to still have discovered peers")
182
	}
183
}
184

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

190 191 192 193
	spm.AddPeer(peers[0])
	if len(fpt.taggedPeers) != 1 {
		t.Fatal("Expected to have tagged one peer")
	}
194

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

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

205 206 207
	spm.RemovePeer(peers[1])
	if len(fpt.taggedPeers) != 1 {
		t.Fatal("Expected to have untagged peer")
208
	}
209 210
}

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

216 217 218 219
	spm.AddPeer(peers[0])
	spm.AddPeer(peers[1])
	if len(fpt.taggedPeers) != 2 {
		t.Fatal("Expected to have tagged two peers")
220 221
	}

222
	spm.Shutdown()
223

224 225
	if len(fpt.taggedPeers) != 0 {
		t.Fatal("Expected to have untagged all peers")
226 227
	}
}