Added expire to the new session.
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use super::SenderID;
|
||||
use chrono::{DateTime, Utc};
|
||||
use crate::{
|
||||
action::{Action, Field, MsgAction},
|
||||
message::{Message, MessageAction, MessageID},
|
||||
@@ -487,23 +488,30 @@ mod route_storeage {
|
||||
#[derive(Clone, Debug)]
|
||||
struct SessionEntry {
|
||||
id: Field,
|
||||
expire_time: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl SessionEntry {
|
||||
fn id(&self) -> &Field {
|
||||
&self.id
|
||||
}
|
||||
|
||||
fn is_expired(&self) -> bool {
|
||||
Utc::now() > self.expire_time
|
||||
}
|
||||
}
|
||||
|
||||
impl SessionEntry {
|
||||
fn new(id: Field) -> Self {
|
||||
Self { id: id }
|
||||
Self { id: id, expire_time: Utc::now(), }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod session_entries {
|
||||
use super::*;
|
||||
use chrono::Utc;
|
||||
use std::time::Duration;
|
||||
|
||||
#[test]
|
||||
fn does_entry_return_id() {
|
||||
@@ -513,7 +521,14 @@ mod session_entries {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_determine_if_entry_expired() {}
|
||||
fn can_determine_if_entry_expired() {
|
||||
let mut entry = SessionEntry::new(Uuid::nil().into());
|
||||
let data = Utc::now();
|
||||
entry.expire_time = data + Duration::from_secs(1);
|
||||
assert!(!entry.is_expired(), "entry should not be expired");
|
||||
entry.expire_time = data - Duration::from_secs(1);
|
||||
assert!(entry.is_expired(), "entry should be expired");
|
||||
}
|
||||
}
|
||||
|
||||
struct Session {
|
||||
@@ -543,7 +558,7 @@ impl Session {
|
||||
while self.entries.contains_key(&new_id) {
|
||||
new_id = Uuid::new_v4().into();
|
||||
}
|
||||
let output = SessionEntry { id: new_id.clone() };
|
||||
let output = SessionEntry::new(new_id.clone());
|
||||
self.entries.insert(new_id, output.clone());
|
||||
output
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user