Skip to content

Thread safety? #10

@Gerschtli

Description

@Gerschtli

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions