[solved] Running actix-web *client* in tests

I’m using actix_web::client to make (not serve) an HTTP request. I’d like to use it to make requests in #[test] and read the result. For reasons, it’s also wrapped in impl Future<Item=ClientResponse>.

What’s the right way to wait for it?

  • future.wait() seems to block forever (deadlock?)
  • tokio::runtime::Runtime::block_on() requires Send, but the ClientResponse is not Send.
    use std::thread;
    use std::sync::mpsc;
    use self::tokio::runtime::current_thread;

    struct TestContext {
        rt: current_thread::Runtime,
    }

    impl TestContext {
        pub fn new() -> Self {
            let (tx, rx) = mpsc::channel();

            // run server in separate thread
            thread::spawn(move || {

                let sys = System::new("test");

                tx.send(System::current()).unwrap();

                sys.run();
            });

            let system = rx.recv().unwrap();
            System::set_current(system);

            Self {
                rt: current_thread::Runtime::new().unwrap()
            }
        }

        pub fn block_on<F: Future>(&mut self, future: F) -> Result<F::Item, F::Error> {
            self.rt.block_on(future)
        }
    }

    impl Drop for TestContext {
        fn drop(&mut self) {
            System::current().stop();
        }
    }
1 Like