go.uber.org/yarpc@v1.72.1/yarpctest/fake_config.go (about) 1 // Copyright (c) 2022 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package yarpctest 22 23 import ( 24 "go.uber.org/yarpc/api/peer" 25 "go.uber.org/yarpc/api/transport" 26 peerbind "go.uber.org/yarpc/peer" 27 "go.uber.org/yarpc/peer/hostport" 28 "go.uber.org/yarpc/yarpcconfig" 29 ) 30 31 // FakeTransportConfig configures the FakeTransport. 32 type FakeTransportConfig struct { 33 Nop string `config:"nop,interpolate"` 34 } 35 36 func buildFakeTransport(c *FakeTransportConfig, kit *yarpcconfig.Kit) (transport.Transport, error) { 37 return NewFakeTransport(NopTransportOption(c.Nop)), nil 38 } 39 40 // FakeOutboundConfig configures the FakeOutbound. 41 type FakeOutboundConfig struct { 42 yarpcconfig.PeerChooser 43 44 Nop string `config:"nop,interpolate"` 45 } 46 47 func buildFakeUnaryOutbound(c *FakeOutboundConfig, t transport.Transport, kit *yarpcconfig.Kit) (transport.UnaryOutbound, error) { 48 return buildFakeOutbound(c, t, kit) 49 } 50 51 func buildFakeOnewayOutbound(c *FakeOutboundConfig, t transport.Transport, kit *yarpcconfig.Kit) (transport.OnewayOutbound, error) { 52 return buildFakeOutbound(c, t, kit) 53 } 54 55 func buildFakeStreamOutbound(c *FakeOutboundConfig, t transport.Transport, kit *yarpcconfig.Kit) (transport.StreamOutbound, error) { 56 return buildFakeOutbound(c, t, kit) 57 } 58 59 func buildFakeOutbound(c *FakeOutboundConfig, t transport.Transport, kit *yarpcconfig.Kit) (*FakeOutbound, error) { 60 x := t.(*FakeTransport) 61 chooser, err := c.BuildPeerChooser(x, hostport.Identify, kit) 62 if err != nil { 63 return nil, err 64 } 65 return x.NewOutbound(chooser, NopOutboundOption(c.Nop)), nil 66 } 67 68 // FakeTransportSpec returns a configurator spec for the fake-transport 69 // transport type, suitable for passing to Configurator.MustRegisterTransport. 70 func FakeTransportSpec() yarpcconfig.TransportSpec { 71 return yarpcconfig.TransportSpec{ 72 Name: "fake-transport", 73 BuildTransport: buildFakeTransport, 74 BuildUnaryOutbound: buildFakeUnaryOutbound, 75 BuildOnewayOutbound: buildFakeOnewayOutbound, 76 BuildStreamOutbound: buildFakeStreamOutbound, 77 PeerChooserPresets: []yarpcconfig.PeerChooserPreset{ 78 FakePeerChooserPreset(), 79 }, 80 } 81 } 82 83 // FakePeerChooserConfig configures the FakePeerChooser. 84 type FakePeerChooserConfig struct { 85 Nop string `config:"nop,interpolate"` 86 } 87 88 func buildFakePeerChooser(c *FakePeerChooserConfig, t peer.Transport, kit *yarpcconfig.Kit) (peer.Chooser, error) { 89 return NewFakePeerChooser(ChooserNop(c.Nop)), nil 90 } 91 92 // FakePeerChooserSpec returns a configurator spec for the fake-chooser FakePeerChooser 93 // peer selection strategy, suitable for passing to 94 // Configurator.MustRegisterPeerChooser. 95 func FakePeerChooserSpec() yarpcconfig.PeerChooserSpec { 96 return yarpcconfig.PeerChooserSpec{ 97 Name: "fake-chooser", 98 BuildPeerChooser: buildFakePeerChooser, 99 } 100 } 101 102 // FakePeerListConfig configures the FakePeerList. 103 type FakePeerListConfig struct { 104 Nop string `config:"nop,interpolate"` 105 } 106 107 func buildFakePeerList(c *FakePeerListConfig, t peer.Transport, kit *yarpcconfig.Kit) (peer.ChooserList, error) { 108 return NewFakePeerList(ListNop(c.Nop)), nil 109 } 110 111 // FakePeerListSpec returns a configurator spec for the fake-list FakePeerList 112 // peer selection strategy, suitable for passing to 113 // Configurator.MustRegisterPeerList. 114 func FakePeerListSpec() yarpcconfig.PeerListSpec { 115 return yarpcconfig.PeerListSpec{ 116 Name: "fake-list", 117 BuildPeerList: buildFakePeerList, 118 } 119 } 120 121 // FakePeerListUpdaterConfig configures a fake-updater FakePeerListUpdater. 122 // It has a fake "watch" property that adds the Watch option for 123 // NewFakePeerListUpdater when you build a peer list with this config. 124 type FakePeerListUpdaterConfig struct { 125 FakeUpdater string `config:"fake-updater"` 126 Nop string `config:"nop,interpolate"` 127 Watch bool `config:"watch"` 128 } 129 130 func buildFakePeerListUpdater(c *FakePeerListUpdaterConfig, kit *yarpcconfig.Kit) (peer.Binder, error) { 131 var opts []FakePeerListUpdaterOption 132 if c.Watch { 133 opts = append(opts, Watch) 134 } 135 if c.Nop != "" { 136 opts = append(opts, UpdaterNop(c.Nop)) 137 } 138 return func(pl peer.List) transport.Lifecycle { 139 return NewFakePeerListUpdater(opts...) 140 }, nil 141 } 142 143 // FakePeerListUpdaterSpec returns a configurator spec for the fake-updater 144 // FakePeerListUpdater type, suitable for passing to Configurator.MustRegisterPeerListUpdaterSpec. 145 func FakePeerListUpdaterSpec() yarpcconfig.PeerListUpdaterSpec { 146 return yarpcconfig.PeerListUpdaterSpec{ 147 Name: "fake-updater", 148 BuildPeerListUpdater: buildFakePeerListUpdater, 149 } 150 } 151 152 // NewFakeConfigurator returns a configurator with fake-transport, 153 // fake-peer-list, and fake-peer-list-updater specs already registered, 154 // suitable for testing the configurator. 155 func NewFakeConfigurator(opts ...yarpcconfig.Option) *yarpcconfig.Configurator { 156 configurator := yarpcconfig.New(opts...) 157 configurator.MustRegisterTransport(FakeTransportSpec()) 158 configurator.MustRegisterPeerChooser(FakePeerChooserSpec()) 159 configurator.MustRegisterPeerList(FakePeerListSpec()) 160 configurator.MustRegisterPeerListUpdater(FakePeerListUpdaterSpec()) 161 return configurator 162 } 163 164 // FakePeerChooserPreset is a PeerChooserPreset which builds a FakePeerList buind to 165 // a FakePeerListUpdater. 166 func FakePeerChooserPreset() yarpcconfig.PeerChooserPreset { 167 return yarpcconfig.PeerChooserPreset{ 168 Name: "fake-preset", 169 BuildPeerChooser: func(peer.Transport, *yarpcconfig.Kit) (peer.Chooser, error) { 170 return peerbind.Bind( 171 NewFakePeerList(), func(peer.List) transport.Lifecycle { 172 return NewFakePeerListUpdater() 173 }), nil 174 }, 175 } 176 }