forked from yarpc/yarpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peers.go
148 lines (129 loc) · 4.89 KB
/
peers.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
// Copyright (c) 2017 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 peertest
import (
"fmt"
"sync"
"github.com/golang/mock/gomock"
"go.uber.org/yarpc/api/peer"
)
// MockPeerIdentifier is a small wrapper around the PeerIdentifier interfaces for a string
// unfortunately gomock + assert.Equal has difficulty seeing between mock objects of the same type.
type MockPeerIdentifier string
// Identifier returns a unique identifier for MockPeerIDs
func (pid MockPeerIdentifier) Identifier() string {
return string(pid)
}
// NewLightMockPeer returns a new MockPeer
func NewLightMockPeer(pid MockPeerIdentifier, conStatus peer.ConnectionStatus) *LightMockPeer {
return &LightMockPeer{
MockPeerIdentifier: pid,
PeerStatus: peer.Status{
ConnectionStatus: conStatus,
PendingRequestCount: 0,
},
}
}
// LightMockPeer is a small simple wrapper around the Peer interface for mocking and changing
// a peer's attributes
// MockPeer is NOT thread safe
type LightMockPeer struct {
sync.Mutex
MockPeerIdentifier
PeerStatus peer.Status
}
// Status returns the Status Object of the MockPeer
func (p *LightMockPeer) Status() peer.Status {
return p.PeerStatus
}
// StartRequest is run when a Request starts
func (p *LightMockPeer) StartRequest() {
p.Lock()
p.PeerStatus.PendingRequestCount++
p.Unlock()
}
// EndRequest should be run after a MockPeer request has finished
func (p *LightMockPeer) EndRequest() {
p.Lock()
p.PeerStatus.PendingRequestCount--
p.Unlock()
}
// PeerIdentifierMatcher is used to match a Peer/PeerIdentifier by comparing
// The peer's .Identifier function with the Matcher string
type PeerIdentifierMatcher string
// Matches returns true of got is equivalent to the PeerIdentifier Matching string
func (pim PeerIdentifierMatcher) Matches(got interface{}) bool {
gotPID, ok := got.(peer.Identifier)
if !ok {
return false
}
return gotPID.Identifier() == string(pim)
}
// String returns a description of the matcher
func (pim PeerIdentifierMatcher) String() string {
return fmt.Sprintf("PeerIdentifierMatcher(%s)", string(pim))
}
// CreatePeerIDs takes a slice of peerID strings and returns a slice of PeerIdentifiers
func CreatePeerIDs(peerIDStrs []string) []peer.Identifier {
pids := make([]peer.Identifier, 0, len(peerIDStrs))
for _, id := range peerIDStrs {
pids = append(pids, MockPeerIdentifier(id))
}
return pids
}
// ExpectPeerRetains registers expectations on a MockTransport to generate peers on the RetainPeer function
func ExpectPeerRetains(
transport *MockTransport,
availablePeerStrs []string,
unavailablePeerStrs []string,
) map[string]*LightMockPeer {
peers := make(map[string]*LightMockPeer, len(availablePeerStrs)+len(unavailablePeerStrs))
for _, peerStr := range availablePeerStrs {
p := NewLightMockPeer(MockPeerIdentifier(peerStr), peer.Available)
transport.EXPECT().RetainPeer(PeerIdentifierMatcher(peerStr), gomock.Any()).Return(p, nil)
peers[p.Identifier()] = p
}
for _, peerStr := range unavailablePeerStrs {
p := NewLightMockPeer(MockPeerIdentifier(peerStr), peer.Unavailable)
transport.EXPECT().RetainPeer(PeerIdentifierMatcher(peerStr), gomock.Any()).Return(p, nil)
peers[p.Identifier()] = p
}
return peers
}
// ExpectPeerRetainsWithError registers expectations on a MockTransport return errors
func ExpectPeerRetainsWithError(
transport *MockTransport,
peerStrs []string,
err error, // Will be returned from the MockTransport on the Retains of these Peers
) {
for _, peerStr := range peerStrs {
transport.EXPECT().RetainPeer(PeerIdentifierMatcher(peerStr), gomock.Any()).Return(nil, err)
}
}
// ExpectPeerReleases registers expectations on a MockTransport to release peers through the ReleasePeer function
func ExpectPeerReleases(
transport *MockTransport,
peerStrs []string,
err error,
) {
for _, peerStr := range peerStrs {
transport.EXPECT().ReleasePeer(PeerIdentifierMatcher(peerStr), gomock.Any()).Return(err)
}
}