testTimeManager.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #include "platform/platform.h"
  23. #include "platform/platformTimer.h"
  24. #include "core/util/journal/journaledSignal.h"
  25. #include "core/util/journal/process.h"
  26. #include "math/mMath.h"
  27. #include "console/console.h"
  28. #include "unit/test.h"
  29. using namespace UnitTesting;
  30. CreateUnitTest(Check_advanceTime, "Platform/Time/advanceTime")
  31. {
  32. void run()
  33. {
  34. U32 time = Platform::getVirtualMilliseconds();
  35. Platform::advanceTime(10);
  36. U32 newTime = Platform::getVirtualMilliseconds();
  37. test(newTime - time == 10, "Platform::advanceTime is borked, we advanced 10ms but didn't get a 10ms delta!");
  38. }
  39. };
  40. CreateUnitTest(Check_platformSleep, "Platform/Time/Sleep")
  41. {
  42. const static S32 sleepTimeMs = 500;
  43. void run()
  44. {
  45. U32 start = Platform::getRealMilliseconds();
  46. Platform::sleep(sleepTimeMs);
  47. U32 end = Platform::getRealMilliseconds();
  48. test(end - start >= sleepTimeMs, "We didn't sleep at least as long as we requested!");
  49. }
  50. };
  51. CreateUnitTest(Check_timeManager, "Platform/Time/Manager")
  52. {
  53. void handleTimeEvent(S32 timeDelta)
  54. {
  55. mElapsedTime += timeDelta;
  56. mNumberCalls++;
  57. if(mElapsedTime >= 1000)
  58. Process::requestShutdown();
  59. }
  60. S32 mElapsedTime;
  61. S32 mNumberCalls;
  62. void run()
  63. {
  64. mElapsedTime = mNumberCalls = 0;
  65. // Initialize the time manager...
  66. TimeManager time;
  67. time.timeEvent.notify(this, &Check_timeManager::handleTimeEvent);
  68. // Event loop till at least one second has passed.
  69. const U32 start = Platform::getRealMilliseconds();
  70. while(Process::processEvents())
  71. {
  72. // If we go too long, kill it off...
  73. if(Platform::getRealMilliseconds() - start > 30*1000)
  74. {
  75. test(false, "Terminated process loop due to watchdog, not due to time manager event, after 30 seconds.");
  76. Process::requestShutdown();
  77. }
  78. }
  79. const U32 end = Platform::getRealMilliseconds();
  80. // Now, confirm we have approximately similar elapsed times.
  81. S32 elapsedRealTime = end - start;
  82. test(mAbs(elapsedRealTime - mElapsedTime) < 50, "Failed to elapse time to within the desired tolerance.");
  83. test(mNumberCalls > 0, "Somehow got no event callbacks from TimeManager?");
  84. Con::printf(" Got %d time events, and elapsed %dms from TimeManager, "
  85. "%dms according to Platform::getRealMilliseconds()",
  86. mNumberCalls, mElapsedTime, elapsedRealTime);
  87. }
  88. };