Skip to content

Commit

Permalink
Make the FakeClock Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
jgosmann committed May 10, 2021
1 parent fd259be commit e127106
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.2.0] - 2021-05-10

### Changed

* The `bsec::clock::test_support::FakeClock` is now `Sync`.


## [0.1.2] - 2021-05-04

Try to get documentation to build on docs.rs.
Expand All @@ -21,7 +28,8 @@ Try to get documentation to build on docs.rs.
Initial release.


[Unreleased]: https://github.com/jgosmann/bsec/compare/v0.1.2...HEAD
[Unreleased]: https://github.com/jgosmann/bsec/compare/v0.2.0...HEAD
[0.2.0]: https://github.com/jgosmann/bsec/compare/v0.1.2...v0.2.0
[0.1.2]: https://github.com/jgosmann/bsec/compare/v0.1.1...v0.1.2
[0.1.1]: https://github.com/jgosmann/bsec/compare/v0.1.0...v0.1.1
[0.1.0]: https://github.com/jgosmann/bsec/releases/tag/v0.1.0
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bsec"
version = "0.1.2"
version = "0.2.0"
authors = ["Jan Gosmann <[email protected]>"]
edition = "2018"
license = "MIT OR Apache-2.0"
Expand Down
13 changes: 6 additions & 7 deletions src/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub mod tests {
/// This module is only available if the **test-support** feature is enabled.
#[cfg(any(test, feature = "test-support"))]
pub mod test_support {
use std::{cell::RefCell, time::Duration};
use std::{sync::atomic::{AtomicI64, Ordering}, time::Duration};

use super::*;

Expand All @@ -93,15 +93,15 @@ pub mod test_support {
/// ```
/// # use bsec::clock::{Clock, test_support::FakeClock};
/// let clock = FakeClock::new();
/// assert_eq!(clock.timestamp_ns(), 0);
/// assert_eq!(clock.timestamp_ns(), 1);
/// assert_eq!(clock.timestamp_ns(), 2);
/// assert_eq!(clock.timestamp_ns(), 3);
/// clock.advance_by(std::time::Duration::from_nanos(5));
/// assert_eq!(clock.timestamp_ns(), 9);
/// assert_eq!(clock.timestamp_ns(), 8);
/// ```
#[derive(Default)]
pub struct FakeClock {
timestamp_ns: RefCell<i64>,
timestamp_ns: AtomicI64,
}

impl FakeClock {
Expand All @@ -114,15 +114,14 @@ pub mod test_support {
impl Clock for FakeClock {
/// Returns the current timestamp and advances it by one.
fn timestamp_ns(&self) -> i64 {
*self.timestamp_ns.borrow_mut() += 1;
*self.timestamp_ns.borrow()
self.timestamp_ns.fetch_add(1, Ordering::AcqRel)
}
}

impl FakeClock {
/// Advance the clock's internal time by `duration`.
pub fn advance_by(&self, duration: Duration) {
*self.timestamp_ns.borrow_mut() += duration.as_nanos() as i64;
self.timestamp_ns.fetch_add(duration.as_nanos() as i64, Ordering::AcqRel);
}
}
}

0 comments on commit e127106

Please sign in to comment.