timeoutManager.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var timeoutManager_exports = {};
  20. __export(timeoutManager_exports, {
  21. TimeoutManager: () => TimeoutManager,
  22. TimeoutManagerError: () => TimeoutManagerError,
  23. kMaxDeadline: () => kMaxDeadline
  24. });
  25. module.exports = __toCommonJS(timeoutManager_exports);
  26. var import_utils = require("playwright-core/lib/utils");
  27. var import_utils2 = require("playwright-core/lib/utils");
  28. var import_util = require("../util");
  29. const kMaxDeadline = 2147483647;
  30. class TimeoutManager {
  31. constructor(timeout) {
  32. this._ignoreTimeouts = false;
  33. this._defaultSlot = { timeout, elapsed: 0 };
  34. }
  35. setIgnoreTimeouts() {
  36. this._ignoreTimeouts = true;
  37. if (this._running)
  38. this._updateTimeout(this._running);
  39. }
  40. interrupt() {
  41. if (this._running)
  42. this._running.timeoutPromise.reject(this._createTimeoutError(this._running));
  43. }
  44. isTimeExhaustedFor(runnable) {
  45. const slot = runnable.fixture?.slot || runnable.slot || this._defaultSlot;
  46. return slot.timeout > 0 && slot.elapsed >= slot.timeout - 1;
  47. }
  48. async withRunnable(runnable, cb) {
  49. if (this._running)
  50. throw new Error(`Internal error: duplicate runnable`);
  51. const running = this._running = {
  52. runnable,
  53. slot: runnable.fixture?.slot || runnable.slot || this._defaultSlot,
  54. start: (0, import_utils.monotonicTime)(),
  55. deadline: kMaxDeadline,
  56. timer: void 0,
  57. timeoutPromise: new import_utils.ManualPromise()
  58. };
  59. let debugTitle = "";
  60. try {
  61. if (import_util.debugTest.enabled) {
  62. debugTitle = runnable.fixture ? `${runnable.fixture.phase} "${runnable.fixture.title}"` : runnable.type;
  63. const location = runnable.location ? ` at "${(0, import_util.formatLocation)(runnable.location)}"` : ``;
  64. (0, import_util.debugTest)(`started ${debugTitle}${location}`);
  65. }
  66. this._updateTimeout(running);
  67. return await Promise.race([
  68. cb(),
  69. running.timeoutPromise
  70. ]);
  71. } finally {
  72. if (running.timer)
  73. clearTimeout(running.timer);
  74. running.timer = void 0;
  75. running.slot.elapsed += (0, import_utils.monotonicTime)() - running.start;
  76. this._running = void 0;
  77. if (import_util.debugTest.enabled)
  78. (0, import_util.debugTest)(`finished ${debugTitle}`);
  79. }
  80. }
  81. _updateTimeout(running) {
  82. if (running.timer)
  83. clearTimeout(running.timer);
  84. running.timer = void 0;
  85. if (this._ignoreTimeouts || !running.slot.timeout) {
  86. running.deadline = kMaxDeadline;
  87. return;
  88. }
  89. running.deadline = running.start + (running.slot.timeout - running.slot.elapsed);
  90. const timeout = running.deadline - (0, import_utils.monotonicTime)() + 1;
  91. if (timeout <= 0)
  92. running.timeoutPromise.reject(this._createTimeoutError(running));
  93. else
  94. running.timer = setTimeout(() => running.timeoutPromise.reject(this._createTimeoutError(running)), timeout);
  95. }
  96. defaultSlot() {
  97. return this._defaultSlot;
  98. }
  99. slow() {
  100. const slot = this._running ? this._running.slot : this._defaultSlot;
  101. slot.timeout = slot.timeout * 3;
  102. if (this._running)
  103. this._updateTimeout(this._running);
  104. }
  105. setTimeout(timeout) {
  106. const slot = this._running ? this._running.slot : this._defaultSlot;
  107. slot.timeout = timeout;
  108. if (this._running)
  109. this._updateTimeout(this._running);
  110. }
  111. currentSlotDeadline() {
  112. return this._running ? this._running.deadline : kMaxDeadline;
  113. }
  114. currentSlotType() {
  115. return this._running ? this._running.runnable.type : "test";
  116. }
  117. _createTimeoutError(running) {
  118. let message = "";
  119. const timeout = running.slot.timeout;
  120. const runnable = running.runnable;
  121. switch (runnable.type) {
  122. case "test": {
  123. if (runnable.fixture) {
  124. if (runnable.fixture.phase === "setup")
  125. message = `Test timeout of ${timeout}ms exceeded while setting up "${runnable.fixture.title}".`;
  126. else
  127. message = `Tearing down "${runnable.fixture.title}" exceeded the test timeout of ${timeout}ms.`;
  128. } else {
  129. message = `Test timeout of ${timeout}ms exceeded.`;
  130. }
  131. break;
  132. }
  133. case "afterEach":
  134. case "beforeEach":
  135. message = `Test timeout of ${timeout}ms exceeded while running "${runnable.type}" hook.`;
  136. break;
  137. case "beforeAll":
  138. case "afterAll":
  139. message = `"${runnable.type}" hook timeout of ${timeout}ms exceeded.`;
  140. break;
  141. case "teardown": {
  142. if (runnable.fixture)
  143. message = `Worker teardown timeout of ${timeout}ms exceeded while ${runnable.fixture.phase === "setup" ? "setting up" : "tearing down"} "${runnable.fixture.title}".`;
  144. else
  145. message = `Worker teardown timeout of ${timeout}ms exceeded.`;
  146. break;
  147. }
  148. case "skip":
  149. case "slow":
  150. case "fixme":
  151. case "fail":
  152. message = `"${runnable.type}" modifier timeout of ${timeout}ms exceeded.`;
  153. break;
  154. }
  155. const fixtureWithSlot = runnable.fixture?.slot ? runnable.fixture : void 0;
  156. if (fixtureWithSlot)
  157. message = `Fixture "${fixtureWithSlot.title}" timeout of ${timeout}ms exceeded during ${fixtureWithSlot.phase}.`;
  158. message = import_utils2.colors.red(message);
  159. const location = (fixtureWithSlot || runnable).location;
  160. const error = new TimeoutManagerError(message);
  161. error.name = "";
  162. error.stack = message + (location ? `
  163. at ${location.file}:${location.line}:${location.column}` : "");
  164. return error;
  165. }
  166. }
  167. class TimeoutManagerError extends Error {
  168. }
  169. // Annotate the CommonJS export names for ESM import in node:
  170. 0 && (module.exports = {
  171. TimeoutManager,
  172. TimeoutManagerError,
  173. kMaxDeadline
  174. });