2

I have the following snippet:

fn main() {
   async_std::task::block_on(async {
      let handle_fast = async {
         for i in 1..=8 {
            println!("fast {}", i);
            async_std::task::sleep(std::time::Duration::from_millis(250)).await;
         }
         ()
      };

      let handle_slow = async {
         for i in 1..=4 {
            println!("slow {}", i);
            async_std::task::sleep(std::time::Duration::from_millis(500)).await;
         }
         ()
      };

      futures_util::join!(handle_fast, handle_slow)
   });
}

#[cfg(test)]
mod tests {
   #[test]
   fn test() {
      async_std::task::block_on(async {
         let handle_fast = async {
            for i in 1..=8 {
               println!("fast {}", i);
               async_std::task::sleep(std::time::Duration::from_millis(250)).await;
            }
            ()
         };

         let handle_slow = async {
            for i in 1..=4 {
               println!("slow {}", i);
               async_std::task::sleep(std::time::Duration::from_millis(500)).await;
            }
            ()
         };

         futures_util::join!(handle_fast, handle_slow)
      });
   }
}

When I run the main method it works as expected. But when running from the tests it doesn't work and just prints all at once at the end of the execution. How can I reproduce the behavior from main in the tests?

OneEyeQuestion
  • 742
  • 7
  • 22

1 Answers1

1
cargo test -- --nocapture

The tasks run fine. It's just that test runner "eats" all print output and hides it if the test passes.

Kornel
  • 97,764
  • 37
  • 219
  • 309
  • 1
    @Kornel thank you. I was running the tests from IntelliJ. Usually when I use println! in tests they always print because they are fast so I didn't notice that didn't print until the end of the test. – OneEyeQuestion Apr 15 '20 at 01:56