-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
lib.rs
134 lines (120 loc) · 3.95 KB
/
lib.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
use std::{collections::HashMap, sync::Arc};
use async_trait::async_trait;
use time::OffsetDateTime;
use tokio::sync::Mutex;
use tower_sessions_core::{
session::{Id, Record},
session_store, SessionStore,
};
/// A session store that lives only in memory.
///
/// This is useful for testing but not recommended for real applications.
///
/// # Examples
///
/// ```rust
/// use tower_sessions::MemoryStore;
/// MemoryStore::default();
/// ```
#[derive(Clone, Debug, Default)]
pub struct MemoryStore(Arc<Mutex<HashMap<Id, Record>>>);
#[async_trait]
impl SessionStore for MemoryStore {
async fn create(&self, record: &mut Record) -> session_store::Result<()> {
let mut store_guard = self.0.lock().await;
while store_guard.contains_key(&record.id) {
// Session ID collision mitigation.
record.id = Id::default();
}
store_guard.insert(record.id, record.clone());
Ok(())
}
async fn save(&self, record: &Record) -> session_store::Result<()> {
self.0.lock().await.insert(record.id, record.clone());
Ok(())
}
async fn load(&self, session_id: &Id) -> session_store::Result<Option<Record>> {
Ok(self
.0
.lock()
.await
.get(session_id)
.filter(|Record { expiry_date, .. }| is_active(*expiry_date))
.cloned())
}
async fn delete(&self, session_id: &Id) -> session_store::Result<()> {
self.0.lock().await.remove(session_id);
Ok(())
}
}
fn is_active(expiry_date: OffsetDateTime) -> bool {
expiry_date > OffsetDateTime::now_utc()
}
#[cfg(test)]
mod tests {
use time::Duration;
use super::*;
#[tokio::test]
async fn test_create() {
let store = MemoryStore::default();
let mut record = Record {
id: Default::default(),
data: Default::default(),
expiry_date: OffsetDateTime::now_utc() + Duration::minutes(30),
};
assert!(store.create(&mut record).await.is_ok());
}
#[tokio::test]
async fn test_save() {
let store = MemoryStore::default();
let record = Record {
id: Default::default(),
data: Default::default(),
expiry_date: OffsetDateTime::now_utc() + Duration::minutes(30),
};
assert!(store.save(&record).await.is_ok());
}
#[tokio::test]
async fn test_load() {
let store = MemoryStore::default();
let mut record = Record {
id: Default::default(),
data: Default::default(),
expiry_date: OffsetDateTime::now_utc() + Duration::minutes(30),
};
store.create(&mut record).await.unwrap();
let loaded_record = store.load(&record.id).await.unwrap();
assert_eq!(Some(record), loaded_record);
}
#[tokio::test]
async fn test_delete() {
let store = MemoryStore::default();
let mut record = Record {
id: Default::default(),
data: Default::default(),
expiry_date: OffsetDateTime::now_utc() + Duration::minutes(30),
};
store.create(&mut record).await.unwrap();
assert!(store.delete(&record.id).await.is_ok());
assert_eq!(None, store.load(&record.id).await.unwrap());
}
#[tokio::test]
async fn test_create_id_collision() {
let store = MemoryStore::default();
let expiry_date = OffsetDateTime::now_utc() + Duration::minutes(30);
let mut record1 = Record {
id: Default::default(),
data: Default::default(),
expiry_date,
};
let mut record2 = Record {
id: Default::default(),
data: Default::default(),
expiry_date,
};
store.create(&mut record1).await.unwrap();
record2.id = record1.id; // Set the same ID for record2
store.create(&mut record2).await.unwrap();
assert_ne!(record1.id, record2.id); // IDs should be different
}
}