Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix docs #18

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
# mock_instant

**_NOTE_** As of version 0.5. MockClock/Instant/SystemTime have been moved to specific modules
**_NOTE_** The modules, `global` and `thread_local` change the behavior across threads. If `global` is used, the clock keeps its state across threads, otherwise if `thread_local` is used, a new _source_ is made for each thread

## Migration from older versions

Simply import the types from the correct namespace:

`mock_instant::global`

and

`mock_instant::thread_local`

---
**_NOTE_** The modules, `global` and `thread_local` change the behavior across threads. If `global` is used, the clock keeps its state across threads, otherwise if `thread_local` is used, a new _source_ is made for each thread

To ensure unsurprising behavior, **reset** the clock _before_ each test (if that behavior is applicable.)

Expand Down Expand Up @@ -55,7 +44,7 @@ MockClock::advance(Duration::from_secs(2));
assert_eq!(now.elapsed(), Duration::from_secs(17));
```

### API:
## API:

```rust,compile_fail
// Overrides the current time to this `Duration`
Expand All @@ -82,7 +71,7 @@ Instant::now().is_thread_local() -> bool
SystemTime::now().is_thread_local() -> bool
```

### Usage:
## Usage:

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

Expand All @@ -94,16 +83,21 @@ You can also get the current frozen time with `MockClock::time`

`SystemTime` is also mockable with a similar API.

### Thread-safety:
## Thread-safety:

Two modes are provided via modules. The APIs are identical but the `MockClock` source has different behavior in different threads.

- global
- `mock_instant::global`

when using mock_instant::global `MockClock` `Instant` and `SystemTime` will share its state across threads
- `MockClock` will have a new state per thread
- `Instant`will have a new state per thread
- `SystemTime` will have a new state per thread

- thread_local
- `mock_instant::thread_local`
- `MockClock` will have a new state per thread
- `Instant`will have a new state per thread
- `SystemTime` will have a new state per thread

when using mock_instant::thread_local `MockClock` `Instant` and `SystemTime` will have a new state per thread
---

License: 0BSD
15 changes: 9 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*! # mock_instant
**_NOTE_** As of version 0.5. MockClock/Instant/SystemTime have been moved to specific modules

**_NOTE_** The modules, `global` and `thread_local` change the behavior across threads. If `global` is used, the clock keeps its state across threads, otherwise if `thread_local` is used, a new *source* is made for each thread

To ensure unsurprising behavior, **reset** the clock _before_ each test (if that behavior is applicable.)
Expand Down Expand Up @@ -85,13 +86,15 @@ You can also get the current frozen time with `MockClock::time`

Two modes are provided via modules. The APIs are identical but the `MockClock` source has different behavior in different threads.

- global

when using mock_instant::global `MockClock` `Instant` and `SystemTime` will share its state across threads

- thread_local
- `mock_instant::global`
- `MockClock` will have a new state per thread
- `Instant`will have a new state per thread
- `SystemTime` will have a new state per thread

when using mock_instant::thread_local `MockClock` `Instant` and `SystemTime` will have a new state per thread
- `mock_instant::thread_local`
- `MockClock` will have a new state per thread
- `Instant`will have a new state per thread
- `SystemTime` will have a new state per thread

*/

Expand Down
2 changes: 1 addition & 1 deletion src/thread_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ crate::macros::define_instant! {
crate::macros::define_system_time! {
MockClock::system_time;
true;
/// This uses a global mutex for its time source
/// This uses a thread-local cell for its time source
}

crate::macros::define_instant_tests!();
Expand Down
Loading