qiniu_http_client/client/chooser/
direct.rs1use super::{ChooseOptions, Chooser, ChooserFeedback, ChosenResults, IpAddrWithPort};
2
3#[derive(Clone, Copy, Debug, Default)]
7pub struct DirectChooser;
8
9impl Chooser for DirectChooser {
10 #[inline]
11 fn choose(&self, ips: &[IpAddrWithPort], _opts: ChooseOptions) -> ChosenResults {
12 ips.to_owned().into()
13 }
14
15 #[inline]
16 fn feedback(&self, _feedback: ChooserFeedback) {}
17}
18
19#[cfg(test)]
20mod tests {
21 use super::*;
22 use anyhow::Result;
23 use once_cell::sync::Lazy;
24 use std::{
25 net::{IpAddr, Ipv4Addr},
26 num::NonZeroU16,
27 };
28
29 static IPS_WITHOUT_PORT: Lazy<Vec<IpAddrWithPort>> = Lazy::new(|| {
30 vec![
31 IpAddrWithPort::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)), None),
32 IpAddrWithPort::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 2)), None),
33 IpAddrWithPort::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 3)), None),
34 ]
35 });
36
37 static IPS_WITH_PORT: Lazy<Vec<IpAddrWithPort>> = Lazy::new(|| {
38 vec![
39 IpAddrWithPort::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)), NonZeroU16::new(443)),
40 IpAddrWithPort::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 2)), NonZeroU16::new(443)),
41 IpAddrWithPort::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 3)), NonZeroU16::new(443)),
42 ]
43 });
44
45 #[test]
46 fn test_direct_chooser() -> Result<()> {
47 let chooser = DirectChooser;
48 assert_eq!(
49 chooser.choose(&IPS_WITHOUT_PORT, Default::default()).into_ip_addrs(),
50 IPS_WITHOUT_PORT.to_vec(),
51 );
52 assert_eq!(
53 chooser.choose(&IPS_WITH_PORT, Default::default()).into_ip_addrs(),
54 IPS_WITH_PORT.to_vec(),
55 );
56 Ok(())
57 }
58
59 #[cfg(feature = "async")]
60 #[tokio::test]
61 async fn async_test_direct_chooser() -> Result<()> {
62 let chooser = DirectChooser;
63 assert_eq!(
64 chooser
65 .async_choose(&IPS_WITHOUT_PORT, Default::default())
66 .await
67 .into_ip_addrs(),
68 IPS_WITHOUT_PORT.to_vec(),
69 );
70 assert_eq!(
71 chooser
72 .async_choose(&IPS_WITH_PORT, Default::default())
73 .await
74 .into_ip_addrs(),
75 IPS_WITH_PORT.to_vec(),
76 );
77 Ok(())
78 }
79}