2
0

main.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #include <chrono>
  9. #include <iostream>
  10. #include <thread>
  11. #include "test.hpp"
  12. #include <rtc/rtc.hpp>
  13. using namespace std;
  14. using namespace chrono_literals;
  15. using chrono::duration_cast;
  16. using chrono::milliseconds;
  17. using chrono::seconds;
  18. using chrono::steady_clock;
  19. TestResult test_connectivity();
  20. TestResult test_connectivity_fail_on_wrong_fingerprint();
  21. TestResult test_pem();
  22. TestResult test_negotiated();
  23. TestResult test_reliability();
  24. TestResult test_turn_connectivity();
  25. TestResult test_track();
  26. TestResult test_capi_connectivity();
  27. TestResult test_capi_track();
  28. TestResult test_websocket();
  29. TestResult test_websocketserver();
  30. TestResult test_capi_websocketserver();
  31. size_t benchmark(chrono::milliseconds duration);
  32. void test_benchmark() {
  33. size_t goodput = benchmark(10s);
  34. if (goodput == 0)
  35. throw runtime_error("No data received");
  36. const size_t threshold = 1000; // 1 MB/s;
  37. if (goodput < threshold)
  38. throw runtime_error("Goodput is too low");
  39. }
  40. TestResult test_cleanup() {
  41. try {
  42. // Every created object must have been destroyed, otherwise the wait will block
  43. if (rtc::Cleanup().wait_for(10s) == future_status::timeout)
  44. return TestResult(false, "timeout");
  45. return TestResult(true);
  46. } catch (const exception &e) {
  47. return TestResult(false, e.what());
  48. }
  49. }
  50. TestResult test_capi_cleanup() {
  51. try {
  52. rtcCleanup();
  53. return TestResult(true);
  54. } catch (const exception &e) {
  55. return TestResult(false, e.what());
  56. }
  57. }
  58. static const vector<Test> tests = {
  59. // C++ API tests
  60. Test("WebRTC connectivity", test_connectivity),
  61. Test("WebRTC broken fingerprint", test_connectivity_fail_on_wrong_fingerprint),
  62. Test("pem", test_pem),
  63. // TODO: Temporarily disabled as the Open Relay TURN server is unreliable
  64. // new Test("WebRTC TURN connectivity", test_turn_connectivity),
  65. Test("WebRTC negotiated DataChannel", test_negotiated),
  66. Test("WebRTC reliability mode", test_reliability),
  67. #if RTC_ENABLE_MEDIA
  68. Test("WebRTC track", test_track),
  69. #endif
  70. #if RTC_ENABLE_WEBSOCKET
  71. // TODO: Temporarily disabled as the echo service is unreliable
  72. // new Test("WebSocket", test_websocket),
  73. Test("WebSocketServer", test_websocketserver),
  74. #endif
  75. Test("Cleanup", test_cleanup),
  76. // C API tests
  77. Test("WebRTC C API connectivity", test_capi_connectivity),
  78. #if RTC_ENABLE_MEDIA
  79. Test("WebRTC C API track", test_capi_track),
  80. #endif
  81. #if RTC_ENABLE_WEBSOCKET
  82. Test("WebSocketServer C API", test_capi_websocketserver),
  83. #endif
  84. Test("C API cleanup", test_capi_cleanup),
  85. };
  86. int main(int argc, char **argv) {
  87. int success_tests = 0;
  88. int failed_tests = 0;
  89. steady_clock::time_point startTime, endTime;
  90. startTime = steady_clock::now();
  91. for (auto test : tests) {
  92. auto res = test.run();
  93. if (res.success) {
  94. success_tests++;
  95. } else {
  96. failed_tests++;
  97. }
  98. }
  99. endTime = steady_clock::now();
  100. auto durationMs = duration_cast<milliseconds>(endTime - startTime);
  101. auto durationS = duration_cast<seconds>(endTime - startTime);
  102. cout << "Finished " << success_tests + failed_tests << " tests in " << durationS.count()
  103. << "s (" << durationMs.count() << " ms). Succeeded: " << success_tests
  104. << ". Failed: " << failed_tests << "." << endl;
  105. /*
  106. // Benchmark
  107. try {
  108. cout << endl << "*** Running WebRTC benchmark..." << endl;
  109. test_benchmark();
  110. cout << "*** Finished WebRTC benchmark" << endl;
  111. } catch (const exception &e) {
  112. cerr << "WebRTC benchmark failed: " << e.what() << endl;
  113. std::this_thread::sleep_for(2s);
  114. return -1;
  115. }
  116. */
  117. return 0;
  118. }