taskRunner.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 taskRunner_exports = {};
  20. __export(taskRunner_exports, {
  21. TaskRunner: () => TaskRunner
  22. });
  23. module.exports = __toCommonJS(taskRunner_exports);
  24. var import_utils = require("playwright-core/lib/utils");
  25. var import_utils2 = require("playwright-core/lib/utils");
  26. var import_utilsBundle = require("playwright-core/lib/utilsBundle");
  27. var import_sigIntWatcher = require("./sigIntWatcher");
  28. var import_util = require("../util");
  29. class TaskRunner {
  30. constructor(reporter, globalTimeoutForError) {
  31. this._tasks = [];
  32. this._hasErrors = false;
  33. this._interrupted = false;
  34. this._isTearDown = false;
  35. this._reporter = reporter;
  36. this._globalTimeoutForError = globalTimeoutForError;
  37. }
  38. addTask(task) {
  39. this._tasks.push(task);
  40. }
  41. async run(context, deadline, cancelPromise) {
  42. const { status, cleanup } = await this.runDeferCleanup(context, deadline, cancelPromise);
  43. const teardownStatus = await cleanup();
  44. return status === "passed" ? teardownStatus : status;
  45. }
  46. async runDeferCleanup(context, deadline, cancelPromise = new import_utils.ManualPromise()) {
  47. const sigintWatcher = new import_sigIntWatcher.SigIntWatcher();
  48. const timeoutWatcher = new TimeoutWatcher(deadline);
  49. const teardownRunner = new TaskRunner(this._reporter, this._globalTimeoutForError);
  50. teardownRunner._isTearDown = true;
  51. let currentTaskName;
  52. const taskLoop = async () => {
  53. for (const task of this._tasks) {
  54. currentTaskName = task.title;
  55. if (this._interrupted)
  56. break;
  57. (0, import_utilsBundle.debug)("pw:test:task")(`"${task.title}" started`);
  58. const errors = [];
  59. const softErrors = [];
  60. try {
  61. teardownRunner._tasks.unshift({ title: `teardown for ${task.title}`, setup: task.teardown });
  62. await task.setup?.(context, errors, softErrors);
  63. } catch (e) {
  64. (0, import_utilsBundle.debug)("pw:test:task")(`error in "${task.title}": `, e);
  65. errors.push((0, import_util.serializeError)(e));
  66. } finally {
  67. for (const error of [...softErrors, ...errors])
  68. this._reporter.onError?.(error);
  69. if (errors.length) {
  70. if (!this._isTearDown)
  71. this._interrupted = true;
  72. this._hasErrors = true;
  73. }
  74. }
  75. (0, import_utilsBundle.debug)("pw:test:task")(`"${task.title}" finished`);
  76. }
  77. };
  78. await Promise.race([
  79. taskLoop(),
  80. cancelPromise,
  81. sigintWatcher.promise(),
  82. timeoutWatcher.promise
  83. ]);
  84. sigintWatcher.disarm();
  85. timeoutWatcher.disarm();
  86. this._interrupted = true;
  87. let status = "passed";
  88. if (sigintWatcher.hadSignal() || cancelPromise?.isDone()) {
  89. status = "interrupted";
  90. } else if (timeoutWatcher.timedOut()) {
  91. this._reporter.onError?.({ message: import_utils2.colors.red(`Timed out waiting ${this._globalTimeoutForError / 1e3}s for the ${currentTaskName} to run`) });
  92. status = "timedout";
  93. } else if (this._hasErrors) {
  94. status = "failed";
  95. }
  96. cancelPromise?.resolve();
  97. const cleanup = () => teardownRunner.runDeferCleanup(context, deadline).then((r) => r.status);
  98. return { status, cleanup };
  99. }
  100. }
  101. class TimeoutWatcher {
  102. constructor(deadline) {
  103. this._timedOut = false;
  104. this.promise = new import_utils.ManualPromise();
  105. if (!deadline)
  106. return;
  107. if (deadline - (0, import_utils.monotonicTime)() <= 0) {
  108. this._timedOut = true;
  109. this.promise.resolve();
  110. return;
  111. }
  112. this._timer = setTimeout(() => {
  113. this._timedOut = true;
  114. this.promise.resolve();
  115. }, deadline - (0, import_utils.monotonicTime)());
  116. }
  117. timedOut() {
  118. return this._timedOut;
  119. }
  120. disarm() {
  121. clearTimeout(this._timer);
  122. }
  123. }
  124. // Annotate the CommonJS export names for ESM import in node:
  125. 0 && (module.exports = {
  126. TaskRunner
  127. });