-
Notifications
You must be signed in to change notification settings - Fork 1.1k
grpc: add testing utilities for LB policy tests #2380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
63c4e38
1226a56
02f22a2
8a2a116
fc5820a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /* | ||
| * | ||
| * Copyright 2025 gRPC authors. | ||
| * | ||
| * 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. | ||
| * | ||
| */ | ||
|
|
||
| use crate::client::load_balancing::{ | ||
| ChannelController, ExternalSubchannel, ForwardingSubchannel, LbState, Subchannel, WorkScheduler, | ||
| }; | ||
| use crate::client::name_resolution::Address; | ||
| use crate::service::{Message, Request, Response, Service}; | ||
| use std::hash::{Hash, Hasher}; | ||
| use std::{fmt::Debug, ops::Add, sync::Arc}; | ||
| use tokio::sync::{mpsc, Notify}; | ||
| use tokio::task::AbortHandle; | ||
|
|
||
| pub(crate) struct EmptyMessage {} | ||
| impl Message for EmptyMessage {} | ||
| pub(crate) fn new_request() -> Request { | ||
| Request::new(Box::pin(tokio_stream::once( | ||
| Box::new(EmptyMessage {}) as Box<dyn Message> | ||
| ))) | ||
| } | ||
|
|
||
| // A test subchannel that forwards connect calls to a channel. | ||
| // This allows tests to verify when a subchannel is asked to connect. | ||
| pub(crate) struct TestSubchannel { | ||
| address: Address, | ||
| tx_connect: mpsc::UnboundedSender<TestEvent>, | ||
| } | ||
|
|
||
| impl TestSubchannel { | ||
| fn new(address: Address, tx_connect: mpsc::UnboundedSender<TestEvent>) -> Self { | ||
| Self { | ||
| address, | ||
| tx_connect, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ForwardingSubchannel for TestSubchannel { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not related to this PR: Should In the regular delegating pattern, all the structs implement a single interface. What is the intended use of the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The most important reason for going with what we have currently (i.e. a
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. I have a follow-up question: why does A struct with private fields and no public constructor can't be instantiated outside the module.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We want LB policies in the tree to be able to wrap subchannels and override some of its functionality.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To clarify, I was asking about the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is the The |
||
| fn delegate(&self) -> Arc<dyn Subchannel> { | ||
| panic!("unsupported operation on a test subchannel"); | ||
| } | ||
|
|
||
| fn address(&self) -> Address { | ||
| self.address.clone() | ||
| } | ||
|
|
||
| fn connect(&self) { | ||
| println!("connect called for subchannel {}", self.address); | ||
| self.tx_connect | ||
| .send(TestEvent::Connect(self.address.clone())) | ||
| .unwrap(); | ||
| } | ||
| } | ||
|
|
||
| impl Hash for TestSubchannel { | ||
| fn hash<H: std::hash::Hasher>(&self, state: &mut H) { | ||
| self.address.hash(state); | ||
| } | ||
| } | ||
|
|
||
| impl PartialEq for TestSubchannel { | ||
| fn eq(&self, other: &Self) -> bool { | ||
| std::ptr::eq(self, other) | ||
| } | ||
| } | ||
| impl Eq for TestSubchannel {} | ||
|
|
||
| pub(crate) enum TestEvent { | ||
| NewSubchannel(Arc<dyn Subchannel>), | ||
| UpdatePicker(LbState), | ||
| RequestResolution, | ||
| Connect(Address), | ||
| ScheduleWork, | ||
| } | ||
|
|
||
| // TODO(easwars): Remove this and instead derive Debug. | ||
| impl Debug for TestEvent { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| match self { | ||
| Self::NewSubchannel(sc) => write!(f, "NewSubchannel({})", sc.address()), | ||
| Self::UpdatePicker(state) => write!(f, "UpdatePicker({})", state.connectivity_state), | ||
| Self::RequestResolution => write!(f, "RequestResolution"), | ||
| Self::Connect(addr) => write!(f, "Connect({})", addr.address.to_string()), | ||
| Self::ScheduleWork => write!(f, "ScheduleWork"), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// A test channel controller that forwards calls to a channel. This allows | ||
| /// tests to verify when a channel controller is asked to create subchannels or | ||
| /// update the picker. | ||
| pub(crate) struct TestChannelController { | ||
| pub(crate) tx_events: mpsc::UnboundedSender<TestEvent>, | ||
| } | ||
|
|
||
| impl ChannelController for TestChannelController { | ||
| fn new_subchannel(&mut self, address: &Address) -> Arc<dyn Subchannel> { | ||
| println!("new_subchannel called for address {}", address); | ||
dfawley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let notify = Arc::new(Notify::new()); | ||
| let subchannel: Arc<dyn Subchannel> = | ||
| Arc::new(TestSubchannel::new(address.clone(), self.tx_events.clone())); | ||
| self.tx_events | ||
| .send(TestEvent::NewSubchannel(subchannel.clone())) | ||
| .unwrap(); | ||
| subchannel | ||
| } | ||
| fn update_picker(&mut self, update: LbState) { | ||
| println!("picker_update called with {}", update.connectivity_state); | ||
| self.tx_events | ||
| .send(TestEvent::UpdatePicker(update)) | ||
| .unwrap(); | ||
| } | ||
| fn request_resolution(&mut self) { | ||
| self.tx_events.send(TestEvent::RequestResolution).unwrap(); | ||
| } | ||
| } | ||
|
|
||
| pub(crate) struct TestWorkScheduler { | ||
| pub(crate) tx_events: mpsc::UnboundedSender<TestEvent>, | ||
| } | ||
|
|
||
| impl WorkScheduler for TestWorkScheduler { | ||
| fn schedule_work(&self) { | ||
| self.tx_events.send(TestEvent::ScheduleWork).unwrap(); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.