Skip to content

Commit

Permalink
tests for std check timer
Browse files Browse the repository at this point in the history
  • Loading branch information
robamu committed Aug 20, 2024
1 parent 7502f05 commit ceb7813
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
50 changes: 40 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,16 @@ pub mod std_mod {
start_time: std::time::Instant::now(),
}
}

pub fn expiry_time_seconds(&self) -> u64 {
self.expiry_time_seconds
}
}

impl CountdownProvider for StdCheckTimer {
fn has_expired(&self) -> bool {
let elapsed_time = self.start_time.elapsed();
if elapsed_time.as_secs() > self.expiry_time_seconds {
if elapsed_time.as_nanos() > self.expiry_time_seconds as u128 * 1_000_000_000 {
return true;
}
false
Expand All @@ -140,14 +144,20 @@ pub mod std_mod {
pub check_limit_timeout_secs: u64,
}

impl Default for StdCheckTimerCreator {
fn default() -> Self {
impl StdCheckTimerCreator {
pub const fn new(check_limit_timeout_secs: u64) -> Self {
Self {
check_limit_timeout_secs: 5,
check_limit_timeout_secs,
}
}
}

impl Default for StdCheckTimerCreator {
fn default() -> Self {
Self::new(5)
}
}

impl CheckTimerProviderCreator for StdCheckTimerCreator {
type CheckTimer = StdCheckTimer;

Expand Down Expand Up @@ -690,14 +700,11 @@ pub(crate) mod tests {
},
util::{UnsignedByteField, UnsignedByteFieldU16, UnsignedEnum},
};
use user::{CfdpUser, OwnedMetadataRecvdParams, TransactionFinishedParams};

use crate::PacketTarget;
use crate::{PacketTarget, StdCheckTimer};

use super::{
user::{CfdpUser, OwnedMetadataRecvdParams, TransactionFinishedParams},
PacketInfo, PduSendProvider, RemoteEntityConfig, RemoteEntityConfigProvider,
StdRemoteEntityConfigProvider, TransactionId, UserFaultHookProvider,
};
use super::*;

pub const LOCAL_ID: UnsignedByteFieldU16 = UnsignedByteFieldU16::new(1);

Expand Down Expand Up @@ -1015,4 +1022,27 @@ pub(crate) mod tests {
FileDirectiveType::EofPdu
);
}

#[test]
fn test_std_check_timer() {
let mut std_check_timer = StdCheckTimer::new(1);
assert!(!std_check_timer.has_expired());
assert_eq!(std_check_timer.expiry_time_seconds(), 1);
std::thread::sleep(Duration::from_millis(800));
assert!(!std_check_timer.has_expired());
std::thread::sleep(Duration::from_millis(205));
assert!(std_check_timer.has_expired());
std_check_timer.reset();
assert!(!std_check_timer.has_expired());
}

#[test]
fn test_std_check_timer_creator() {
let std_check_timer_creator = StdCheckTimerCreator::new(1);
let check_timer =
std_check_timer_creator.create_check_timer_provider(TimerContext::NakActivity {
expiry_time_seconds: 1.0,
});
assert_eq!(check_timer.expiry_time_seconds(), 1);
}
}
6 changes: 3 additions & 3 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub enum SourceError {
#[error("error related to PDU creation")]
Pdu(#[from] PduError),
#[error("cfdp feature not implemented")]
NotImplemented
NotImplemented,
}

#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -205,12 +205,12 @@ impl<
super::State::Idle => {
// TODO: In acknowledged mode, add timer handling.
Ok(0)
},
}
super::State::Busy => self.fsm_busy(cfdp_user),
super::State::Suspended => {
// There is now way to suspend the handler currently anyway.
Ok(0)
},
}
}
}

Expand Down

0 comments on commit ceb7813

Please sign in to comment.