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
docs: update docs with synchronization example #337
Conversation
This is probably a good addition, once you fix rustdoc. Even better though would be dedicated synchronization examples, perhaps in the examples directory. |
48d3914
to
aaf4cc8
Compare
@asomers, i added an example to the examples folder. let me know what you think. nightly failed on some stuff in personally i would like some sort of automatic locking. it's noisy to have to have to create a lock and then acquire it in every test. it's also vulnerable to new people joining the codebase. if they don't copy an existing test mod, read the readme or find the line in the docs that talks about synchronization they might end up with some "works on my machine" problems my opinion would probably change if the tests deadlocked a lot. but i wonder if it would be possible to create an attribute or macro that was like |
fn get_lock(m: &'static Mutex<()>) -> MutexGuard<'static, ()> { | ||
match m.lock() { | ||
Ok(guard) => guard, | ||
Err(poisoned) => poisoned.into_inner(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice touch.
Looks like nightly clippy got pickier. I'll pacify it in a separate PR. |
Ok, if you rebase on master now, it should fix your compile problems. |
e0696b0
to
8298c23
Compare
ok, @asomers should be good now |
does the synchronization need to happen between all different test mods or are the different test modules isolated? i have two files with test modules |
it seems like the synchronization needs to happen between all modules that are using the same mock. i'll update the example tomorrow to better illustrate that. i just did some refactoring that exposed that potential failure mode in our own codebase 😅 |
i'm also unsure if my get_lock function is working as intended, i still seem to get poison errors sometimes from the automock attribute. unsure if the backtrace is just confused or if there is another mutex in being used in the automock that is getting poisoned as well |
It depends on how the two modules are linked. If they're in the same process, then you need synchronization between them. If they are separate processes, then you don't. By default, Cargo will create a separate program for every file in the tests directory, and all tests within that program get executed as part of the same process. |
Oh, and for unit tests Cargo by default creates one program for a crate's library's unit tests, and one program for each of the crate's binary's unit tests. |
mockall/src/lib.rs
Outdated
@@ -836,7 +836,8 @@ | |||
//! | |||
//! Mockall can also mock static methods. But be careful! The expectations are | |||
//! global. If you want to use a static method in multiple tests, you must | |||
//! provide your own synchronization. For ordinary methods, expectations are | |||
//! provide your own synchronization. See the [`synchronization example`](examples/synchronization.rs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This link will be dead, because rustdoc doesn't include files in the examples/ directory. Instead, it's probably best to link to the file on Github.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i just linked to where the file would be once this is merged in. should i link to a specific commit hash or is that fine?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's best not to provide a specific hash. We might update the example later, and it would be easy to forget to update the link.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, updated to use my previous commit as the reference
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you've pinned a specific commit. Don't you think it's better to point to the latest version?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, oops. i misread your comment. i thought you wanted me to include a specific commit hash. i can revert that to point to the latest version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@asomers does this seem good to go now?
6e01e72
to
0999b53
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, it looks good now.
just adds a link to the synchronization example in the main lib.rs doc