platformTimerTest.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifdef TORQUE_TESTS_ENABLED
  23. #include "testing/unitTesting.h"
  24. #include "platform/platformTimer.h"
  25. #include "core/util/journal/process.h"
  26. #include "math/mMath.h"
  27. TEST(Platform, AdvanceTime)
  28. {
  29. U32 time = Platform::getVirtualMilliseconds();
  30. Platform::advanceTime(10);
  31. U32 newTime = Platform::getVirtualMilliseconds();
  32. EXPECT_EQ(10, newTime - time)
  33. << "We advanced 10ms but didn't get a 10ms delta!";
  34. }
  35. TEST(Platform, Sleep)
  36. {
  37. U32 start = Platform::getRealMilliseconds();
  38. Platform::sleep(500);
  39. U32 end = Platform::getRealMilliseconds();
  40. EXPECT_GE(end - start, 500-10) // account for clock resolution
  41. << "We didn't sleep at least as long as we requested!";
  42. };
  43. struct handle
  44. {
  45. S32 mElapsedTime;
  46. S32 mNumberCalls;
  47. handle() : mElapsedTime(0), mNumberCalls(0) {}
  48. void timeEvent(S32 timeDelta)
  49. {
  50. mElapsedTime += timeDelta;
  51. mNumberCalls++;
  52. if(mElapsedTime >= 1000)
  53. Process::requestShutdown();
  54. }
  55. };
  56. TEST(TimeManager, BasicAPI)
  57. {
  58. handle handler;
  59. // Initialize the time manager...
  60. TimeManager time;
  61. time.timeEvent.notify(&handler, &handle::timeEvent);
  62. // Event loop till at least one second has passed.
  63. const U32 start = Platform::getRealMilliseconds();
  64. while(Process::processEvents())
  65. {
  66. // If we go too long, kill it off...
  67. if(Platform::getRealMilliseconds() - start > 30*1000)
  68. {
  69. EXPECT_TRUE(false)
  70. << "Terminated process loop due to watchdog, not due to time manager event, after 30 seconds.";
  71. Process::requestShutdown();
  72. }
  73. }
  74. const U32 end = Platform::getRealMilliseconds();
  75. // Now, confirm we have approximately similar elapsed times.
  76. S32 elapsedRealTime = end - start;
  77. EXPECT_LT(mAbs(elapsedRealTime - handler.mElapsedTime), 50)
  78. << "Failed to elapse time to within the desired tolerance.";
  79. EXPECT_GT(handler.mNumberCalls, 0)
  80. << "Somehow got no event callbacks from TimeManager?";
  81. };
  82. #endif