Skip to content

Commit 85d5de7

Browse files
committed
additional testing
1 parent df4c1d8 commit 85d5de7

File tree

6 files changed

+58
-68
lines changed

6 files changed

+58
-68
lines changed

dht_test.go

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/stretchr/testify/require"
2626

2727
pb "github.com/libp2p/go-libp2p-kad-dht/pb"
28+
test "github.com/libp2p/go-libp2p-kad-dht/internal/testing"
2829

2930
"github.com/ipfs/go-cid"
3031
u "github.com/ipfs/go-ipfs-util"
@@ -72,33 +73,8 @@ type blankValidator struct{}
7273
func (blankValidator) Validate(_ string, _ []byte) error { return nil }
7374
func (blankValidator) Select(_ string, _ [][]byte) (int, error) { return 0, nil }
7475

75-
type testValidator struct{}
76-
77-
func (testValidator) Select(_ string, bs [][]byte) (int, error) {
78-
index := -1
79-
for i, b := range bs {
80-
if bytes.Equal(b, []byte("newer")) {
81-
index = i
82-
} else if bytes.Equal(b, []byte("valid")) {
83-
if index == -1 {
84-
index = i
85-
}
86-
}
87-
}
88-
if index == -1 {
89-
return -1, errors.New("no rec found")
90-
}
91-
return index, nil
92-
}
93-
func (testValidator) Validate(_ string, b []byte) error {
94-
if bytes.Equal(b, []byte("expired")) {
95-
return errors.New("expired")
96-
}
97-
return nil
98-
}
99-
10076
type testAtomicPutValidator struct {
101-
testValidator
77+
test.TestValidator
10278
}
10379

10480
// selects the entry with the 'highest' last byte
@@ -372,7 +348,7 @@ func TestValueSetInvalid(t *testing.T) {
372348
defer dhtA.host.Close()
373349
defer dhtB.host.Close()
374350

375-
dhtA.Validator.(record.NamespacedValidator)["v"] = testValidator{}
351+
dhtA.Validator.(record.NamespacedValidator)["v"] = test.TestValidator{}
376352
dhtB.Validator.(record.NamespacedValidator)["v"] = blankValidator{}
377353

378354
connect(t, ctx, dhtA, dhtB)
@@ -451,8 +427,8 @@ func TestSearchValue(t *testing.T) {
451427

452428
connect(t, ctx, dhtA, dhtB)
453429

454-
dhtA.Validator.(record.NamespacedValidator)["v"] = testValidator{}
455-
dhtB.Validator.(record.NamespacedValidator)["v"] = testValidator{}
430+
dhtA.Validator.(record.NamespacedValidator)["v"] = test.TestValidator{}
431+
dhtB.Validator.(record.NamespacedValidator)["v"] = test.TestValidator{}
456432

457433
ctxT, cancel := context.WithTimeout(ctx, time.Second)
458434
defer cancel()
@@ -554,7 +530,7 @@ func TestValueGetInvalid(t *testing.T) {
554530
defer dhtB.host.Close()
555531

556532
dhtA.Validator.(record.NamespacedValidator)["v"] = blankValidator{}
557-
dhtB.Validator.(record.NamespacedValidator)["v"] = testValidator{}
533+
dhtB.Validator.(record.NamespacedValidator)["v"] = test.TestValidator{}
558534

559535
connect(t, ctx, dhtA, dhtB)
560536

dual/dual.go

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/libp2p/go-libp2p-core/protocol"
1515
"github.com/libp2p/go-libp2p-core/routing"
1616
dht "github.com/libp2p/go-libp2p-kad-dht"
17+
helper "github.com/libp2p/go-libp2p-routing-helpers"
1718
)
1819

1920
// DHT implements the routing interface to provide two concrete DHT implementationts for use
@@ -230,35 +231,8 @@ func (d *DHT) GetValue(ctx context.Context, key string, opts ...routing.Option)
230231

231232
// SearchValue searches for better values from this value
232233
func (dht *DHT) SearchValue(ctx context.Context, key string, opts ...routing.Option) (<-chan []byte, error) {
233-
streama, erra := dht.WAN.SearchValue(ctx, key, opts...)
234-
streamb, errb := dht.WAN.SearchValue(ctx, key, opts...)
235-
if erra == nil && errb == nil {
236-
combinedStream := make(chan []byte)
237-
var combinedWg sync.WaitGroup
238-
combinedWg.Add(2)
239-
go func(out chan []byte) {
240-
for itm := range streama {
241-
out <- itm
242-
}
243-
combinedWg.Done()
244-
}(combinedStream)
245-
go func(out chan []byte) {
246-
for itm := range streamb {
247-
out <- itm
248-
}
249-
combinedWg.Done()
250-
}(combinedStream)
251-
go func() {
252-
combinedWg.Wait()
253-
close(combinedStream)
254-
}()
255-
return combinedStream, nil
256-
} else if erra == nil {
257-
return streama, nil
258-
} else if errb == nil {
259-
return streamb, nil
260-
}
261-
return nil, mergeErrors(erra, errb)
234+
p := helper.Parallel{Routers: []routing.Routing{dht.WAN, dht.LAN}, Validator: dht.WAN.Validator}
235+
return p.SearchValue(ctx, key, opts...)
262236
}
263237

264238
// GetPublicKey returns the public key for the given peer.

dual/dual_test.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import (
1212
"github.com/libp2p/go-libp2p-core/network"
1313
"github.com/libp2p/go-libp2p-core/peer"
1414
dht "github.com/libp2p/go-libp2p-kad-dht"
15+
test "github.com/libp2p/go-libp2p-kad-dht/internal/testing"
1516
peerstore "github.com/libp2p/go-libp2p-peerstore"
17+
record "github.com/libp2p/go-libp2p-record"
1618
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
1719
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
1820
)
@@ -108,11 +110,9 @@ func connect(ctx context.Context, t *testing.T, a, b *dht.IpfsDHT) {
108110
t.Fatal("no addresses for connection.")
109111
}
110112
a.Host().Peerstore().AddAddrs(bid, baddr, peerstore.TempAddrTTL)
111-
fmt.Fprintf(os.Stderr, "gonna connect.\n")
112113
if err := a.Host().Connect(ctx, peer.AddrInfo{ID: bid}); err != nil {
113114
t.Fatal(err)
114115
}
115-
fmt.Fprintf(os.Stderr, "gonn wait\n")
116116
wait(ctx, t, a, b)
117117
}
118118

@@ -231,15 +231,15 @@ func TestValueGetSet(t *testing.T) {
231231
defer wan.Close()
232232
defer lan.Close()
233233

234-
err := d.PutValue(ctx, "/v/hello", []byte("world"))
234+
err := d.PutValue(ctx, "/v/hello", []byte("valid"))
235235
if err != nil {
236236
t.Fatal(err)
237237
}
238238
val, err := wan.GetValue(ctx, "/v/hello")
239239
if err != nil {
240240
t.Fatal(err)
241241
}
242-
if string(val) != "world" {
242+
if string(val) != "valid" {
243243
t.Fatal("failed to get expected string.")
244244
}
245245

@@ -258,7 +258,10 @@ func TestSearchValue(t *testing.T) {
258258
defer wan.Close()
259259
defer lan.Close()
260260

261-
err := wan.PutValue(ctx, "/v/hello", []byte("world"), )
261+
d.WAN.Validator.(record.NamespacedValidator)["v"] = test.TestValidator{}
262+
d.LAN.Validator.(record.NamespacedValidator)["v"] = test.TestValidator{}
263+
264+
err := wan.PutValue(ctx, "/v/hello", []byte("valid"))
262265

263266
valCh, err := d.SearchValue(ctx, "/v/hello", dht.Quorum(0))
264267
if err != nil {
@@ -267,8 +270,8 @@ func TestSearchValue(t *testing.T) {
267270

268271
select {
269272
case v := <-valCh:
270-
if string(v) != "world" {
271-
t.Errorf("expected 'world', got '%s'", string(v))
273+
if string(v) != "valid" {
274+
t.Errorf("expected 'valid', got '%s'", string(v))
272275
}
273276
case <-ctx.Done():
274277
t.Fatal(ctx.Err())
@@ -280,10 +283,13 @@ func TestSearchValue(t *testing.T) {
280283
}
281284

282285
select {
283-
case v := <-valCh:
286+
case v, ok := <-valCh:
284287
if string(v) != "newer" {
285288
t.Errorf("expected 'newer', got '%s'", string(v))
286289
}
290+
if !ok {
291+
t.Errorf("chan closed early")
292+
}
287293
case <-ctx.Done():
288294
t.Fatal(ctx.Err())
289295
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ require (
2020
github.com/libp2p/go-libp2p-kbucket v0.3.3
2121
github.com/libp2p/go-libp2p-peerstore v0.2.2
2222
github.com/libp2p/go-libp2p-record v0.1.2
23+
github.com/libp2p/go-libp2p-routing-helpers v0.2.0
2324
github.com/libp2p/go-libp2p-swarm v0.2.3
2425
github.com/libp2p/go-libp2p-testing v0.1.1
2526
github.com/libp2p/go-msgio v0.0.4
2627
github.com/libp2p/go-netroute v0.1.2
27-
github.com/mr-tron/base58 v1.1.3
2828
github.com/multiformats/go-base32 v0.0.3
2929
github.com/multiformats/go-multiaddr v0.2.1
3030
github.com/multiformats/go-multiaddr-dns v0.2.0

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ github.com/gxed/hashland/murmur3 v0.0.1 h1:SheiaIt0sda5K+8FLz952/1iWS9zrnKsEJaOJ
7979
github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48=
8080
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
8181
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
82+
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
8283
github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI=
8384
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
8485
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
@@ -264,6 +265,8 @@ github.com/libp2p/go-libp2p-pnet v0.2.0 h1:J6htxttBipJujEjz1y0a5+eYoiPcFHhSYHH6n
264265
github.com/libp2p/go-libp2p-pnet v0.2.0/go.mod h1:Qqvq6JH/oMZGwqs3N1Fqhv8NVhrdYcO0BW4wssv21LA=
265266
github.com/libp2p/go-libp2p-record v0.1.2 h1:M50VKzWnmUrk/M5/Dz99qO9Xh4vs8ijsK+7HkJvRP+0=
266267
github.com/libp2p/go-libp2p-record v0.1.2/go.mod h1:pal0eNcT5nqZaTV7UGhqeGqxFgGdsU/9W//C8dqjQDk=
268+
github.com/libp2p/go-libp2p-routing-helpers v0.2.0 h1:+QKTsx2Bg0q3oueQ9CopTwKN5NsnF+qEC+sbkSVXnsU=
269+
github.com/libp2p/go-libp2p-routing-helpers v0.2.0/go.mod h1:Db+7LRSPImkV9fOKsNWVW5IXyy9XDse92lUtO3O+jlo=
267270
github.com/libp2p/go-libp2p-secio v0.1.0/go.mod h1:tMJo2w7h3+wN4pgU2LSYeiKPrfqBgkOsdiKK77hE7c8=
268271
github.com/libp2p/go-libp2p-secio v0.2.0 h1:ywzZBsWEEz2KNTn5RtzauEDq5RFEefPsttXYwAWqHng=
269272
github.com/libp2p/go-libp2p-secio v0.2.0/go.mod h1:2JdZepB8J5V9mBp79BmwsaPQhRPNN2NrnB2lKQcdy6g=

internal/testing/helper.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package testing
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
)
7+
8+
type TestValidator struct{}
9+
10+
func (TestValidator) Select(_ string, bs [][]byte) (int, error) {
11+
index := -1
12+
for i, b := range bs {
13+
if bytes.Equal(b, []byte("newer")) {
14+
index = i
15+
} else if bytes.Equal(b, []byte("valid")) {
16+
if index == -1 {
17+
index = i
18+
}
19+
}
20+
}
21+
if index == -1 {
22+
return -1, errors.New("no rec found")
23+
}
24+
return index, nil
25+
}
26+
func (TestValidator) Validate(_ string, b []byte) error {
27+
if bytes.Equal(b, []byte("expired")) {
28+
return errors.New("expired")
29+
}
30+
return nil
31+
}

0 commit comments

Comments
 (0)