-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectableHostStub.go
172 lines (140 loc) · 4.29 KB
/
connectableHostStub.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package mock
import (
"context"
"errors"
"github.com/libp2p/go-libp2p/core/connmgr"
"github.com/libp2p/go-libp2p/core/event"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/multiformats/go-multiaddr"
)
// ConnectableHostStub -
type ConnectableHostStub struct {
EventBusCalled func() event.Bus
IDCalled func() peer.ID
PeerstoreCalled func() peerstore.Peerstore
AddrsCalled func() []multiaddr.Multiaddr
NetworkCalled func() network.Network
MuxCalled func() protocol.Switch
ConnectCalled func(ctx context.Context, pi peer.AddrInfo) error
SetStreamHandlerCalled func(pid protocol.ID, handler network.StreamHandler)
SetStreamHandlerMatchCalled func(protocol.ID, func(string) bool, network.StreamHandler)
RemoveStreamHandlerCalled func(pid protocol.ID)
NewStreamCalled func(ctx context.Context, p peer.ID, pids ...protocol.ID) (network.Stream, error)
CloseCalled func() error
ConnManagerCalled func() connmgr.ConnManager
ConnectToPeerCalled func(ctx context.Context, address string) error
AddressToPeerInfoCalled func(address string) (*peer.AddrInfo, error)
}
// EventBus -
func (hs *ConnectableHostStub) EventBus() event.Bus {
if hs.EventBusCalled != nil {
return hs.EventBusCalled()
}
return &EventBusStub{}
}
// ConnectToPeer -
func (hs *ConnectableHostStub) ConnectToPeer(ctx context.Context, address string) error {
if hs.ConnectToPeerCalled != nil {
return hs.ConnectToPeerCalled(ctx, address)
}
return nil
}
// ID -
func (hs *ConnectableHostStub) ID() peer.ID {
if hs.IDCalled != nil {
return hs.IDCalled()
}
return "mock pid"
}
// Peerstore -
func (hs *ConnectableHostStub) Peerstore() peerstore.Peerstore {
if hs.PeerstoreCalled != nil {
return hs.PeerstoreCalled()
}
return nil
}
// Addrs -
func (hs *ConnectableHostStub) Addrs() []multiaddr.Multiaddr {
if hs.AddrsCalled != nil {
return hs.AddrsCalled()
}
return make([]multiaddr.Multiaddr, 0)
}
// Network -
func (hs *ConnectableHostStub) Network() network.Network {
if hs.NetworkCalled != nil {
return hs.NetworkCalled()
}
return &NetworkStub{}
}
// Mux -
func (hs *ConnectableHostStub) Mux() protocol.Switch {
if hs.MuxCalled != nil {
return hs.MuxCalled()
}
return nil
}
// Connect -
func (hs *ConnectableHostStub) Connect(ctx context.Context, pi peer.AddrInfo) error {
if hs.ConnectCalled != nil {
return hs.ConnectCalled(ctx, pi)
}
return nil
}
// SetStreamHandler -
func (hs *ConnectableHostStub) SetStreamHandler(pid protocol.ID, handler network.StreamHandler) {
if hs.SetStreamHandlerCalled != nil {
hs.SetStreamHandlerCalled(pid, handler)
}
}
// SetStreamHandlerMatch -
func (hs *ConnectableHostStub) SetStreamHandlerMatch(pid protocol.ID, handler func(string) bool, streamHandler network.StreamHandler) {
if hs.SetStreamHandlerMatchCalled != nil {
hs.SetStreamHandlerMatchCalled(pid, handler, streamHandler)
}
}
// RemoveStreamHandler -
func (hs *ConnectableHostStub) RemoveStreamHandler(pid protocol.ID) {
if hs.RemoveStreamHandlerCalled != nil {
hs.RemoveStreamHandlerCalled(pid)
}
}
// NewStream -
func (hs *ConnectableHostStub) NewStream(ctx context.Context, p peer.ID, pids ...protocol.ID) (network.Stream, error) {
if hs.NewStreamCalled != nil {
return hs.NewStreamCalled(ctx, p, pids...)
}
return nil, errors.New("no stream")
}
// Close -
func (hs *ConnectableHostStub) Close() error {
if hs.CloseCalled != nil {
return hs.CloseCalled()
}
return nil
}
// ConnManager -
func (hs *ConnectableHostStub) ConnManager() connmgr.ConnManager {
if hs.ConnManagerCalled != nil {
return hs.ConnManagerCalled()
}
return nil
}
// AddressToPeerInfo -
func (hs *ConnectableHostStub) AddressToPeerInfo(address string) (*peer.AddrInfo, error) {
if hs.AddressToPeerInfoCalled != nil {
return hs.AddressToPeerInfoCalled(address)
}
multiAddr, err := multiaddr.NewMultiaddr(address)
if err != nil {
return nil, err
}
return peer.AddrInfoFromP2pAddr(multiAddr)
}
// IsInterfaceNil returns true if there is no value under the interface
func (hs *ConnectableHostStub) IsInterfaceNil() bool {
return hs == nil
}