Giter VIP home page Giter VIP logo

Comments (9)

la10736 avatar la10736 commented on June 9, 2024

Yeah... you are right. Thanks to report it.

I don't know when I can publish the new version.... sorry

from rstest.

Joining7943 avatar Joining7943 commented on June 9, 2024

Sure. I think you should also consider a downcast for String in addition to &str. I didn't noticed it at first, but the assert! macros and panic("{}", "something") produce a String instead of a &str. Could you also remove the panic message of the unnamed thread, please?

from rstest.

la10736 avatar la10736 commented on June 9, 2024

Ok.

from rstest.

Joining7943 avatar Joining7943 commented on June 9, 2024

Great! Thanks.

from rstest.

la10736 avatar la10736 commented on June 9, 2024

Ok, I fixed the String casting but I don't found a clear and clean way to remove the unnamed thread: I should run another thread to implement timeout (timeout should be in the current thread) and this message will contain the original stack trace and correct error coordinates.

The only way that I found to catch the stack trace is to use something like described in https://stackoverflow.com/questions/69593235/how-to-get-panic-information-i-e-stack-trace-with-catch-unwind but it seams a little bit overkilled and it will not prevent the panic output.

If you are ok I'm incline to close this ticket now.... If you have some ideas of how to move the panic from other thread to test thread I would really happy to hear you.

from rstest.

Joining7943 avatar Joining7943 commented on June 9, 2024

Great thanks for your work.

Oh I see. You want to preserve the backtrace, what's pretty cool. After a lot of searching and reading through the stdlib, I found a nice and simple solution.

pub fn execute_with_timeout_sync<T: 'static + Send, F: FnOnce() -> T + Send + 'static>(
    code: F,
    timeout: Duration,
) -> T {
    let (sender, receiver) = mpsc::channel();

    let thread = if let Some(name) = thread::current().name() {
        std::thread::Builder::new().name(name.to_string())
    } else {
        std::thread::Builder::new()
    };
    let handle = thread.spawn(move || sender.send(code())).unwrap();

    match receiver.recv_timeout(timeout) {
        Ok(result) => {
            // Unwraps are safe because we got a result from the thread, which is not a `SendError`,
            // and there was no panic within the thread which caused a disconnect.
            handle.join().unwrap().unwrap();
            result
        }
        Err(RecvTimeoutError::Timeout) => {
            panic!("Timeout {:?} expired", timeout)
        }
        Err(RecvTimeoutError::Disconnected) => match handle.join() {
            Ok(_) => unreachable!(),
            Err(any) => {
                panic::resume_unwind(any);
            }
        },
    }
}

Sorry, I've written this on the base of an older commit on the main branch, but I hope you get the idea.

from rstest.

la10736 avatar la10736 commented on June 9, 2024

from rstest.

Joining7943 avatar Joining7943 commented on June 9, 2024

Sure :)

from rstest.

la10736 avatar la10736 commented on June 9, 2024

THX again

from rstest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.