testing_clock.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. /* Enable POSIX features */
  5. #ifndef _XOPEN_SOURCE
  6. # define _XOPEN_SOURCE 600
  7. #endif
  8. #ifndef _DEFAULT_SOURCE
  9. # define _DEFAULT_SOURCE
  10. #endif
  11. /* On older systems we need to define _BSD_SOURCE.
  12. * _DEFAULT_SOURCE is an alias for that. */
  13. #ifndef _BSD_SOURCE
  14. # define _BSD_SOURCE
  15. #endif
  16. #include <time.h>
  17. #include "testing_clock.h"
  18. UA_DateTime testingClock = 0;
  19. UA_DateTime UA_DateTime_now(void) {
  20. return testingClock;
  21. }
  22. UA_DateTime UA_DateTime_nowMonotonic(void) {
  23. return testingClock;
  24. }
  25. void
  26. UA_fakeSleep(UA_UInt32 duration) {
  27. testingClock += duration * UA_MSEC_TO_DATETIME;
  28. }
  29. /* 1 millisecond = 1,000,000 Nanoseconds */
  30. #define NANO_SECOND_MULTIPLIER 1000000
  31. void
  32. UA_realSleep(UA_UInt32 duration) {
  33. UA_UInt32 sec = duration / 1000;
  34. UA_UInt32 ns = (duration % 1000) * NANO_SECOND_MULTIPLIER;
  35. struct timespec sleepValue;
  36. sleepValue.tv_sec = sec;
  37. sleepValue.tv_nsec = ns;
  38. nanosleep(&sleepValue, NULL);
  39. }