Skip to content

Commit 3dd4514

Browse files
committed
Add optional tokio-runtime feature
Signed-off-by: Aaron Erhardt <[email protected]>
1 parent 433d1d9 commit 3dd4514

File tree

3 files changed

+111
-91
lines changed

3 files changed

+111
-91
lines changed

Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@ authors = [
2222
]
2323

2424
[features]
25+
#default = ["async-std-runtime"]
26+
default = ["tokio-runtime"]
27+
async-std-runtime = ["mongodb/async-std-runtime"]
28+
tokio-runtime = ["mongodb/tokio-runtime"]
2529

2630
[dependencies]
2731
async-session = "3"
28-
mongodb = { version = "2.1", default-features = false, features = [
29-
"async-std-runtime",
32+
mongodb = { package = "mongodb", version = "2.1", default-features = false, features = [
3033
"bson-chrono-0_4",
3134
] }
3235

3336
[dev-dependencies]
3437
async-std = { version = "1.10", features = ["attributes"] }
3538
lazy_static = "1"
3639
rand = { version = "0.8" }
40+
tokio = { version = "1", features = ["rt"] }

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818
use async_session::chrono::{Duration, Utc};
1919
use async_session::{async_trait, Result, Session, SessionStore};
20-
use mongodb::bson::{self, Document};
21-
use mongodb::bson::{doc, Bson};
20+
use mongodb::bson::{self, doc, Bson, Document};
2221
use mongodb::options::{ReplaceOptions, SelectionCriteria};
2322
use mongodb::Client;
2423

tests/test.rs

Lines changed: 104 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
#[cfg(test)]
22
mod tests {
3+
use std::future::Future;
4+
5+
#[cfg(feature = "async-std-runtime")]
6+
fn run_test<F: Future>(future: F) -> F::Output {
7+
async_std::task::block_on(future)
8+
}
9+
#[cfg(feature = "tokio-runtime")]
10+
fn run_test<F: Future>(future: F) -> F::Output {
11+
let rt = tokio::runtime::Runtime::new().unwrap();
12+
rt.block_on(future)
13+
}
14+
315
use async_mongodb_session::*;
416
use async_session::{Session, SessionStore};
517
use lazy_static::lazy_static;
@@ -14,106 +26,111 @@ mod tests {
1426
format!("mongodb://{}:{}/", HOST.as_str(), PORT.as_str());
1527
}
1628

17-
#[test]
18-
fn test_from_client() -> async_session::Result {
19-
async_std::task::block_on(async {
20-
let client_options = match ClientOptions::parse(CONNECTION_STRING.as_str()).await {
21-
Ok(c) => c,
22-
Err(e) => panic!("Client Options Failed: {}", e),
23-
};
24-
25-
let client = match Client::with_options(client_options) {
26-
Ok(c) => c,
27-
Err(e) => panic!("Client Creation Failed: {}", e),
28-
};
29-
30-
let store = MongodbSessionStore::from_client(client, "db_name", "collection");
31-
let mut rng = rand::thread_rng();
32-
let n2: u16 = rng.gen();
33-
let key = format!("key-{}", n2);
34-
let value = format!("value-{}", n2);
35-
let mut session = Session::new();
36-
session.insert(&key, &value)?;
37-
38-
let cookie_value = store.store_session(session).await?.unwrap();
39-
let session = store.load_session(cookie_value).await?.unwrap();
40-
assert_eq!(&session.get::<String>(&key).unwrap(), &value);
41-
42-
Ok(())
43-
})
29+
async fn from_client() -> async_session::Result {
30+
let client_options = match ClientOptions::parse(CONNECTION_STRING.as_str()).await {
31+
Ok(c) => c,
32+
Err(e) => panic!("Client Options Failed: {}", e),
33+
};
34+
35+
let client = match Client::with_options(client_options) {
36+
Ok(c) => c,
37+
Err(e) => panic!("Client Creation Failed: {}", e),
38+
};
39+
40+
let store = MongodbSessionStore::from_client(client, "db_name", "collection");
41+
let mut rng = rand::thread_rng();
42+
let n2: u16 = rng.gen();
43+
let key = format!("key-{}", n2);
44+
let value = format!("value-{}", n2);
45+
let mut session = Session::new();
46+
session.insert(&key, &value)?;
47+
48+
let cookie_value = store.store_session(session).await?.unwrap();
49+
let session = store.load_session(cookie_value).await?.unwrap();
50+
assert_eq!(&session.get::<String>(&key).unwrap(), &value);
51+
52+
Ok(())
4453
}
4554

46-
#[test]
47-
fn test_new() -> async_session::Result {
48-
async_std::task::block_on(async {
49-
let store =
50-
MongodbSessionStore::new(&CONNECTION_STRING, "db_name", "collection").await?;
51-
52-
let mut rng = rand::thread_rng();
53-
let n2: u16 = rng.gen();
54-
let key = format!("key-{}", n2);
55-
let value = format!("value-{}", n2);
56-
let mut session = Session::new();
57-
session.insert(&key, &value)?;
58-
59-
let cookie_value = store.store_session(session).await?.unwrap();
60-
let session = store.load_session(cookie_value).await?.unwrap();
61-
assert_eq!(&session.get::<String>(&key).unwrap(), &value);
62-
63-
Ok(())
64-
})
55+
async fn new() -> async_session::Result {
56+
let store = MongodbSessionStore::new(&CONNECTION_STRING, "db_name", "collection").await?;
57+
58+
let mut rng = rand::thread_rng();
59+
let n2: u16 = rng.gen();
60+
let key = format!("key-{}", n2);
61+
let value = format!("value-{}", n2);
62+
let mut session = Session::new();
63+
session.insert(&key, &value)?;
64+
65+
let cookie_value = store.store_session(session).await?.unwrap();
66+
let session = store.load_session(cookie_value).await?.unwrap();
67+
assert_eq!(&session.get::<String>(&key).unwrap(), &value);
68+
69+
Ok(())
6570
}
6671

67-
#[test]
68-
fn test_with_expire() -> async_session::Result {
69-
async_std::task::block_on(async {
70-
let store =
71-
MongodbSessionStore::new(&CONNECTION_STRING, "db_name", "collection").await?;
72-
73-
store.initialize().await?;
74-
75-
let mut rng = rand::thread_rng();
76-
let n2: u16 = rng.gen();
77-
let key = format!("key-{}", n2);
78-
let value = format!("value-{}", n2);
79-
let mut session = Session::new();
80-
session.expire_in(std::time::Duration::from_secs(5));
81-
session.insert(&key, &value)?;
82-
83-
let cookie_value = store.store_session(session).await?.unwrap();
84-
let session = store.load_session(cookie_value).await?.unwrap();
85-
assert_eq!(&session.get::<String>(&key).unwrap(), &value);
86-
87-
Ok(())
88-
})
72+
async fn with_expire() -> async_session::Result {
73+
let store = MongodbSessionStore::new(&CONNECTION_STRING, "db_name", "collection").await?;
74+
75+
store.initialize().await?;
76+
77+
let mut rng = rand::thread_rng();
78+
let n2: u16 = rng.gen();
79+
let key = format!("key-{}", n2);
80+
let value = format!("value-{}", n2);
81+
let mut session = Session::new();
82+
session.expire_in(std::time::Duration::from_secs(5));
83+
session.insert(&key, &value)?;
84+
85+
let cookie_value = store.store_session(session).await?.unwrap();
86+
let session = store.load_session(cookie_value).await?.unwrap();
87+
assert_eq!(&session.get::<String>(&key).unwrap(), &value);
88+
89+
Ok(())
8990
}
9091

91-
#[test]
92-
fn test_check_expired() -> async_session::Result {
92+
async fn check_expired() -> async_session::Result {
9393
use async_std::task;
9494
use std::time::Duration;
95-
async_std::task::block_on(async {
96-
let store =
97-
MongodbSessionStore::new(&CONNECTION_STRING, "db_name", "collection").await?;
95+
let store = MongodbSessionStore::new(&CONNECTION_STRING, "db_name", "collection").await?;
96+
97+
store.initialize().await?;
9898

99-
store.initialize().await?;
99+
let mut rng = rand::thread_rng();
100+
let n2: u16 = rng.gen();
101+
let key = format!("key-{}", n2);
102+
let value = format!("value-{}", n2);
103+
let mut session = Session::new();
104+
session.expire_in(Duration::from_secs(1));
105+
session.insert(&key, &value)?;
100106

101-
let mut rng = rand::thread_rng();
102-
let n2: u16 = rng.gen();
103-
let key = format!("key-{}", n2);
104-
let value = format!("value-{}", n2);
105-
let mut session = Session::new();
106-
session.expire_in(Duration::from_secs(1));
107-
session.insert(&key, &value)?;
107+
let cookie_value = store.store_session(session).await?.unwrap();
108108

109-
let cookie_value = store.store_session(session).await?.unwrap();
109+
task::sleep(Duration::from_secs(1)).await;
110+
let session_to_recover = store.load_session(cookie_value).await?;
110111

111-
task::sleep(Duration::from_secs(1)).await;
112-
let session_to_recover = store.load_session(cookie_value).await?;
112+
assert!(&session_to_recover.is_none());
113113

114-
assert!(&session_to_recover.is_none());
114+
Ok(())
115+
}
115116

116-
Ok(())
117-
})
117+
#[test]
118+
fn test_from_client() -> async_session::Result {
119+
run_test(from_client())
120+
}
121+
122+
#[test]
123+
fn test_new() -> async_session::Result {
124+
run_test(new())
125+
}
126+
127+
#[test]
128+
fn test_with_expire() -> async_session::Result {
129+
run_test(with_expire())
130+
}
131+
132+
#[test]
133+
fn test_check_expired() -> async_session::Result {
134+
run_test(check_expired())
118135
}
119136
}

0 commit comments

Comments
 (0)