testing_clock.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include "testing_clock.h"
  5. #include <time.h>
  6. /* To avoid zero timestamp value in header, the testingClock
  7. * is assigned with non-zero timestamp to pass unit tests */
  8. UA_DateTime testingClock = 0x5C8F735D;
  9. UA_DateTime UA_DateTime_now(void) {
  10. return testingClock;
  11. }
  12. UA_DateTime UA_DateTime_nowMonotonic(void) {
  13. return testingClock;
  14. }
  15. UA_DateTime UA_DateTime_localTimeUtcOffset(void) {
  16. return 0;
  17. }
  18. void
  19. UA_fakeSleep(UA_UInt32 duration) {
  20. testingClock += duration * UA_DATETIME_MSEC;
  21. }
  22. /* 1 millisecond = 1,000,000 Nanoseconds */
  23. #define NANO_SECOND_MULTIPLIER 1000000
  24. void
  25. UA_realSleep(UA_UInt32 duration) {
  26. #ifdef _WIN32
  27. Sleep(duration);
  28. #else
  29. UA_UInt32 sec = duration / 1000;
  30. UA_UInt32 ns = (duration % 1000) * NANO_SECOND_MULTIPLIER;
  31. struct timespec sleepValue;
  32. sleepValue.tv_sec = sec;
  33. sleepValue.tv_nsec = ns;
  34. nanosleep(&sleepValue, NULL);
  35. #endif
  36. }
  37. /* Defined for the architecture via
  38. * add_definitions(-DUA_sleep_ms=UA_comboSleep) */
  39. void
  40. UA_comboSleep(unsigned long duration) {
  41. UA_fakeSleep((UA_UInt32)duration);
  42. UA_realSleep((UA_UInt32)duration);
  43. }