-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathenv2.rs
More file actions
284 lines (242 loc) · 8.5 KB
/
Copy pathenv2.rs
File metadata and controls
284 lines (242 loc) · 8.5 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
use std::{
any::Any,
collections::BTreeMap,
sync::{Arc, LockResult, Mutex, MutexGuard, RwLock},
};
use chrono::{DateTime, Utc};
use once_cell::sync::Lazy;
use thiserror::Error;
use crate::runtime::Env;
use super::{default_fetch_handler, FetchHandler, Request};
static ENV: Lazy<TestEnv2> = Lazy::new(|| TestEnv2::new(None, None));
pub struct TestEnv2 {
// used to lock the struct while the test is running
env_mutex: Mutex<()>,
pub fetch_handler: RwLock<FetchHandler>,
pub requests: RwLock<Vec<Request>>,
pub storage: RwLock<BTreeMap<String, String>>,
pub events: RwLock<Vec<Box<dyn Any + Send + Sync + 'static>>>,
pub states: RwLock<Vec<Box<dyn Any + Send + Sync + 'static>>>,
pub now: RwLock<DateTime<Utc>>,
pub runtime: tokio::runtime::Runtime,
// pub fetch_handler: FetchHandler,
// pub requests: Vec<Request>,
// pub storage: BTreeMap<String, String>,
// pub events: Vec<Box<dyn Any + Send + Sync + 'static>>,
// pub states: Vec<Box<dyn Any + Send + Sync + 'static>>,
// pub now: DateTime<Utc>,
}
impl TestEnv2 {
pub fn new(fetcher: Option<FetchHandler>, now: impl Into<Option<DateTime<Utc>>>) -> Self {
let fetcher: Option<FetchHandler> = fetcher.into();
let now_datetime = now.into();
Self {
env_mutex: Default::default(),
fetch_handler: RwLock::new(fetcher.unwrap_or(Box::new(default_fetch_handler))),
requests: Default::default(),
storage: Default::default(),
events: Default::default(),
states: Default::default(),
now: RwLock::new(now_datetime.unwrap_or(Utc::now())),
runtime: tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.expect("Should build tokio runtime"),
}
}
// with Lazy static
pub fn reset() -> LockResult<MutexGuard<'static, ()>> {
let env_mutex = ENV.env_mutex.lock()?;
*ENV.fetch_handler.write().unwrap() = Box::new(default_fetch_handler);
*ENV.requests.write().unwrap() = vec![];
*ENV.storage.write().unwrap() = BTreeMap::new();
*ENV.events.write().unwrap() = vec![];
*ENV.states.write().unwrap() = vec![];
*ENV.now.write().unwrap() = Utc::now();
Ok(env_mutex)
// with Env lock
// let mut env_lock = ENV.lock()?;
// env_lock.fetch_handler = Box::new(default_fetch_handler);
// env_lock.requests = Default::default();
// env_lock.storage = Default::default();
// env_lock.events = Default::default();
// env_lock.states = Default::default();
// env_lock.now = Utc::now();
// Ok(env_lock)
}
fn set_now(&self, now: DateTime<Utc>) {
*self.now.write().expect("Should lock Now") = now;
}
// /// resets the TestEnv and set's the values from `new`
// pub fn reset_with(new: TestEnv2) -> LockResult<MutexGuard<'static, ()>> {
// let env_mutex = ENV.env_mutex.lock()?;
// *ENV.fetch_handler.write().unwrap() = *new.fetch_handler.read().unwrap();
// *ENV.requests.write().unwrap() = (*new.requests.read().unwrap()).clone();
// *ENV.storage.write().unwrap() = (*new.storage.read().unwrap()).clone();
// *ENV.states.write().unwrap() = (*new.states.read().unwrap()).clone();
// *ENV.events.write().unwrap() = (*new.events.read().unwrap()).clone();
// *ENV.now.write().unwrap() = *new.now.read().unwrap();
// Ok(env_mutex)
// }
// pub fn reset_with(new: TestEnv2) -> LockResult<MutexGuard<'static, TestEnv2>> {
// let mut env_lock = ENV.lock()?;
// env_lock.fetch_handler = new.fetch_handler;
// env_lock.requests = new.requests;
// env_lock.storage = new.storage;
// env_lock.events = new.events;
// env_lock.states = new.states;
// env_lock.now = new.now;
// Ok(env_lock)
// }
}
impl Default for TestEnv2 {
fn default() -> Self {
Self::new(Some(Box::new(default_fetch_handler)), Utc::now())
}
}
impl Env for TestEnv2 {
fn fetch<
IN: serde::Serialize + crate::runtime::ConditionalSend + 'static,
OUT: for<'de> serde::Deserialize<'de> + crate::runtime::ConditionalSend + 'static,
>(
request: http::Request<IN>,
) -> crate::runtime::TryEnvFuture<OUT> {
todo!()
}
fn get_storage<
T: for<'de> serde::Deserialize<'de> + crate::runtime::ConditionalSend + 'static,
>(
key: &str,
) -> crate::runtime::TryEnvFuture<Option<T>> {
todo!()
}
fn set_storage<T: serde::Serialize>(
key: &str,
value: Option<&T>,
) -> crate::runtime::TryEnvFuture<()> {
todo!()
}
fn exec_concurrent<
F: futures::Future<Output = ()> + crate::runtime::ConditionalSend + 'static,
>(
future: F,
) {
todo!()
}
fn exec_sequential<
F: futures::Future<Output = ()> + crate::runtime::ConditionalSend + 'static,
>(
future: F,
) {
todo!()
}
fn now() -> DateTime<Utc> {
*ENV.now.read().expect("Failed to read now")
}
fn flush_analytics() -> crate::runtime::EnvFuture<'static, ()> {
todo!()
}
fn analytics_context(
ctx: &crate::models::ctx::Ctx,
streaming_server: &crate::models::streaming_server::StreamingServer,
path: &str,
) -> serde_json::Value {
todo!()
}
fn log(message: String) {
todo!()
}
}
#[derive(Error, Debug)]
pub enum Error {
#[error("Failed to lock local Env mutex")]
EnvLock(#[from] std::sync::PoisonError<MutexGuard<'static, ()>>),
}
impl TestEnv2 {
pub fn new_case() -> Result<(TestCaseEnv, MutexGuard<'static, ()>), Error> {
// let env_mutex = ENV_MUTEX.with(|mutex| mutex.lock())?;
// Ok((TestCaseEnv::default(), env_mutex))
todo!("Does not work with thread_local because of lifetime")
}
}
// #[derive(Debug, PartialEq, Eq)]
pub struct TestCaseEnv {
inner: Arc<TestCaseInner>,
}
impl Default for TestCaseEnv {
fn default() -> Self {
Self {
inner: Arc::new(TestCaseInner {
fetch_handler: Box::new(default_fetch_handler),
requests: Default::default(),
storage: Default::default(),
events: Default::default(),
states: Default::default(),
now: Utc::now(),
}),
}
}
}
impl TestCaseEnv {
pub fn new(
fetcher: impl Into<Option<FetchHandler>>,
now: impl Into<Option<DateTime<Utc>>>,
) -> Self {
let fetcher: Option<FetchHandler> = fetcher.into();
let now_datetime = now.into();
Self {
inner: Arc::new(TestCaseInner {
fetch_handler: fetcher.unwrap_or(Box::new(default_fetch_handler)),
requests: Default::default(),
storage: Default::default(),
events: Default::default(),
states: Default::default(),
now: now_datetime.unwrap_or(Utc::now()),
}),
}
}
}
// #[derive(Debug, PartialEq, Eq)]
struct TestCaseInner {
pub fetch_handler: FetchHandler,
pub requests: Vec<Request>,
pub storage: BTreeMap<String, String>,
pub events: Vec<Box<dyn Any + Send + Sync + 'static>>,
pub states: Vec<Box<dyn Any + Send + Sync + 'static>>,
pub now: DateTime<Utc>,
}
#[cfg(test)]
mod test {
use std::{panic, thread::LocalKey};
use chrono::{TimeZone, Utc};
use super::*;
fn setup<'a>() -> LockResult<MutexGuard<'static, ()>> {
TestEnv2::reset()
}
/// any additional teardown actions
fn teardown() {}
fn run_test<T>(test: T) -> ()
where
// with thread_local
T: FnOnce(&'static TestEnv2) -> () + panic::UnwindSafe,
{
let env_guard = setup().expect("Should lock the environment");
let result = panic::catch_unwind(|| test(&*ENV));
// test has finished, drop the Mutex guard
drop(env_guard);
teardown();
assert!(result.is_ok())
}
#[tokio::test]
async fn test_single_test_case() {
run_test(|env| {
env.set_now(Utc.with_ymd_and_hms(2020, 6, 27, 14, 20, 5).unwrap());
let env_datetime = call_env_function::<TestEnv2>();
let expected = Utc.with_ymd_and_hms(2020, 6, 27, 14, 20, 5).unwrap();
assert_eq!(expected, env_datetime);
});
}
fn call_env_function<E: Env + 'static>() -> DateTime<Utc> {
E::now()
}
}