testing_clock.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. void
  38. UA_comboSleep(unsigned long duration) {
  39. UA_fakeSleep((UA_UInt32)duration);
  40. UA_realSleep((UA_UInt32)duration);
  41. }