xplatInterval.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. // test custom GetSystemTime caching on xplat
  6. function rand() {
  7. return parseInt(Math.random() * 1e2) + 50;
  8. }
  9. for (var j = 0; j < 1e2; j++) {
  10. var pre_time = Date.now(), now;
  11. for (var i = 0; i < 1e6; i++) {
  12. now = Date.now();
  13. var diff = now - pre_time
  14. // INTERVAL_FOR_TICK_BACKUP = 5
  15. // So, anything beyond 5ms is not subject to our testing here.
  16. if (diff < 0 && Math.abs(diff) <= 5) {
  17. throw new Error("Timer interval has failed. diff < 0 -> " + diff);
  18. }
  19. pre_time = now;
  20. }
  21. // wait rand time until next trial
  22. for (var i = 0, to = rand(); i < to; i++) {
  23. now = Date.now();
  24. }
  25. // INTERVAL_FOR_TICK_BACKUP = 5
  26. // So, anything beyond 5ms is not subject to our testing here.
  27. if (now < pre_time && Math.abs(now - pre_time) <= 5) {
  28. throw new Error("Timer interval has failed. now < pre_time");
  29. }
  30. }
  31. print("PASS");