sleep.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Phoenix Shell.
  5. *
  6. * Phoenix Shell is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import assert from 'assert';
  20. import sinon from 'sinon';
  21. import { MakeTestContext } from './harness.js';
  22. import builtins from '../../src/puter-shell/coreutils/__exports__.js';
  23. export const runSleepTests = () => {
  24. describe('sleep', function () {
  25. let clock;
  26. beforeEach(() => {
  27. clock = sinon.useFakeTimers();
  28. });
  29. afterEach(() => {
  30. clock.restore();
  31. });
  32. const failureCases = [
  33. {
  34. description: 'expects at least 1 argument',
  35. positionals: [],
  36. },
  37. {
  38. description: 'expects at most 1 argument',
  39. positionals: ['1', '2'],
  40. },
  41. {
  42. description: 'expects its argument to be a number',
  43. positionals: ['frog'],
  44. },
  45. {
  46. description: 'expects its argument to be positive',
  47. positionals: ['-1'],
  48. },
  49. ];
  50. for (const { description, positionals } of failureCases) {
  51. it(description, async () => {
  52. let ctx = MakeTestContext(builtins.sleep, { positionals });
  53. let hadError = false;
  54. try {
  55. await builtins.sleep.execute(ctx);
  56. } catch (e) {
  57. hadError = true;
  58. }
  59. if (!hadError) {
  60. assert.fail('didn\'t return an error code');
  61. }
  62. assert.equal(ctx.externs.out.output, '', 'nothing should be written to stdout');
  63. // Output to stderr is allowed but not required.
  64. });
  65. }
  66. const testCases = [
  67. {
  68. description: 'sleep 0.5',
  69. positionals: ['0.5'],
  70. durationS: 0.5,
  71. },
  72. {
  73. description: 'sleep 1',
  74. positionals: ['1'],
  75. durationS: 1,
  76. },
  77. {
  78. description: 'sleep 1.5',
  79. positionals: ['1.5'],
  80. durationS: 1.5,
  81. },
  82. {
  83. description: 'sleep 27',
  84. positionals: ['27'],
  85. durationS: 27,
  86. },
  87. ];
  88. for (const { description, positionals, durationS } of testCases) {
  89. it(description, async () => {
  90. const durationMs = durationS * 1000;
  91. let ctx = MakeTestContext(builtins.sleep, { positionals });
  92. const startTimeMs = performance.now();
  93. let endTimeMs;
  94. builtins.sleep.execute(ctx)
  95. .then(() => { endTimeMs = performance.now(); })
  96. .catch((e) => { assert.fail(e); });
  97. await clock.tickAsync(durationMs - 5);
  98. assert.ok(endTimeMs === undefined, `sleep took less than ${durationS}s, took ${(endTimeMs - startTimeMs) / 1000}s`);
  99. await clock.tickAsync(10);
  100. assert.ok(endTimeMs !== undefined, `sleep took more than ${durationS}s, not done after ${(durationS + 0.005)}s`);
  101. assert.equal(ctx.externs.out.output, '', 'sleep should not write to stdout');
  102. assert.equal(ctx.externs.err.output, '', 'sleep should not write to stderr');
  103. });
  104. }
  105. });
  106. };