-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fake: Add fake outbound, peer list, peer list updater
- Loading branch information
Showing
4 changed files
with
221 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// 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 yarpctest | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go.uber.org/yarpc/api/peer" | ||
"go.uber.org/yarpc/api/transport" | ||
intsync "go.uber.org/yarpc/internal/sync" | ||
) | ||
|
||
// FakeOutboundOption is an option for FakeTransport.NewOutbound. | ||
type FakeOutboundOption func(*FakeOutbound) | ||
|
||
// NopOutboundOption returns an option to set the "nopOption" for a | ||
// FakeTransport.NewOutbound. | ||
// The nopOption has no effect exists only to verify that the option was | ||
// passed, via `FakeOutbound.NopOption()`. | ||
func NopOutboundOption(nopOption string) FakeOutboundOption { | ||
return func(o *FakeOutbound) { | ||
o.nopOption = nopOption | ||
} | ||
} | ||
|
||
// NewOutbound returns a FakeOutbound with a given peer chooser and options. | ||
func (t *FakeTransport) NewOutbound(c peer.Chooser, opts ...FakeOutboundOption) *FakeOutbound { | ||
o := &FakeOutbound{ | ||
once: intsync.Once(), | ||
transport: t, | ||
chooser: c, | ||
} | ||
for _, opt := range opts { | ||
opt(o) | ||
} | ||
return o | ||
} | ||
|
||
// FakeOutbound is a unary outbound for the FakeTransport. It is fake. | ||
type FakeOutbound struct { | ||
once intsync.LifecycleOnce | ||
transport *FakeTransport | ||
chooser peer.Chooser | ||
nopOption string | ||
} | ||
|
||
// Chooser returns theis FakeOutbound's peer chooser. | ||
func (o *FakeOutbound) Chooser() peer.Chooser { | ||
return o.chooser | ||
} | ||
|
||
// NopOption returns this FakeOutbound's nopOption. It is fake. | ||
func (o *FakeOutbound) NopOption() string { | ||
return o.nopOption | ||
} | ||
|
||
// Start starts the fake outbound and its chooser. | ||
func (o *FakeOutbound) Start() error { | ||
return o.once.Start(o.chooser.Start) | ||
} | ||
|
||
// Stop stops the fake outbound and its chooser. | ||
func (o *FakeOutbound) Stop() error { | ||
return o.once.Stop(o.chooser.Stop) | ||
} | ||
|
||
// IsRunning returns whether the fake outbound is running. | ||
func (o *FakeOutbound) IsRunning() bool { | ||
return o.once.IsRunning() | ||
} | ||
|
||
// Transports returns the FakeTransport that owns this outbound. | ||
func (o *FakeOutbound) Transports() []transport.Transport { | ||
return []transport.Transport{o.transport} | ||
} | ||
|
||
// Call pretends to send a unary RPC, but actually just returns an error. | ||
func (o *FakeOutbound) Call(ctx context.Context, req *transport.Request) (*transport.Response, error) { | ||
return nil, fmt.Errorf(`FakeOutbound does not support calls. It's fake`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// 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 yarpctest | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go.uber.org/yarpc/api/peer" | ||
"go.uber.org/yarpc/api/transport" | ||
intsync "go.uber.org/yarpc/internal/sync" | ||
) | ||
|
||
// FakePeerList is a fake peer list. | ||
type FakePeerList struct { | ||
transport.Lifecycle | ||
} | ||
|
||
// NewFakePeerList returns a fake peer list. | ||
func NewFakePeerList() *FakePeerList { | ||
return &FakePeerList{ | ||
Lifecycle: intsync.NewNopLifecycle(), | ||
} | ||
} | ||
|
||
// Choose pretends to choose a peer, but actually always returns an error. It's fake. | ||
func (c *FakePeerList) Choose(ctx context.Context, req *transport.Request) (peer.Peer, func(error), error) { | ||
return nil, nil, fmt.Errorf(`fake peer list can't actually choose peers`) | ||
} | ||
|
||
// Update pretends to add or remove peers. | ||
func (c *FakePeerList) Update(up peer.ListUpdates) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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 yarpctest | ||
|
||
import ( | ||
"go.uber.org/yarpc/api/transport" | ||
intsync "go.uber.org/yarpc/internal/sync" | ||
) | ||
|
||
// FakePeerListUpdaterOption is an option for NewFakePeerListUpdater. | ||
type FakePeerListUpdaterOption func(*FakePeerListUpdater) | ||
|
||
// Watch is a fake option for NewFakePeerListUpdater that enables "watch". It's fake. | ||
func Watch(u *FakePeerListUpdater) { | ||
u.watch = true | ||
} | ||
|
||
// FakePeerListUpdater is a fake peer list updater. It doesn't actually update | ||
// a peer list. | ||
type FakePeerListUpdater struct { | ||
transport.Lifecycle | ||
watch bool | ||
} | ||
|
||
// NewFakePeerListUpdater returns a new FakePeerListUpdater, applying any | ||
// passed options. | ||
func NewFakePeerListUpdater(opts ...FakePeerListUpdaterOption) *FakePeerListUpdater { | ||
u := &FakePeerListUpdater{ | ||
Lifecycle: intsync.NewNopLifecycle(), | ||
} | ||
for _, opt := range opts { | ||
opt(u) | ||
} | ||
return u | ||
} | ||
|
||
// Watch returns whether the peer list updater was configured to "watch". It is | ||
// fake. | ||
func (u *FakePeerListUpdater) Watch() bool { | ||
return u.watch | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters