-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdummy_test.go
128 lines (109 loc) · 3.13 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
package routinghelpers
import (
"context"
"errors"
"strings"
"sync"
cid "github.com/ipfs/go-cid"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
routing "github.com/libp2p/go-libp2p-routing"
ropts "github.com/libp2p/go-libp2p-routing/options"
)
type failValueStore struct{}
var failValueErr = errors.New("fail valuestore error")
func (f failValueStore) PutValue(ctx context.Context, key string, value []byte, opts ...ropts.Option) error {
return failValueErr
}
func (f failValueStore) GetValue(ctx context.Context, key string, opts ...ropts.Option) ([]byte, error) {
return nil, failValueErr
}
func (f failValueStore) SearchValue(ctx context.Context, key string, opts ...ropts.Option) (<-chan []byte, error) {
return nil, failValueErr
}
type dummyValueStore sync.Map
func (d *dummyValueStore) PutValue(ctx context.Context, key string, value []byte, opts ...ropts.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 ...ropts.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 ...ropts.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 pstore.PeerInfo {
peers := d[c.KeyString()]
if len(peers) > count {
peers = peers[:count]
}
out := make(chan pstore.PeerInfo)
go func() {
defer close(out)
for _, p := range peers {
if p == "stall" {
<-ctx.Done()
return
}
select {
case out <- pstore.PeerInfo{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 pstore.PeerInfo {
ch := make(chan pstore.PeerInfo)
close(ch)
return ch
}
type dummyPeerRouter map[peer.ID]struct{}
func (d dummyPeerRouter) FindPeer(ctx context.Context, p peer.ID) (pstore.PeerInfo, error) {
if _, ok := d[p]; ok {
return pstore.PeerInfo{ID: p}, nil
}
return pstore.PeerInfo{}, routing.ErrNotFound
}