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

Thread safety? #10

Open
Gerschtli opened this issue Dec 24, 2018 · 1 comment
Open

Thread safety? #10

Gerschtli opened this issue Dec 24, 2018 · 1 comment
Labels
enhancement New feature or request

Comments

@Gerschtli
Copy link

Hello,
first of all, I am really amazed by this crate! But I am not able to understand, what is really going on under the hood to achieve this functionality. Therefore my first idea was to use your crate for mocking purposes in tests. Sadly, your implementation is not thread safe, reproducible example below:

#[cfg(test)]
extern crate guerrilla;

fn function_int() -> i32 { 1 }

fn function_str() -> &'static str { "original" }

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_int_default() {
        assert_eq!(function_int(), 1);
    }

    #[test]
    fn test_int_mock_1() {
        guerrilla::patch0(function_int, || 5);
        assert_eq!(function_int(), 5);
    }

    #[test]
    fn test_int_mock_2() {
        guerrilla::patch0(function_int, || 2);
        assert_eq!(function_int(), 2);
    }

    #[test]
    fn test_str_default() {
        assert_eq!(function_str(), "original");
    }

    #[test]
    fn test_str_mock_1() {
        guerrilla::patch0(function_str, || "mock_1");
        assert_eq!(function_str(), "mock_1");
    }

    #[test]
    fn test_str_mock_2() {
        guerrilla::patch0(function_str, || "mock_2");
        assert_eq!(function_str(), "mock_2");
    }
}

(Execute this a couple times to see random failures and random successful runs. Runs perfectly fine with cargo test -- --test-threads 1.)

Is it possible, to achieve thread safety? And would it be something you would like to implement? I would really appreciate it :)

Thank you!

@mehcode
Copy link
Owner

mehcode commented Jan 21, 2019

Its doable. Just a great deal of work so not sure when I will get to it. I do want to do this.

@mehcode mehcode added the enhancement New feature or request label Jan 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants