timeout_test.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #define _CRT_SECURE_NO_WARNINGS // work around 'fopen' deprecation on WIN32
  2. #if defined(_WIN32)
  3. # include <windows.h>
  4. #else
  5. # include <unistd.h>
  6. #endif
  7. #include <string>
  8. #include <stdio.h>
  9. void sleepFor(unsigned seconds)
  10. {
  11. #if defined(_WIN32)
  12. Sleep(seconds * 1000);
  13. #else
  14. sleep(seconds);
  15. #endif
  16. }
  17. int main(int argc, char** argv)
  18. {
  19. // Note: GoogleTest.cmake doesn't actually depend on Google Test as such;
  20. // it only requires that we produce output in the expected format when
  21. // invoked with --gtest_list_tests. Thus, we fake that here. This allows us
  22. // to test the module without actually needing Google Test.
  23. if (argc > 1 && std::string(argv[1]) == "--gtest_list_tests" &&
  24. std::string(argv[2]).find("--gtest_output=json:") != std::string::npos) {
  25. printf("timeout.\n case\n");
  26. fflush(stdout);
  27. std::string output_param(argv[2]);
  28. std::string::size_type split = output_param.find(":");
  29. std::string filepath = output_param.substr(split + 1);
  30. // The full file path is passed
  31. FILE* ofile = fopen(filepath.c_str(), "wb");
  32. if (!ofile) {
  33. fprintf(stderr, "Failed to create file: %s\n", filepath.c_str());
  34. return 1;
  35. }
  36. std::string json_output = "{\n"
  37. " \"tests\": 1,\n"
  38. " \"name\": \"AllTests\",\n"
  39. " \"testsuites\": [\n"
  40. " {\n"
  41. " \"name\": \"timeout\",\n"
  42. " \"tests\": 1,\n"
  43. " \"testsuite\": [\n"
  44. " { \"name\": \"case\", "
  45. "\"file\": \"file.cpp\", \"line\": 42 }\n"
  46. " ]\n"
  47. " }\n"
  48. " ]\n"
  49. " }";
  50. fprintf(ofile, "%s", json_output.c_str());
  51. fclose(ofile);
  52. #ifdef discoverySleepSec
  53. sleepFor(discoverySleepSec);
  54. #endif
  55. return 0;
  56. }
  57. #ifdef sleepSec
  58. sleepFor(sleepSec);
  59. #endif
  60. return 0;
  61. }