Skip to content

Commit 61b2613

Browse files
committed
namesys/pubsub: update test to use extant mock routing
License: MIT Signed-off-by: vyzo <[email protected]>
1 parent c20a099 commit 61b2613

File tree

2 files changed

+34
-79
lines changed

2 files changed

+34
-79
lines changed

namesys/pubsub_test.go

+30-79
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import (
77
"time"
88

99
path "github.com/ipfs/go-ipfs/path"
10+
mockrouting "github.com/ipfs/go-ipfs/routing/mock"
11+
testutil "github.com/ipfs/go-ipfs/thirdparty/testutil"
1012

1113
ci "gx/ipfs/QmP1DfoUjiWH2ZBo1PBH6FupdBucbDepx3HpWmEY6JMUpY/go-libp2p-crypto"
1214
routing "gx/ipfs/QmP1wMAqk6aZYRZirbaAwmrNeqFRgQrwBt3orUtvSa1UYD/go-libp2p-routing"
1315
floodsub "gx/ipfs/QmUpeULWfmtsgCnfuRN3BHsfhHvBxNphoYh4La4CMxGt2Z/floodsub"
1416
p2phost "gx/ipfs/QmUywuGNZoUKV8B9iyvup9bPkLiMrhTsyVMkeSXW5VxAfC/go-libp2p-host"
1517
ds "gx/ipfs/QmVSase1JP7cq9QkPT46oNwdp9pT6kBkG3oqS14y3QcZjG/go-datastore"
16-
pstore "gx/ipfs/QmXZSd1qR5BxZkPyuwfT5jpqQFScZccoZvDneXsKzCNHWX/go-libp2p-peerstore"
1718
netutil "gx/ipfs/Qma2j8dYePrvN5DoNgwh1uAuu3FFtEtrUQFmr737ws8nCp/go-libp2p-netutil"
18-
cid "gx/ipfs/Qma4RJSuh7mMeJQYCqMbKzekn6EwBo7HEs5AQYjVRMQATB/go-cid"
1919
bhost "gx/ipfs/Qma4Xhhqtr9tpV814eNjbLHzjuDaRjs96XLcZPJiR742ZV/go-libp2p-blankhost"
2020
peer "gx/ipfs/QmdS9KpbDyPrieswibZhkod1oXqRwZJrUPzxCofAMWpFGq/go-libp2p-peer"
2121
)
@@ -36,13 +36,19 @@ func newNetHosts(ctx context.Context, t *testing.T, n int) []p2phost.Host {
3636
return out
3737
}
3838

39-
type mockDHT struct {
39+
// PubKeyFetcher implementation with a global key store
40+
type mockKeyStore struct {
4041
keys map[peer.ID]ci.PubKey
41-
prov map[string][]pstore.PeerInfo
4242
mx sync.Mutex
4343
}
4444

45-
func (m *mockDHT) getPubKey(id peer.ID) (ci.PubKey, error) {
45+
func (m *mockKeyStore) addPubKey(id peer.ID, pkey ci.PubKey) {
46+
m.mx.Lock()
47+
defer m.mx.Unlock()
48+
m.keys[id] = pkey
49+
}
50+
51+
func (m *mockKeyStore) getPubKey(id peer.ID) (ci.PubKey, error) {
4652
m.mx.Lock()
4753
defer m.mx.Unlock()
4854
pkey, ok := m.keys[id]
@@ -53,86 +59,31 @@ func (m *mockDHT) getPubKey(id peer.ID) (ci.PubKey, error) {
5359
return nil, routing.ErrNotFound
5460
}
5561

56-
func (m *mockDHT) addPubKey(id peer.ID, pkey ci.PubKey) {
57-
m.mx.Lock()
58-
defer m.mx.Unlock()
59-
m.keys[id] = pkey
60-
}
61-
62-
func (m *mockDHT) getProviders(cid *cid.Cid) []pstore.PeerInfo {
63-
m.mx.Lock()
64-
defer m.mx.Unlock()
65-
return m.prov[cid.String()]
66-
}
67-
68-
func (m *mockDHT) addProvider(cid *cid.Cid, pi pstore.PeerInfo) {
69-
m.mx.Lock()
70-
defer m.mx.Unlock()
71-
key := cid.String()
72-
m.prov[key] = append(m.prov[key], pi)
62+
func (m *mockKeyStore) GetPublicKey(ctx context.Context, id peer.ID) (ci.PubKey, error) {
63+
return m.getPubKey(id)
7364
}
7465

75-
func newMockDHT() *mockDHT {
76-
return &mockDHT{
66+
func newMockKeyStore() *mockKeyStore {
67+
return &mockKeyStore{
7768
keys: make(map[peer.ID]ci.PubKey),
78-
prov: make(map[string][]pstore.PeerInfo),
7969
}
8070
}
8171

82-
type mockRouting struct {
83-
pi pstore.PeerInfo
84-
dht *mockDHT
85-
}
86-
87-
func (m *mockRouting) Provide(ctx context.Context, cid *cid.Cid, announce bool) error {
88-
m.dht.addProvider(cid, m.pi)
89-
return nil
90-
}
91-
92-
func (m *mockRouting) FindProvidersAsync(ctx context.Context, cid *cid.Cid, count int) <-chan pstore.PeerInfo {
93-
ch := make(chan pstore.PeerInfo)
94-
go func() {
95-
defer close(ch)
96-
k := 0
97-
loop:
98-
for _, pi := range m.dht.getProviders(cid) {
99-
if k < count {
100-
select {
101-
case ch <- pi:
102-
k++
103-
case <-ctx.Done():
104-
break loop
105-
}
106-
} else {
107-
break loop
108-
}
109-
}
110-
}()
111-
112-
return ch
113-
}
114-
115-
func (m *mockRouting) GetPublicKey(ctx context.Context, id peer.ID) (ci.PubKey, error) {
116-
return m.dht.getPubKey(id)
117-
}
118-
119-
func newMockRouting(dht *mockDHT, host p2phost.Host) *mockRouting {
72+
func newMockRouting(ms mockrouting.Server, ks *mockKeyStore, host p2phost.Host) routing.ContentRouting {
12073
id := host.ID()
12174

75+
privk := host.Peerstore().PrivKey(id)
12276
pubk := host.Peerstore().PubKey(id)
123-
dht.addPubKey(id, pubk)
124-
12577
pi := host.Peerstore().PeerInfo(id)
126-
return &mockRouting{
127-
pi: pi,
128-
dht: dht,
129-
}
78+
79+
ks.addPubKey(id, pubk)
80+
return ms.Client(testutil.NewIdentity(id, pi.Addrs[0], privk, pubk))
13081
}
13182

132-
func newMockRoutingForHosts(dht *mockDHT, hosts []p2phost.Host) []*mockRouting {
133-
rs := make([]*mockRouting, len(hosts))
83+
func newMockRoutingForHosts(ms mockrouting.Server, ks *mockKeyStore, hosts []p2phost.Host) []routing.ContentRouting {
84+
rs := make([]routing.ContentRouting, len(hosts))
13485
for i := 0; i < len(hosts); i++ {
135-
rs[i] = newMockRouting(dht, hosts[i])
86+
rs[i] = newMockRouting(ms, ks, hosts[i])
13687
}
13788
return rs
13889
}
@@ -141,20 +92,21 @@ func TestPubsubPublishResolve(t *testing.T) {
14192
ctx, cancel := context.WithCancel(context.Background())
14293
defer cancel()
14394

144-
dht := newMockDHT()
95+
ms := mockrouting.NewServer()
96+
ks := newMockKeyStore()
14597

14698
pubhost := newNetHost(ctx, t)
147-
pubmr := newMockRouting(dht, pubhost)
99+
pubmr := newMockRouting(ms, ks, pubhost)
148100
pub := NewPubsubPublisher(ctx, ds.NewMapDatastore(), pubhost, pubmr, floodsub.NewFloodSub(ctx, pubhost))
149101
privk := pubhost.Peerstore().PrivKey(pubhost.ID())
150102

151103
name := "/ipns/" + pubhost.ID().Pretty()
152104

153105
reshosts := newNetHosts(ctx, t, 20)
154-
resmrs := newMockRoutingForHosts(dht, reshosts)
106+
resmrs := newMockRoutingForHosts(ms, ks, reshosts)
155107
res := make([]Resolver, len(reshosts))
156108
for i := 0; i < len(res); i++ {
157-
res[i] = NewPubsubResolver(ctx, reshosts[i], resmrs[i], resmrs[i], floodsub.NewFloodSub(ctx, reshosts[i]))
109+
res[i] = NewPubsubResolver(ctx, reshosts[i], resmrs[i], ks, floodsub.NewFloodSub(ctx, reshosts[i]))
158110
}
159111

160112
time.Sleep(time.Millisecond * 100)
@@ -171,7 +123,7 @@ func TestPubsubPublishResolve(t *testing.T) {
171123
t.Fatal(err)
172124
}
173125

174-
time.Sleep(time.Second * 1)
126+
time.Sleep(time.Second * 3)
175127
for i := 0; i < len(res); i++ {
176128
checkResolve(ctx, t, res[i], name, val)
177129
}
@@ -182,11 +134,10 @@ func TestPubsubPublishResolve(t *testing.T) {
182134
t.Fatal(err)
183135
}
184136

185-
time.Sleep(time.Second * 1)
137+
time.Sleep(time.Second * 3)
186138
for i := 0; i < len(res); i++ {
187139
checkResolve(ctx, t, res[i], name, val)
188140
}
189-
190141
}
191142

192143
func checkResolveNotFound(ctx context.Context, t *testing.T, resolver Resolver, name string) {

thirdparty/testutil/identity.go

+4
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@ func (p *identity) PrivateKey() ci.PrivKey {
5353
func (p *identity) PublicKey() ci.PubKey {
5454
return p.PubKey
5555
}
56+
57+
func NewIdentity(ID peer.ID, addr ma.Multiaddr, privk ci.PrivKey, pubk ci.PubKey) Identity {
58+
return &identity{PeerNetParams{ID: ID, Addr: addr, PrivKey: privk, PubKey: pubk}}
59+
}

0 commit comments

Comments
 (0)