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
use futures::{channel::mpsc, stream::select, StreamExt, TryStreamExt};
use super::switch::switch_convert_stream;
use crate::{connector::passthru_pair, Pair};
pub fn errors_to_sink<S, R, Ei, Eo>(pair: impl Pair<S, R, Ei> + 'static) -> impl Pair<S, R, Eo>
where
R: Send + 'static,
S: Send + 'static,
Ei: Send + 'static,
Eo: Into<Ei> + Send + 'static,
{
let (theirs, ours) = passthru_pair::<S, R, Eo>();
let (p_sender, p_receiver) = pair.split();
let (o_sender, o_receiver) = ours.split();
let (e_sender, e_receiver) = mpsc::channel::<Result<_, Ei>>(0);
let o_receiver = o_receiver.map_err(Into::into);
tokio::spawn(async move {
let send_f = select(e_receiver, o_receiver).map(Ok).forward(p_sender);
let rcv_f =
switch_convert_stream(p_receiver, |x| x.map(Ok).map_err(Err), o_sender, e_sender);
tokio::select! {
res = send_f => {
tracing::debug!("errors_to_sink send_f exited with {res:?}");
}
res = rcv_f => {
tracing::debug!("errors_to_sink rcv_f exited with {res:?}");
}
}
tracing::debug!("errors_to_sink thread exiting");
});
theirs
}
pub fn errors_to_stream<S, R, Ei, Eo>(pair: impl Pair<S, R, Ei> + 'static) -> impl Pair<S, R, Eo>
where
R: Send + 'static,
S: Send + 'static,
Ei: Into<Eo> + Send + 'static,
Eo: Send + 'static,
{
let (theirs, ours) = passthru_pair::<S, R, Eo>();
let (p_sender, p_receiver) = pair.split();
let (o_sender, o_receiver) = ours.split();
let (e_sender, e_receiver) = mpsc::channel::<Result<R, Eo>>(0);
let p_receiver = p_receiver.map_err(Into::into);
tokio::spawn(async move {
let send_f = select(e_receiver, p_receiver).map(Ok).forward(o_sender);
let rcv_f =
switch_convert_stream(o_receiver, |x| x.map(Ok).map_err(Err), p_sender, e_sender);
tokio::select! {
res = send_f => {
tracing::debug!("errors_to_stream send_f exited with {res:?}");
}
res = rcv_f => {
tracing::debug!("errors_to_stream rcv_f exited with {res:?}");
}
}
tracing::debug!("errors_to_stream thread exiting");
});
theirs
}
#[cfg(test)]
mod test {
use super::*;
use crate::{
connector::passthru_pair,
jrpc::{no_response, Error as JrpcError, Id},
JsonRpcErr,
};
use futures::{FutureExt, SinkExt, StreamExt};
use rstest::rstest;
use std::pin::Pin;
#[rstest]
#[case::to_sink(0)]
#[case::to_stream(1)]
#[tokio::test]
async fn test_errors(#[case] direction: usize) {
let (mut errs, mut noerrs) = if direction == 0 {
let (errs, noerrs) = passthru_pair::<usize, usize, JsonRpcErr>();
let noerrs = errors_to_sink::<_, _, _, JrpcError>(noerrs);
(
Box::pin(errs) as Pin<Box<dyn Pair<usize, usize, JsonRpcErr>>>,
Box::pin(noerrs) as Pin<Box<dyn Pair<usize, usize, JrpcError>>>,
)
} else {
let (noerrs, errs) = passthru_pair::<usize, usize, JrpcError>();
let errs = errors_to_stream::<_, _, _, JsonRpcErr>(errs);
(
Box::pin(errs) as Pin<Box<dyn Pair<usize, usize, JsonRpcErr>>>,
Box::pin(noerrs) as Pin<Box<dyn Pair<usize, usize, JrpcError>>>,
)
};
errs.send(Ok(0)).await.unwrap();
assert_eq!(noerrs.next().await.transpose().unwrap(), Some(0));
assert!(errs.next().now_or_never().is_none());
noerrs.send(Ok(1)).await.unwrap();
assert_eq!(errs.next().await.transpose().unwrap(), Some(1));
assert!(noerrs.next().now_or_never().is_none());
errs.send(Err(no_response(None as Option<String>)))
.await
.unwrap();
assert!(matches!(errs.next().await, Some(Err(JsonRpcErr::Jrpc(_)))));
assert!(noerrs.next().now_or_never().is_none());
noerrs
.send(Err(JrpcError::new(Id::from(0), 0, "asdf", None)))
.await
.unwrap();
assert!(matches!(errs.next().await, Some(Err(JsonRpcErr::Jrpc(_)))));
assert!(noerrs.next().now_or_never().is_none());
}
}