-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: fix UDP conn ID generation test by mocking current time
The current implementation of `get_connection_id` seems to be OK. I've added some tests. I needed to mock the current time becuase it's used in the ID generation. For now, I only neded to mock the current time (now). Not the clock service. I have also changed the signature of the `handle_connect` method. Now it panics if we cannot get the system time. In the previous behavior it was returning always a hardcoded connection ID: ConnectionId(0x7FFFFFFFFFFFFFFF)
- Loading branch information
1 parent
d8b13e5
commit 6c2f312
Showing
4 changed files
with
96 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use std::time::{SystemTime}; | ||
|
||
pub trait Clock { | ||
fn now_as_timestamp(&self) -> u64; | ||
} | ||
|
||
/// A [`Clock`] which uses the operating system to determine the time. | ||
struct SystemClock; | ||
|
||
impl Clock for SystemClock { | ||
fn now_as_timestamp(&self) -> u64 { | ||
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() | ||
} | ||
} | ||
|
||
/// It returns the current timestamp using the system clock. | ||
pub fn current_timestamp_from_system_clock() -> u64 { | ||
let system_clock = SystemClock; | ||
system_clock.now_as_timestamp() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod common; | ||
pub mod utils; | ||
pub mod clock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters