-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlifespan.rs
More file actions
327 lines (287 loc) · 9.33 KB
/
lifespan.rs
File metadata and controls
327 lines (287 loc) · 9.33 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::PyDict;
use crate::asgi::AsgiInfo;
/// The lifespan scope exists for the duration of the event loop.
#[derive(Debug)]
pub struct LifespanScope {
/// An empty namespace where the application can persist state to be used
/// when handling subsequent requests. Optional; if missing the server
/// does not support this feature.
state: Option<Py<PyDict>>,
}
impl<'py> IntoPyObject<'py> for LifespanScope {
type Target = PyDict;
type Output = Bound<'py, Self::Target>;
type Error = PyErr;
fn into_pyobject(self, py: Python<'py>) -> PyResult<Self::Output> {
let dict = PyDict::new(py);
dict.set_item("type", "lifespan")?;
dict.set_item("asgi", AsgiInfo::new("3.0", "2.0").into_pyobject(py)?)?;
dict.set_item("state", self.state)?;
Ok(dict)
}
}
//
// Lifespan Scope
//
/// Lifespan Scope messages given to `receive()` function of an ASGI application.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum LifespanReceiveMessage {
LifespanStartup,
LifespanShutdown,
}
// Only ever converted from Rust to Python.
impl<'py> IntoPyObject<'py> for LifespanReceiveMessage {
type Target = PyDict;
type Output = Bound<'py, Self::Target>;
type Error = PyErr;
fn into_pyobject(self, py: Python<'py>) -> PyResult<Self::Output> {
let dict = PyDict::new(py);
match self {
LifespanReceiveMessage::LifespanStartup => {
dict.set_item("type", "lifespan.startup")?;
}
LifespanReceiveMessage::LifespanShutdown => {
dict.set_item("type", "lifespan.shutdown")?;
}
}
Ok(dict)
}
}
/// Lifespan Scope messages given to the `send()` function by an ASGI application.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum LifespanSendMessage {
LifespanStartupComplete,
LifespanShutdownComplete,
}
// Only ever converted from Python to Rust.
impl<'py> FromPyObject<'py> for LifespanSendMessage {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
let dict = ob.downcast::<PyDict>()?;
let message_type = dict.get_item("type")?.ok_or_else(|| {
PyValueError::new_err("Missing 'type' key in Lifespan send message dictionary")
})?;
let message_type: String = message_type.extract()?;
match message_type.as_str() {
"lifespan.startup.complete" => Ok(LifespanSendMessage::LifespanStartupComplete),
"lifespan.shutdown.complete" => Ok(LifespanSendMessage::LifespanShutdownComplete),
_ => Err(PyValueError::new_err(format!(
"Unknown Lifespan send message type: {message_type}"
))),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
macro_rules! dict_get {
($dict:expr, $key:expr) => {
$dict
.get_item($key)
.expect(&("Failed to get ".to_owned() + stringify!($key)))
.expect(&("Item \"".to_owned() + stringify!($key) + "\" not found"))
};
}
macro_rules! dict_extract {
($dict:expr, $key:expr, $type:ty) => {
dict_get!($dict, $key)
.extract::<$type>()
.expect(&("Unable to convert to ".to_owned() + stringify!($type)))
};
}
#[test]
fn test_lifespan_scope_into_pyobject() {
Python::with_gil(|py| {
let lifespan_scope = LifespanScope { state: None };
let py_obj = lifespan_scope.into_pyobject(py).unwrap();
assert_eq!(
dict_extract!(py_obj, "type", String),
"lifespan".to_string()
);
assert!(dict_get!(py_obj, "state").is_none());
let state = Some(PyDict::new(py).unbind());
let lifespan_scope = LifespanScope { state };
let py_obj = lifespan_scope.into_pyobject(py).unwrap();
assert_eq!(
dict_extract!(py_obj, "type", String),
"lifespan".to_string()
);
assert!(!dict_get!(py_obj, "state").is_none());
});
}
#[test]
fn test_lifespan_receive_message_into_pyobject() {
Python::with_gil(|py| {
let message = LifespanReceiveMessage::LifespanStartup;
let py_obj = message.into_pyobject(py).unwrap();
assert_eq!(
dict_extract!(py_obj, "type", String),
"lifespan.startup".to_string()
);
let message = LifespanReceiveMessage::LifespanShutdown;
let py_obj = message.into_pyobject(py).unwrap();
assert_eq!(
dict_extract!(py_obj, "type", String),
"lifespan.shutdown".to_string()
);
});
}
#[test]
fn test_lifespan_send_message_from_pyobject() {
Python::with_gil(|py| {
let dict = PyDict::new(py);
dict.set_item("type", "lifespan.shutdown.complete").unwrap();
let message: LifespanSendMessage = dict.extract().unwrap();
assert_eq!(message, LifespanSendMessage::LifespanShutdownComplete);
let dict = PyDict::new(py);
dict.set_item("type", "lifespan.startup.complete").unwrap();
let message: LifespanSendMessage = dict.extract().unwrap();
assert_eq!(message, LifespanSendMessage::LifespanStartupComplete);
});
}
#[test]
fn test_lifespan_send_message_from_pyobject_error_cases() {
Python::with_gil(|py| {
// Test missing 'type' key
let dict = PyDict::new(py);
let result: Result<LifespanSendMessage, _> = dict.extract();
assert!(result.is_err());
assert!(
result
.unwrap_err()
.to_string()
.contains("Missing 'type' key")
);
// Test unknown message type
let dict = PyDict::new(py);
dict.set_item("type", "unknown.message.type").unwrap();
let result: Result<LifespanSendMessage, _> = dict.extract();
assert!(result.is_err());
assert!(
result
.unwrap_err()
.to_string()
.contains("Unknown Lifespan send message type")
);
// Test non-dict object
let list = py.eval(c"[]", None, None).unwrap();
let result: Result<LifespanSendMessage, _> = list.extract();
assert!(result.is_err());
// Test invalid type value (not string)
let dict = PyDict::new(py);
dict.set_item("type", 123).unwrap();
let result: Result<LifespanSendMessage, _> = dict.extract();
assert!(result.is_err());
});
}
#[test]
fn test_lifespan_send_message_traits() {
// Test Debug trait
let msg1 = LifespanSendMessage::LifespanStartupComplete;
let msg2 = LifespanSendMessage::LifespanShutdownComplete;
let debug1 = format!("{:?}", msg1);
let debug2 = format!("{:?}", msg2);
assert!(debug1.contains("LifespanStartupComplete"));
assert!(debug2.contains("LifespanShutdownComplete"));
// Test Clone
let cloned1 = msg1.clone();
let cloned2 = msg2.clone();
// Test PartialEq and Eq
assert_eq!(msg1, cloned1);
assert_eq!(msg2, cloned2);
assert_ne!(msg1, msg2);
// Test Hash
use std::collections::HashSet;
let mut set = HashSet::new();
set.insert(msg1);
set.insert(cloned1); // Should not increase size due to equality
set.insert(msg2);
assert_eq!(set.len(), 2); // Only unique messages
}
#[test]
fn test_lifespan_receive_message_traits() {
// Test all the derive traits for LifespanReceiveMessage
let msg1 = LifespanReceiveMessage::LifespanStartup;
let msg2 = LifespanReceiveMessage::LifespanShutdown;
// Test Debug
let debug1 = format!("{:?}", msg1);
let debug2 = format!("{:?}", msg2);
assert!(debug1.contains("LifespanStartup"));
assert!(debug2.contains("LifespanShutdown"));
// Test Clone and Copy
let cloned1 = msg1.clone();
let copied1 = msg1;
// Test PartialEq and Eq
assert_eq!(msg1, cloned1);
assert_eq!(msg1, copied1);
assert_ne!(msg1, msg2);
// Test Hash
use std::collections::HashSet;
let mut set = HashSet::new();
set.insert(msg1);
set.insert(copied1); // Should not increase size due to equality
set.insert(msg2);
assert_eq!(set.len(), 2); // Only unique messages
}
#[test]
fn test_lifespan_scope_with_populated_state() {
Python::with_gil(|py| {
// Create a state dictionary with some data
let state_dict = PyDict::new(py);
state_dict.set_item("initialized", true).unwrap();
state_dict.set_item("counter", 42).unwrap();
let lifespan_scope = LifespanScope {
state: Some(state_dict.unbind()),
};
let py_obj = lifespan_scope.into_pyobject(py).unwrap();
// Verify the scope structure
assert_eq!(
dict_extract!(py_obj, "type", String),
"lifespan".to_string()
);
// Verify ASGI info is present
let asgi_info = dict_get!(py_obj, "asgi");
let asgi_dict = asgi_info.downcast::<PyDict>().unwrap();
assert_eq!(
asgi_dict
.get_item("version")
.unwrap()
.unwrap()
.extract::<String>()
.unwrap(),
"3.0"
);
assert_eq!(
asgi_dict
.get_item("spec_version")
.unwrap()
.unwrap()
.extract::<String>()
.unwrap(),
"2.0"
);
// Verify state is preserved
let state_obj = dict_get!(py_obj, "state");
let state_dict = state_obj.downcast::<PyDict>().unwrap();
assert_eq!(
state_dict
.get_item("initialized")
.unwrap()
.unwrap()
.extract::<bool>()
.unwrap(),
true
);
assert_eq!(
state_dict
.get_item("counter")
.unwrap()
.unwrap()
.extract::<i32>()
.unwrap(),
42
);
});
}
}