Skip to content

Commit

Permalink
fake: Add fake outbound, peer list, peer list updater
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal authored and abhinav committed Apr 26, 2017
1 parent 8526314 commit 5fcc9be
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 3 deletions.
99 changes: 99 additions & 0 deletions yarpctest/fake_outbound.go
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`)
}
52 changes: 52 additions & 0 deletions yarpctest/fake_peer_list.go
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
}
59 changes: 59 additions & 0 deletions yarpctest/fake_peer_list_updater.go
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
}
14 changes: 11 additions & 3 deletions yarpctest/fake_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ import (
"go.uber.org/yarpc/peer/hostport"
)

// FakeTransportOption is an option for NewFakeTransport.
type FakeTransportOption func(*FakeTransport)

func FakeTransportAddress(addr string) FakeTransportOption {
// 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.Address = addr
t.nopOption = nopOption
}
}

Expand All @@ -49,7 +52,12 @@ func NewFakeTransport(opts ...FakeTransportOption) *FakeTransport {
// FakeTransport is a fake transport.
type FakeTransport struct {
transport.Lifecycle
address string
nopOption string
}

// NopOption returns the configured nopOption. It's fake.
func (t *FakeTransport) NopOption() string {
return t.nopOption
}

// RetainPeer returns a fake peer.
Expand Down

0 comments on commit 5fcc9be

Please sign in to comment.