-
Notifications
You must be signed in to change notification settings - Fork 82
/
stub_host.go
79 lines (64 loc) · 2.77 KB
/
stub_host.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
// Copyright (c) 2015 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 relaytest
import (
"errors"
"github.com/uber/tchannel-go"
"github.com/uber/tchannel-go/relay"
)
var errNoChannel = errors.New("no channel set to get peers from")
// Ensure that the StubRelayHost implements tchannel.RelayHost.
var _ tchannel.RelayHost = (*StubRelayHost)(nil)
// StubRelayHost is a stub RelayHost for tests that backs peer selection to an
// underlying channel using isolated subchannels and the default peer selection.
type StubRelayHost struct {
ch *tchannel.Channel
stats *MockStats
}
type stubCall struct {
*MockCallStats
peer *tchannel.Peer
}
// NewStubRelayHost creates a new stub RelayHost for tests.
func NewStubRelayHost() *StubRelayHost {
return &StubRelayHost{nil, NewMockStats()}
}
// SetChannel is called by the channel after creation so we can
// get a reference to the channels' peers.
func (rh *StubRelayHost) SetChannel(ch *tchannel.Channel) {
rh.ch = ch
}
// Start starts a new RelayCall for the given call on a specific connection.
func (rh *StubRelayHost) Start(cf relay.CallFrame, _ *relay.Conn) (tchannel.RelayCall, error) {
// Get a peer from the subchannel.
peer, err := rh.ch.GetSubChannel(string(cf.Service())).Peers().Get(nil)
return &stubCall{rh.stats.Begin(cf), peer}, err
}
// Add adds a service instance with the specified host:port.
func (rh *StubRelayHost) Add(service, hostPort string) {
rh.ch.GetSubChannel(service, tchannel.Isolated).Peers().GetOrAdd(hostPort)
}
// Stats returns the *MockStats tracked for this channel.
func (rh *StubRelayHost) Stats() *MockStats {
return rh.stats
}
// Destination returns the selected peer for this call.
func (c *stubCall) Destination() (*tchannel.Peer, bool) {
return c.peer, c.peer != nil
}