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
use http::{HeaderMap, HeaderValue};
type BoxError = Box<dyn std::error::Error + Send + Sync>;
pub trait BodyCallback: Send + Sync {
fn update(&mut self, bytes: &[u8]) -> Result<(), BoxError> {
let _ = bytes;
Ok(())
}
fn trailers(&self) -> Result<Option<HeaderMap<HeaderValue>>, BoxError> {
Ok(None)
}
fn make_new(&self) -> Box<dyn BodyCallback>;
}
impl BodyCallback for Box<dyn BodyCallback> {
fn update(&mut self, bytes: &[u8]) -> Result<(), BoxError> {
self.as_mut().update(bytes)
}
fn trailers(&self) -> Result<Option<HeaderMap<HeaderValue>>, BoxError> {
self.as_ref().trailers()
}
fn make_new(&self) -> Box<dyn BodyCallback> {
self.as_ref().make_new()
}
}
#[cfg(test)]
mod tests {
use super::{BodyCallback, BoxError};
use crate::body::SdkBody;
use crate::byte_stream::ByteStream;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
#[tracing_test::traced_test]
#[tokio::test]
async fn callbacks_are_called_for_update() {
struct CallbackA;
struct CallbackB;
impl BodyCallback for CallbackA {
fn update(&mut self, _bytes: &[u8]) -> Result<(), BoxError> {
tracing::debug!("callback A was called");
Ok(())
}
fn make_new(&self) -> Box<dyn BodyCallback> {
Box::new(Self)
}
}
impl BodyCallback for CallbackB {
fn update(&mut self, _bytes: &[u8]) -> Result<(), BoxError> {
tracing::debug!("callback B was called");
Ok(())
}
fn make_new(&self) -> Box<dyn BodyCallback> {
Box::new(Self)
}
}
let mut body = SdkBody::from("test");
body.with_callback(Box::new(CallbackA))
.with_callback(Box::new(CallbackB));
let body = ByteStream::from(body).collect().await.unwrap().into_bytes();
let body = std::str::from_utf8(&body).unwrap();
assert_eq!(body, "test");
assert!(logs_contain("callback A was called"));
assert!(logs_contain("callback B was called"));
}
struct TestCallback {
times_called: Arc<AtomicUsize>,
}
impl BodyCallback for TestCallback {
fn update(&mut self, _bytes: &[u8]) -> Result<(), BoxError> {
self.times_called.fetch_add(1, Ordering::SeqCst);
Ok(())
}
fn make_new(&self) -> Box<dyn BodyCallback> {
Box::new(Self {
times_called: Arc::new(AtomicUsize::new(0)),
})
}
}
#[tokio::test]
async fn callback_for_buffered_body_is_called_once() {
let times_called = Arc::new(AtomicUsize::new(0));
let test_text: String = (0..=1000)
.into_iter()
.map(|n| format!("line {}\n", n))
.collect();
{
let mut body = SdkBody::from(test_text);
let callback = TestCallback {
times_called: times_called.clone(),
};
body.with_callback(Box::new(callback));
let _body = ByteStream::new(body).collect().await.unwrap().into_bytes();
}
let times_called = Arc::try_unwrap(times_called).unwrap();
let times_called = times_called.into_inner();
assert_eq!(times_called, 1);
}
#[tracing_test::traced_test]
#[tokio::test]
async fn callback_for_streaming_body_is_called_per_chunk() {
let times_called = Arc::new(AtomicUsize::new(0));
{
let test_stream = tokio_stream::iter(
(1..=1000)
.into_iter()
.map(|n| -> Result<String, std::io::Error> { Ok(format!("line {}\n", n)) }),
);
let mut body = SdkBody::from(hyper::body::Body::wrap_stream(test_stream));
tracing::trace!("{:?}", body);
assert!(logs_contain("Streaming(Body(Streaming))"));
let callback = TestCallback {
times_called: times_called.clone(),
};
body.with_callback(Box::new(callback));
let _body = ByteStream::new(body).collect().await.unwrap().into_bytes();
}
let times_called = Arc::try_unwrap(times_called).unwrap();
let times_called = times_called.into_inner();
assert_eq!(times_called, 1000);
}
}