-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfunctional.rs
219 lines (193 loc) · 6.3 KB
/
functional.rs
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
// Copyright 2020 Cognite AS
//! Functional test against an unleashed API server running locally.
//! Set environment variables as per config.rs to exercise this.
//!
//! Currently expects a feature called default with one strategy default
//! Additional features are ignored.
#[cfg(feature = "functional")]
mod tests {
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use std::{future::Future, pin::Pin};
use async_std::task;
use async_trait::async_trait;
use enum_map::Enum;
use futures_timer::Delay;
use serde::{Deserialize, Serialize};
use unleash_api_client::{client, config::EnvironmentConfig, http::HttpClient};
#[cfg(not(any(feature = "surf", feature = "reqwest")))]
compile_error!("Cannot run test suite without a client enabled");
#[allow(non_camel_case_types)]
#[derive(Debug, Deserialize, Serialize, Enum, Clone)]
enum UserFeatures {
default,
}
#[async_trait]
trait AsyncImpl {
type JoinHandle: Future<Output = ()>;
fn spawn<F>(f: F) -> Self::JoinHandle
where
F: Future<Output = ()> + Send + 'static;
async fn sleep(d: Duration);
}
#[cfg(feature = "surf")]
struct AsyncStdAsync;
#[cfg(feature = "surf")]
#[async_trait]
impl AsyncImpl for AsyncStdAsync {
type JoinHandle = task::JoinHandle<()>;
fn spawn<F>(f: F) -> Self::JoinHandle
where
F: Future<Output = ()> + Send + 'static,
{
task::spawn(f)
}
async fn sleep(d: Duration) {
thread::sleep(d)
}
}
#[cfg(or(feature = "reqwest", feature = "reqwest-11"))]
struct TokioJoinHandle {
inner: tokio::task::JoinHandle<()>,
}
impl Unpin for TokioJoinHandle {}
impl Future for TokioJoinHandle {
type Output = ();
fn poll(
mut self: std::pin::Pin<&mut Self>,
cx: &mut task::Context<'_>,
) -> core::task::Poll<Self::Output> {
let inner = Pin::new(&mut self.inner);
match inner.poll(cx) {
core::task::Poll::Pending => core::task::Poll::Pending,
core::task::Poll::Ready(r) => core::task::Poll::Ready(r.unwrap()),
}
}
}
#[cfg(or(feature = "reqwest", feature = "reqwest-11"))]
struct TokioAsync;
#[cfg(or(feature = "reqwest", feature = "reqwest-11"))]
#[async_trait]
impl AsyncImpl for TokioAsync {
type JoinHandle = TokioJoinHandle;
fn spawn<F>(f: F) -> Self::JoinHandle
where
F: Future<Output = ()> + Send + 'static,
{
TokioJoinHandle {
inner: tokio::spawn(f),
}
}
async fn sleep(d: Duration) {
tokio::time::sleep(d).await
}
}
async fn test_smoke_async<C>() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
where
C: HttpClient + Default + 'static,
{
let _ = simple_logger::init();
let config = EnvironmentConfig::from_env()?;
let client = client::ClientBuilder::default()
.interval(500)
.into_client::<UserFeatures, C>(
&config.api_url,
&config.app_name,
&config.instance_id,
config.secret,
)?;
client.register().await?;
futures::future::join(client.poll_for_updates(), async {
// Ensure we have features
Delay::new(Duration::from_millis(500)).await;
assert!(client.is_enabled(UserFeatures::default, None, false));
// Ensure the metrics get up-loaded
Delay::new(Duration::from_millis(500)).await;
client.stop_poll().await;
})
.await;
println!("got metrics");
Ok(())
}
#[cfg(feature = "surf")]
#[test]
fn test_smoke_async_surf() {
task::block_on(async {
test_smoke_async::<surf::Client>().await.unwrap();
});
}
#[cfg(feature = "reqwest")]
#[tokio::test]
async fn test_smoke_async_reqwest() {
test_smoke_async::<reqwest::Client>().await.unwrap();
}
#[cfg(feature = "reqwest-11")]
#[tokio::test]
async fn test_smoke_async_reqwest() {
test_smoke_async::<reqwest_11::Client>().await.unwrap();
}
async fn test_smoke_threaded<C, A>(
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
where
C: HttpClient + Default + 'static,
A: AsyncImpl,
<C as unleash_api_client::http::HttpClient>::RequestBuilder: std::marker::Send,
{
let _ = simple_logger::init();
let config = EnvironmentConfig::from_env()?;
let client = Arc::new(
client::ClientBuilder::default()
.interval(500)
.into_client::<_, C>(
&config.api_url,
&config.app_name,
&config.instance_id,
config.secret,
)?,
);
if let Err(e) = client.register().await {
Err(e)
} else {
Ok(())
}?;
// Spin off a polling thread
let poll_handle = client.clone();
let handler = A::spawn(async move {
// thread code
poll_handle.poll_for_updates().await;
});
// Ensure we have features
A::sleep(Duration::from_millis(500)).await;
assert!(client.is_enabled(UserFeatures::default, None, false));
// Ensure the metrics get up-loaded
A::sleep(Duration::from_millis(500));
client.stop_poll().await;
handler.await;
println!("got metrics");
Ok(())
}
#[cfg(feature = "surf")]
#[test]
fn test_smoke_threaded_surf() {
task::block_on(async {
test_smoke_threaded::<surf::Client, AsyncStdAsync>()
.await
.unwrap();
});
}
#[cfg(feature = "reqwest")]
#[tokio::test]
async fn test_smoke_threaded_reqwest() {
test_smoke_threaded::<reqwest::Client, TokioAsync>()
.await
.unwrap();
}
#[cfg(feature = "reqwest-11")]
#[tokio::test]
async fn test_smoke_threaded_reqwest() {
test_smoke_threaded::<reqwest_11::Client, TokioAsync>()
.await
.unwrap();
}
}