1

I am trying to sleep in Google test. According to this post, I can use the following:

#include <chrono>
#include <thread>
TEST_F(TestSuite, TestOne) {
    // f1();
    std::this_thread::sleep_for(std::chrono::seconds(2));
    // f2();
}

I put this command in the Test, but it does not change anything. Code executes quickly as it omitted the sleep. I am using g++ (Ubuntu 9.3.0-10ubuntu2) 9.3.0.
What might I be doing wrong?

Chris
  • 839
  • 1
  • 12
  • 30
  • Is your code/test really executed? Try add some log, breakpoint or failing test code and see is it working. Or maybe your test exits before `sleep_for` (at `f1` for example) – Ihor Drachuk Jul 30 '20 at 11:02
  • In code, I put sleep_for inside for loop which couts array, but it is printed immediately. – Chris Jul 30 '20 at 11:05
  • Sorry for stupid assumption, but... have your code rebuilt? Did you also try change something else except sleep_for? Maybe change some text message and see did it really changed in output? – Ihor Drachuk Jul 30 '20 at 11:09
  • Your example (although not complete) works correctly. You have the problem somewhere else. Show: Fixture definition, f1, f2 definitions, command used to run the tests, console output. – pptaszni Jul 30 '20 at 11:20
  • I removed everything from test leaving it like this:cout; sleep; cout; It executed immediately. I am running it in WSL, maybe that's the issue, I will check it – Chris Jul 30 '20 at 11:35
  • I tested exactly the same code on Linux and it works properly. The above code does not sleep in the Windows Subsystem for Linux. I don't know what causes this error, maybe something related to thread library. – Chris Jul 30 '20 at 12:12
  • Does mostly empty program in "Windows Subsystem for Linux" sleep if it call just from `main`? (Not test, just new app) Maybe problem in running programs under this subsystem – Ihor Drachuk Jul 30 '20 at 13:24
  • No. Program like this std::cout << "Hello " << std::flush; std::this_thread::sleep_for(std::chrono::seconds(2)); std::cout << "World\n"; Prints Hello World immediately. – Chris Jul 30 '20 at 13:35
  • 1
    This seems to be a WSL bug. Check [this](https://askubuntu.com/questions/1230252/sleep-doesnt-work-on-ubuntu-20-04-wsl) and [this](https://github.com/microsoft/WSL/issues/4898). – for_stack Aug 06 '20 at 09:41

2 Answers2

0

Does your test really execute? I'd recommend to put temporary std::cout << "STARTING TEST" << std::endl; in the beginning of the test and check if you can see those words on the output.

ivan.ukr
  • 2,853
  • 1
  • 23
  • 41
0

Well... As I understand, the problem is in WSL, see this link: Unpredictable behaviour of std::sleep_for on Windows 10.

Try to build your sources with native compilers for Windows: MSVC, MinGW or Cygwin. If your code uses Linux-specific things, probably Cygwin will be better (easier to build code or easier to port it). Or you can try using some Linux system natively. Sorry, I don't know what to advice else...

Ihor Drachuk
  • 1,265
  • 7
  • 17