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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use ;
use Ordering;
use RuntimeLite;
use ;
use AddressResolver;
use ;
static BACKOFFS_LOCK: Mutex = new;
static BACKOFFS_COUNT: AtomicUsize = new;
pub
// /// Unit test for testing promised processor backoff
// #[cfg(any(feature = "test", test))]
// #[cfg_attr(docsrs, doc(cfg(any(feature = "test", test))))]
// #[allow(clippy::await_holding_lock, dead_code)]
// pub async fn listener_backoff<A, T, S>(
// s: S,
// kind: memberlist_core::transport::tests::AddressKind,
// ) -> Result<(), memberlist_core::tests::AnyError>
// where
// A: AddressResolver<ResolvedAddress = SocketAddr>,
// T:
// Transport<Resolver = A, ResolvedAddress = SocketAddr, Connection = S::Stream, Runtime = A::Runtime>,
// S: StreamLayer<Runtime = A::Runtime>,
// {
// struct TestStreamLayer<TS: StreamLayer> {
// _m: std::marker::PhantomData<TS>,
// }
// struct TestListener<TS: StreamLayer> {
// ln: TS::Listener,
// }
// impl<TS: StreamLayer> Listener for TestListener<TS> {
// type Stream = TS::Stream;
// async fn accept(&self) -> std::io::Result<(Self::Stream, SocketAddr)> {
// Err(std::io::Error::new(
// std::io::ErrorKind::Other,
// "always fail to accept for testing",
// ))
// }
// fn local_addr(&self) -> SocketAddr {
// self.ln.local_addr()
// }
// async fn shutdown(&self) -> std::io::Result<()> {
// self.ln.shutdown().await
// }
// }
// impl<TS: StreamLayer> StreamLayer for TestStreamLayer<TS> {
// type Runtime = TS::Runtime;
// type Listener = TestListener<TS>;
// type Stream = TS::Stream;
// type Options = ();
// async fn new(_: Self::Options) -> std::io::Result<Self>
// where
// Self: Sized,
// {
// unreachable!()
// }
// async fn connect(&self, _addr: SocketAddr) -> std::io::Result<Self::Stream> {
// unreachable!()
// }
// async fn bind(&self, _addr: SocketAddr) -> std::io::Result<Self::Listener> {
// unreachable!()
// }
// fn is_secure() -> bool {
// unreachable!()
// }
// }
// const TEST_TIME: std::time::Duration = std::time::Duration::from_secs(4);
// let _lock = BACKOFFS_LOCK.lock();
// let ln = s.bind(kind.next(0)).await?;
// let local_addr = ln.local_addr();
// let (shutdown_tx, shutdown_rx) = async_channel::bounded(1);
// let (stream_tx, stream_rx) = memberlist_core::transport::promised_stream::<T>();
// let task = <T::Runtime as RuntimeLite>::spawn(
// PromisedProcessor::<A, T, TestStreamLayer<S>> {
// stream_tx,
// ln: Arc::new(TestListener { ln }),
// local_addr,
// shutdown_rx,
// }
// .run(),
// );
// // sleep (+yield) for testTime seconds before asking the accept loop to shut down
// <T::Runtime as RuntimeLite>::sleep(TEST_TIME).await;
// shutdown_tx.close();
// // Verify that the wg was completed on exit (but without blocking this test)
// // maxDelay == 1s, so we will give the routine 1.25s to loop around and shut down.
// let (ctx, crx) = async_channel::bounded::<()>(1);
// <T::Runtime as RuntimeLite>::spawn_detach(async move {
// let _ = task.await;
// ctx.close();
// });
// futures::select! {
// _ = crx.recv().fuse() => {}
// _ = <T::Runtime as RuntimeLite>::sleep(Duration::from_millis(1250)).fuse() => {
// panic!("timed out waiting for transport waitgroup to be done after flagging shutdown");
// }
// }
// // In testTime==4s, we expect to loop approximately 12 times (and log approximately 11 errors),
// // with the following delays (in ms):
// // 0+5+10+20+40+80+160+320+640+1000+1000+1000 == 4275 ms
// // Too few calls suggests that the minDelay is not in force; too many calls suggests that the
// // maxDelay is not in force or that the back-off isn't working at all.
// // We'll leave a little flex; the important thing here is the asymptotic behavior.
// // If the minDelay or maxDelay in NetTransport#tcpListen() are modified, this test may fail
// // and need to be adjusted.
// let calls = BACKOFFS_COUNT.load(Ordering::SeqCst);
// assert!(8 < calls && calls < 14, "calls: {calls}");
// // no connections should have been accepted and sent to the channel
// assert!(stream_rx.is_empty());
// BACKOFFS_COUNT.store(0, Ordering::SeqCst);
// Ok(())
// }