/
fake_transport.go
282 lines (243 loc) · 8.15 KB
/
fake_transport.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package yarpctest
import (
"fmt"
"sync"
"go.uber.org/yarpc/api/peer"
"go.uber.org/yarpc/pkg/lifecycle"
)
// FakeTransportOption is an option for NewFakeTransport.
type FakeTransportOption func(*FakeTransport)
// NopTransportOption returns a no-op option for NewFakeTransport.
// The option exists to verify that options work.
func NopTransportOption(nopOption string) FakeTransportOption {
return func(t *FakeTransport) {
t.nopOption = nopOption
}
}
// InitialConnectionStatus specifies the initial connection status for new
// peers of this transport. This is Available by default. With the status set
// to Unavailable, the test may manual simmulate connection and disconnection
// with the SimulateConnect and SimulateDisconnect methods.
func InitialConnectionStatus(s peer.ConnectionStatus) FakeTransportOption {
return func(t *FakeTransport) {
t.initialConnectionStatus = s
}
}
// RetainErrors specifies an error for RetainPeer to return for the given
// addresses.
func RetainErrors(err error, addrs []string) FakeTransportOption {
return func(t *FakeTransport) {
for _, addr := range addrs {
t.retainErrors[addr] = err
}
}
}
// ReleaseErrors specifies an error for ReleasePeer to return for the given
// addresses.
func ReleaseErrors(err error, addrs []string) FakeTransportOption {
return func(t *FakeTransport) {
for _, addr := range addrs {
t.releaseErrors[addr] = err
}
}
}
// NewFakeTransport returns a fake transport.
func NewFakeTransport(opts ...FakeTransportOption) *FakeTransport {
t := &FakeTransport{
once: lifecycle.NewOnce(),
initialConnectionStatus: peer.Available,
initialPeerConnectionStatuses: make(map[string]peer.ConnectionStatus),
peers: make(map[string]*FakePeer),
retainErrors: make(map[string]error),
releaseErrors: make(map[string]error),
pendingStatusChanges: make(chan struct{}, 1),
done: make(chan struct{}),
}
for _, opt := range opts {
opt(t)
}
return t
}
// FakeTransport is a fake transport.
type FakeTransport struct {
nopOption string
initialConnectionStatus peer.ConnectionStatus
initialPeerConnectionStatuses map[string]peer.ConnectionStatus
retainErrors map[string]error
releaseErrors map[string]error
once *lifecycle.Once
mu sync.RWMutex
peers map[string]*FakePeer
changesQueue []statusChange
pendingStatusChanges chan struct{}
done chan struct{}
}
// NopOption returns the configured nopOption. It's fake.
func (t *FakeTransport) NopOption() string {
return t.nopOption
}
// SimulateRetainError leaves a note that any subsequent Retain for a
// particular address should return an error.
func (t *FakeTransport) SimulateRetainError(id peer.Identifier, err error) {
t.retainErrors[id.Identifier()] = err
}
// SimulateReleaseError leaves a note that any subsequent Release for a particular
// address should return an error.
func (t *FakeTransport) SimulateReleaseError(id peer.Identifier, err error) {
t.releaseErrors[id.Identifier()] = err
}
// SimulateStatusChange simulates a connection or disconnection to the peer,
// marking the peer connection status and notifying all subscribers.
func (t *FakeTransport) SimulateStatusChange(id peer.Identifier, status peer.ConnectionStatus) {
t.Peer(id).simulateStatusChange(status)
}
// SimulateConnect simulates a connection to the peer, marking the peer as
// available and notifying subscribers.
func (t *FakeTransport) SimulateConnect(id peer.Identifier) {
t.Peer(id).simulateConnect()
}
// SimulateDisconnect simulates a disconnection to the peer, marking the peer
// as unavailable and notifying subscribers.
func (t *FakeTransport) SimulateDisconnect(id peer.Identifier) {
t.Peer(id).simulateDisconnect()
}
// Peer returns the persistent peer object for that peer identifier for the
// lifetime of the fake transport.
func (t *FakeTransport) Peer(id peer.Identifier) *FakePeer {
t.mu.Lock()
defer t.mu.Unlock()
if p, ok := t.peers[id.Identifier()]; ok {
return p
}
p := &FakePeer{
id: id,
status: peer.Status{
ConnectionStatus: t.initialConnectionStatus,
},
}
t.peers[id.Identifier()] = p
return p
}
// RetainPeer returns a fake peer.
func (t *FakeTransport) RetainPeer(id peer.Identifier, ps peer.Subscriber) (peer.Peer, error) {
if err := t.retainErrors[id.Identifier()]; err != nil {
return nil, err
}
peer := t.Peer(id)
peer.subscribe(ps)
t.enqueue(statusChange{
Peer: peer,
Status: t.getInitialStatus(id.Identifier()),
})
t.scheduleFlush()
return peer, nil
}
func (t *FakeTransport) getInitialStatus(addr string) peer.ConnectionStatus {
if status, ok := t.initialPeerConnectionStatuses[addr]; ok {
return status
}
return t.initialConnectionStatus
}
// ReleasePeer does nothing.
func (t *FakeTransport) ReleasePeer(id peer.Identifier, ps peer.Subscriber) error {
peer := t.Peer(id)
t.mu.Lock()
defer t.mu.Unlock()
if err := t.releaseErrors[id.Identifier()]; err != nil {
return err
}
if count := peer.unsubscribe(ps); count == 0 {
return fmt.Errorf("no such subscriber")
} else if count > 1 {
return fmt.Errorf("extra subscribers: %d", count-1)
}
t.scheduleFlush()
return nil
}
// Start spins up a goroutine to asynchronously flush status change notifications.
//
// If you do not start a fake dialer, you must call Flush explicitly.
func (t *FakeTransport) Start() error {
return t.once.Start(func() error {
go t.monitor()
return nil
})
}
// Stop shuts down the fake dialer, allowing its status change notification
// loop to exit.
func (t *FakeTransport) Stop() error {
return t.once.Stop(func() error {
close(t.done)
return nil
})
}
// IsRunning returns whether the fake transport is running.
func (t *FakeTransport) IsRunning() bool {
return t.once.IsRunning()
}
func (t *FakeTransport) scheduleFlush() {
select {
case t.pendingStatusChanges <- struct{}{}:
default:
}
}
func (t *FakeTransport) monitor() {
Loop:
for {
select {
case <-t.done:
break Loop
case <-t.pendingStatusChanges:
t.Flush()
}
}
}
type statusChange struct {
Peer *FakePeer
Status peer.ConnectionStatus
}
// Flush effects all queued status changes from retaining or releasing peers.
//
// Calling RetainPeer and ReleasePeer schedules a peer status change and its
// notifications.
// Concrete dialer implementations dispatch these notifications from a
// goroutine and subscribers may obtain a lock on the peer status.
// For testability, the fake transport queues these changes and calling Flush
// dispatches the notifications synchronously, but still off the RetainPeer and
// ReleasePeer stacks.
func (t *FakeTransport) Flush() {
for _, change := range t.dequeue() {
change.Peer.simulateStatusChange(change.Status)
}
}
func (t *FakeTransport) enqueue(change statusChange) {
t.mu.Lock()
defer t.mu.Unlock()
t.changesQueue = append(t.changesQueue, change)
}
func (t *FakeTransport) dequeue() []statusChange {
t.mu.Lock()
defer t.mu.Unlock()
queue := t.changesQueue
t.changesQueue = make([]statusChange, 0)
return queue
}