forked from libp2p/go-libp2p-routing-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdummy_test.go
135 lines (114 loc) · 3.14 KB
/
dummy_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
132
133
134
135
package routinghelpers
import (
"context"
"errors"
"strings"
"sync"
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
)
type testCloser struct {
closed int
}
func (closer *testCloser) Close() error {
closer.closed++
return nil
}
type failValueStore struct{}
var errFailValue = errors.New("fail value-store error")
func (f failValueStore) PutValue(ctx context.Context, key string, value []byte, opts ...routing.Option) error {
return errFailValue
}
func (f failValueStore) GetValue(ctx context.Context, key string, opts ...routing.Option) ([]byte, error) {
return nil, errFailValue
}
func (f failValueStore) SearchValue(ctx context.Context, key string, opts ...routing.Option) (<-chan []byte, error) {
return nil, errFailValue
}
type dummyValueStore sync.Map
func (d *dummyValueStore) PutValue(ctx context.Context, key string, value []byte, opts ...routing.Option) error {
if strings.HasPrefix(key, "/notsupported/") {
return routing.ErrNotSupported
}
if strings.HasPrefix(key, "/error/") {
return errors.New(key[len("/error/"):])
}
if strings.HasPrefix(key, "/stall/") {
<-ctx.Done()
return ctx.Err()
}
(*sync.Map)(d).Store(key, value)
return nil
}
func (d *dummyValueStore) GetValue(ctx context.Context, key string, opts ...routing.Option) ([]byte, error) {
if strings.HasPrefix(key, "/error/") {
return nil, errors.New(key[len("/error/"):])
}
if strings.HasPrefix(key, "/stall/") {
<-ctx.Done()
return nil, ctx.Err()
}
if v, ok := (*sync.Map)(d).Load(key); ok {
return v.([]byte), nil
}
return nil, routing.ErrNotFound
}
func (d *dummyValueStore) SearchValue(ctx context.Context, key string, opts ...routing.Option) (<-chan []byte, error) {
out := make(chan []byte)
if strings.HasPrefix(key, "/error/") {
return nil, errors.New(key[len("/error/"):])
}
go func() {
defer close(out)
v, err := d.GetValue(ctx, key, opts...)
if err == nil {
select {
case out <- v:
case <-ctx.Done():
}
}
}()
return out, nil
}
type dummyProvider map[string][]peer.ID
func (d dummyProvider) FindProvidersAsync(ctx context.Context, c cid.Cid, count int) <-chan peer.AddrInfo {
peers := d[c.KeyString()]
if count > 0 && len(peers) > count {
peers = peers[:count]
}
out := make(chan peer.AddrInfo)
go func() {
defer close(out)
for _, p := range peers {
if p == "stall" {
<-ctx.Done()
return
}
select {
case out <- peer.AddrInfo{ID: p}:
case <-ctx.Done():
}
}
}()
return out
}
func (d dummyProvider) Provide(ctx context.Context, c cid.Cid, local bool) error {
return routing.ErrNotSupported
}
type cbProvider func(c cid.Cid, local bool) error
func (d cbProvider) Provide(ctx context.Context, c cid.Cid, local bool) error {
return d(c, local)
}
func (d cbProvider) FindProvidersAsync(ctx context.Context, c cid.Cid, count int) <-chan peer.AddrInfo {
ch := make(chan peer.AddrInfo)
close(ch)
return ch
}
type dummyPeerRouter map[peer.ID]struct{}
func (d dummyPeerRouter) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) {
if _, ok := d[p]; ok {
return peer.AddrInfo{ID: p}, nil
}
return peer.AddrInfo{}, routing.ErrNotFound
}