-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool_handle.rs
More file actions
198 lines (173 loc) · 5.86 KB
/
pool_handle.rs
File metadata and controls
198 lines (173 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//! Unit tests for the `PoolHandle` fairness API.
use std::{
sync::{
Arc,
atomic::{AtomicUsize, Ordering},
},
time::Duration,
};
use rstest::{fixture, rstest};
use tokio::{
sync::Mutex,
time::{advance, timeout},
};
use crate::{
client::{ClientError, ClientPoolConfig, PoolFairnessPolicy, PoolHandle},
test_helpers::{Ping, Pong, PoolTestServer, TestClientPool, build_pooled_client},
};
type TestResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
#[rustfmt::skip]
#[fixture]
fn client_pool_config() -> ClientPoolConfig {
ClientPoolConfig::default()
}
async fn build_handle_pool(
config: ClientPoolConfig,
) -> Result<(PoolTestServer, TestClientPool), ClientError> {
let server = PoolTestServer::start().await?;
let pool = build_pooled_client(server.addr, config, Arc::new(AtomicUsize::new(0))).await?;
Ok((server, pool))
}
async fn build_preamble_pool(
config: ClientPoolConfig,
) -> Result<(PoolTestServer, TestClientPool, Arc<AtomicUsize>), ClientError> {
let preamble_callback_count = Arc::new(AtomicUsize::new(0));
let server = PoolTestServer::start().await?;
let pool =
build_pooled_client(server.addr, config, Arc::clone(&preamble_callback_count)).await?;
Ok((server, pool, preamble_callback_count))
}
async fn acquire_and_record(
mut handle: PoolHandle<
crate::serializer::BincodeSerializer,
crate::test_helpers::ClientHello,
(),
>,
label: &'static str,
rounds: usize,
grants: Arc<Mutex<Vec<&'static str>>>,
) -> Result<(), ClientError> {
for _ in 0..rounds {
let lease = handle.acquire().await?;
grants.lock().await.push(label);
tokio::task::yield_now().await;
drop(lease);
}
Ok(())
}
#[rstest]
#[tokio::test(flavor = "current_thread")]
async fn round_robin_handles_share_one_socket_fairly(
client_pool_config: ClientPoolConfig,
) -> TestResult {
let (_server, pool) = build_handle_pool(
client_pool_config
.pool_size(1)
.max_in_flight_per_socket(1)
.fairness_policy(PoolFairnessPolicy::RoundRobin),
)
.await?;
let grants = Arc::new(Mutex::new(Vec::new()));
let first = pool.handle();
let second = pool.handle();
let left = tokio::spawn(acquire_and_record(first, "a", 3, Arc::clone(&grants)));
let right = tokio::spawn(acquire_and_record(second, "b", 3, Arc::clone(&grants)));
let (left_result, right_result) = tokio::join!(left, right);
left_result??;
right_result??;
let observed = grants.lock().await.clone();
assert_eq!(observed, vec!["a", "b", "a", "b", "a", "b"]);
Ok(())
}
#[rstest]
#[tokio::test(flavor = "current_thread")]
async fn fifo_policy_preserves_wait_order(client_pool_config: ClientPoolConfig) -> TestResult {
let (_server, pool) = build_handle_pool(
client_pool_config
.pool_size(1)
.max_in_flight_per_socket(1)
.fairness_policy(PoolFairnessPolicy::Fifo),
)
.await?;
let blocker = pool.acquire().await?;
let grants = Arc::new(Mutex::new(Vec::new()));
let first = tokio::spawn(acquire_and_record(
pool.handle(),
"first",
1,
Arc::clone(&grants),
));
tokio::task::yield_now().await;
let second = tokio::spawn(acquire_and_record(
pool.handle(),
"second",
1,
Arc::clone(&grants),
));
tokio::task::yield_now().await;
let third = tokio::spawn(acquire_and_record(
pool.handle(),
"third",
1,
Arc::clone(&grants),
));
tokio::task::yield_now().await;
drop(blocker);
first.await??;
second.await??;
third.await??;
let observed = grants.lock().await.clone();
assert_eq!(observed, vec!["first", "second", "third"]);
Ok(())
}
#[rstest]
#[tokio::test(flavor = "current_thread")]
async fn handle_acquire_respects_back_pressure(client_pool_config: ClientPoolConfig) -> TestResult {
let (_server, pool) = build_handle_pool(client_pool_config.pool_size(1)).await?;
let mut first = pool.handle();
let mut second = pool.handle();
let held_lease = first.acquire().await?;
let blocked = timeout(Duration::from_millis(25), second.acquire()).await;
assert!(blocked.is_err(), "second handle should stay blocked");
drop(held_lease);
let recovered = timeout(Duration::from_millis(100), second.acquire()).await?;
let _recovered = recovered?;
Ok(())
}
#[rstest]
#[tokio::test]
async fn handle_path_preserves_warm_reuse_and_preamble(
client_pool_config: ClientPoolConfig,
) -> TestResult {
let (server, pool, preamble_callback_count) =
build_preamble_pool(client_pool_config.pool_size(1)).await?;
let mut handle = pool.handle();
let first: Pong = handle.call(&Ping(7)).await?;
let second: Pong = handle.call(&Ping(8)).await?;
assert_eq!(first, Pong(7));
assert_eq!(second, Pong(8));
assert_eq!(preamble_callback_count.load(Ordering::SeqCst), 1);
assert_eq!(server.preamble_count(), 1);
assert_eq!(server.connection_count(), 1);
Ok(())
}
#[rstest]
#[tokio::test(start_paused = true, flavor = "current_thread")]
async fn handle_path_recycles_after_idle_timeout(
client_pool_config: ClientPoolConfig,
) -> TestResult {
let idle_timeout = Duration::from_millis(50);
let (server, pool, preamble_callback_count) =
build_preamble_pool(client_pool_config.pool_size(1).idle_timeout(idle_timeout)).await?;
let mut handle = pool.handle();
let first: Pong = handle.call(&Ping(1)).await?;
assert_eq!(first, Pong(1));
advance(idle_timeout + idle_timeout).await;
tokio::task::yield_now().await;
let second: Pong = handle.call(&Ping(2)).await?;
assert_eq!(second, Pong(2));
assert_eq!(preamble_callback_count.load(Ordering::SeqCst), 2);
assert_eq!(server.preamble_count(), 2);
assert_eq!(server.connection_count(), 2);
Ok(())
}