-
Notifications
You must be signed in to change notification settings - Fork 4
/
bitswap_test.go
131 lines (117 loc) · 3.64 KB
/
bitswap_test.go
1
2
3
4
5
6
7
8
9
10
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
package bitswap_test
import (
"context"
"errors"
"sync"
"testing"
"time"
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p"
bitswap "github.com/willscott/go-selfish-bitswap-client"
bitswapserver "github.com/willscott/go-selfish-bitswap-client/server"
"github.com/willscott/go-selfish-bitswap-client/server/util"
)
func TestRoundtrip(t *testing.T) {
serverHost, _ := libp2p.New()
clientHost, _ := libp2p.New()
clientHost.Peerstore().AddAddrs(serverHost.ID(), serverHost.Addrs(), time.Hour)
store := util.NewMemStore(make(map[cid.Cid][]byte))
c := util.Add(store, []byte("hello world"))
bitswapserver.AttachBitswapServer(serverHost, store)
session := bitswap.New(clientHost, serverHost.ID(), bitswap.Options{})
blk, err := session.Get(context.Background(), c)
if err != nil {
t.Fatalf("should get block, got %v", err)
}
if string(blk) != "hello world" {
t.Fatalf("get didn't succeed")
}
}
func TestAskRepeated(t *testing.T) {
serverHost, _ := libp2p.New()
clientHost, _ := libp2p.New()
clientHost.Peerstore().AddAddrs(serverHost.ID(), serverHost.Addrs(), time.Hour)
store := util.NewMemStore(make(map[cid.Cid][]byte))
c1 := util.Add(store, []byte("hello world"))
c2 := util.Add(store, []byte("hello world 2"))
bitswapserver.AttachBitswapServer(serverHost, store)
session := bitswap.New(clientHost, serverHost.ID(), bitswap.Options{})
blk1, err := session.Get(context.Background(), c1)
if err != nil {
t.Fatalf("should get block, got %v", err)
}
if string(blk1) != "hello world" {
t.Fatalf("get didn't succeed")
}
blk2, err := session.Get(context.Background(), c2)
if err != nil {
t.Fatalf("should get block, got %v", err)
}
if string(blk2) != "hello world 2" {
t.Fatalf("repeated get didn't succeed")
}
}
func TestAskMultiple(t *testing.T) {
serverHost, _ := libp2p.New()
clientHost, _ := libp2p.New()
clientHost.Peerstore().AddAddrs(serverHost.ID(), serverHost.Addrs(), time.Hour)
store := util.NewMemStore(make(map[cid.Cid][]byte))
c1 := util.Add(store, []byte("hello world"))
c2 := util.Add(store, []byte("hello world 2"))
bitswapserver.AttachBitswapServer(serverHost, store)
session := bitswap.New(clientHost, serverHost.ID(), bitswap.Options{})
wg := sync.WaitGroup{}
wg.Add(2)
var err1 error
var err2 error
go func(t *testing.T) {
blk1, err := session.Get(context.Background(), c1)
if err != nil {
err1 = err
} else if string(blk1) != "hello world" {
err1 = errors.New("get didn't succeed")
}
wg.Done()
}(t)
go func(t *testing.T) {
blk2, err := session.Get(context.Background(), c2)
if err != nil {
err2 = err
}
if string(blk2) != "hello world 2" {
err2 = errors.New("2nd didn't get succeed")
}
wg.Done()
}(t)
wg.Wait()
if err1 != nil {
t.Fatal(err1)
}
if err2 != nil {
t.Fatal(err2)
}
}
func TestBadAsksClose(t *testing.T) {
serverHost, _ := libp2p.New()
clientHost, _ := libp2p.New()
clientHost.Peerstore().AddAddrs(serverHost.ID(), serverHost.Addrs(), time.Hour)
store := util.NewMemStore(make(map[cid.Cid][]byte))
_ = util.Add(store, []byte("hello world"))
otherStore := util.NewMemStore(make(map[cid.Cid][]byte))
badC := util.Add(otherStore, []byte("not a number"))
bitswapserver.AttachBitswapServer(serverHost, store)
session := bitswap.New(clientHost, serverHost.ID(), bitswap.Options{})
_, err := session.Get(context.Background(), badC)
if err == nil {
t.Fatalf("should fail to get a cid not on server")
}
conns := clientHost.Network().ConnsToPeer(serverHost.ID())
for _, c := range conns {
strs := c.GetStreams()
for _, s := range strs {
if s.Protocol() == bitswap.ProtocolBitswap {
t.Fatal("after failure there shouldn't be an open bitswap conn")
}
}
}
}