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

docs: update docs with synchronization example #337

Merged
merged 2 commits into from Feb 4, 2022

Conversation

TomPridham
Copy link
Contributor

just adds a link to the synchronization example in the main lib.rs doc

@asomers
Copy link
Owner

asomers commented Sep 20, 2021

This is probably a good addition, once you fix rustdoc. Even better though would be dedicated synchronization examples, perhaps in the examples directory.
Also, I've considered building synchronization into Mockall. Basically, creating a Context object would automatically acquire a lock. However, these would be vulnerable to lock-order reversals and other bugs, since the user would probably be unaware of them. What do you think of that idea?

@TomPridham TomPridham force-pushed the patch-1 branch 2 times, most recently from 48d3914 to aaf4cc8 Compare October 12, 2021 19:14
mockall/src/lib.rs Outdated Show resolved Hide resolved
@TomPridham
Copy link
Contributor Author

@asomers, i added an example to the examples folder. let me know what you think.

nightly failed on some stuff in mockall_derive that i didn't touch. not sure what you want to do about that

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 #[mockall_synchronization] that basically just did the work of creating the mutex and adding the lock acquisition to the start of every test in the mod with the attribute

mockall/examples/synchronization.rs Outdated Show resolved Hide resolved
mockall/src/lib.rs Outdated Show resolved Hide resolved
fn get_lock(m: &'static Mutex<()>) -> MutexGuard<'static, ()> {
match m.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice touch.

@asomers
Copy link
Owner

asomers commented Oct 12, 2021

Looks like nightly clippy got pickier. I'll pacify it in a separate PR.

@asomers
Copy link
Owner

asomers commented Oct 12, 2021

Ok, if you rebase on master now, it should fix your compile problems.

@TomPridham TomPridham force-pushed the patch-1 branch 2 times, most recently from e0696b0 to 8298c23 Compare October 14, 2021 21:01
@TomPridham
Copy link
Contributor Author

ok, @asomers should be good now

@TomPridham
Copy link
Contributor Author

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 a.rs and b.rs that both mock c.rs. does the synchronization need to make sure that only one test is running at a time between a AND b or is it enough to make sure that only one test at a time is running per module?

@TomPridham
Copy link
Contributor Author

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 😅

@TomPridham
Copy link
Contributor Author

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

@asomers
Copy link
Owner

asomers commented Oct 15, 2021

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 a.rs and b.rs that both mock c.rs. does the synchronization need to make sure that only one test is running at a time between a AND b or is it enough to make sure that only one test at a time is running per module?

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.

mockall/examples/synchronization.rs Outdated Show resolved Hide resolved
@asomers
Copy link
Owner

asomers commented Oct 15, 2021

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.

@@ -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)
Copy link
Owner

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.

Copy link
Contributor Author

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?

Copy link
Owner

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.

Copy link
Contributor Author

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

Copy link
Owner

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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?

@TomPridham TomPridham force-pushed the patch-1 branch 2 times, most recently from 6e01e72 to 0999b53 Compare January 26, 2022 00:08
Copy link
Owner

@asomers asomers left a 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.

@asomers asomers merged commit 0fb6367 into asomers:master Feb 4, 2022
@TomPridham TomPridham deleted the patch-1 branch February 15, 2022 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants