Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
museun committed Apr 7, 2024
2 parents e9e24b7 + 54dedaa commit 26a15ed
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 104 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: CI
on:
push:
branches: [main]
pull_request:

jobs:
ci:
name: CI
needs: [test, clippy]
runs-on: ubuntu-latest
steps:
- name: Done
run: exit 0
test:
name: Tests
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
rust: [stable]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Install rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
components: rustfmt
- uses: taiki-e/install-action@cargo-deny
- uses: Swatinem/rust-cache@v2
- run: cargo test
- run: cargo test --all-targets --all-features --workspace
- run: cargo doc --no-deps --all-features
clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install rust
uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- uses: Swatinem/rust-cache@v2
- run: cargo clippy
- run: cargo clippy --all-targets --all-features --workspace
9 changes: 1 addition & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mock_instant"
version = "0.3.2"
version = "0.3.0"
authors = [ "museun <museun@outlook.com>" ]
edition = "2018"
license = "0BSD"
Expand All @@ -9,10 +9,3 @@ keywords = [ "mock", "test", "time", "instant" ]
description = "a simple way to mock an std::time::Instant"
documentation = "https://docs.rs/mock_instant"
repository = "https://github.com/museun/mock_instant"

[features]
default = [ ]
sync = [ "once_cell" ]

[dependencies]
once_cell = { version = "1.17", optional = true }
56 changes: 37 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# mock_instant

## mock_instant
**_NOTE_** As of version 0.3, the thread-local clock has been removed. The clock will always be thread-safe.

This crate allows you to test Instant/Duration code, deterministically **_per thread_**.
To ensure unsurprising behavior, **reset** the clock _before_ each test (if that behavior is applicable.)

If cross-thread determinism is required, enable the `sync` feature:
---

```toml
mock_instant = { version = "0.3", features = ["sync"] }
```
This crate allows you to test `Instant`/`Duration`/`SystemTime` code, deterministically.

_This uses a static mutex to have a thread-aware clock._

It provides a replacement `std::time::Instant` and `std::time::SystemTime` that uses a deterministic thread-local 'clock'
It provides a replacement `std::time::Instant` that uses a deterministic 'clock'

You can swap out the `std::time::Instant` with this one by doing something similar to:

Expand All @@ -35,6 +35,8 @@ use std::time::{SystemTime, SystemTimeError};
## Example

```rust
# use mock_instant::MockClock;
# use mock_instant::Instant;
use std::time::Duration;

let now = Instant::now();
Expand All @@ -45,24 +47,40 @@ MockClock::advance(Duration::from_secs(2));
assert_eq!(now.elapsed(), Duration::from_secs(17));
```

# Mocking a SystemTime
## API:

```
# use mock_instant::{MockClock, SystemTime};
use std::time::Duration;
```rust
// Overrides the current time to this `Duration`
MockClick::set_time(time: Duration)

let now = SystemTime::now();
MockClock::advance_system_time(Duration::from_secs(15));
MockClock::advance_system_time(Duration::from_secs(2));
// Advance the current time by this `Duration`
MockClick::advance(time: Duration)

// its been '17' seconds
assert_eq!(now.elapsed().unwrap(), Duration::from_secs(17));
// Get the current time
MockClick::time() -> Duration

// Overrides the current `SystemTime` to this duration
MockClick::set_system_time(time: Duration)

// Advance the current `SystemTime` by this duration
MockClick::sdvance_system_time(time: Duration)

// Get the current `SystemTime`
MockClick::system_time() -> Duration
```

# Caveats
## Usage:

**_NOTE_** The clock starts at `Duration::ZERO`

In your tests, you can use `MockClock::set_time(Duration::ZERO)` to reset the clock back to 0. Or, you can set it to some sentinel time value.

Then, before you check your time-based logic, you can advance the clock by some `Duration` (it'll freeze the time to that duration)

You can also get the current frozen time with `MockClock::time`

If the `sync` feature is enabled then all tests using this crate will use a global singleton clock.
`SystemTime` is also mockable with a similar API.

see <https://github.com/museun/mock_instant/issues/6>
---

License: 0BSD
Loading

0 comments on commit 26a15ed

Please sign in to comment.