timeoutManager.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.TimeoutManager = void 0;
  6. var _utilsBundle = require("playwright-core/lib/utilsBundle");
  7. var _utils = require("playwright-core/lib/utils");
  8. /**
  9. * Copyright Microsoft Corporation. All rights reserved.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. class TimeoutManager {
  24. constructor(timeout) {
  25. this._defaultSlot = void 0;
  26. this._defaultRunnable = void 0;
  27. this._runnable = void 0;
  28. this._fixture = void 0;
  29. this._timeoutRunner = void 0;
  30. this._defaultSlot = {
  31. timeout,
  32. elapsed: 0
  33. };
  34. this._defaultRunnable = {
  35. type: 'test',
  36. slot: this._defaultSlot
  37. };
  38. this._runnable = this._defaultRunnable;
  39. this._timeoutRunner = new _utils.TimeoutRunner(timeout);
  40. }
  41. interrupt() {
  42. this._timeoutRunner.interrupt();
  43. }
  44. async withRunnable(runnable, cb) {
  45. const existingRunnable = this._runnable;
  46. const effectiveRunnable = {
  47. ...runnable
  48. };
  49. if (!effectiveRunnable.slot) effectiveRunnable.slot = this._runnable.slot;
  50. this._updateRunnables(effectiveRunnable, undefined);
  51. try {
  52. return await cb();
  53. } finally {
  54. this._updateRunnables(existingRunnable, undefined);
  55. }
  56. }
  57. setCurrentFixture(fixture) {
  58. this._updateRunnables(this._runnable, fixture);
  59. }
  60. defaultSlotTimings() {
  61. const slot = this._currentSlot();
  62. slot.elapsed = this._timeoutRunner.elapsed();
  63. return this._defaultSlot;
  64. }
  65. slow() {
  66. const slot = this._currentSlot();
  67. slot.timeout = slot.timeout * 3;
  68. this._timeoutRunner.updateTimeout(slot.timeout);
  69. }
  70. async runWithTimeout(cb) {
  71. try {
  72. await this._timeoutRunner.run(cb);
  73. } catch (error) {
  74. if (!(error instanceof _utils.TimeoutRunnerError)) throw error;
  75. return this._createTimeoutError();
  76. }
  77. }
  78. setTimeout(timeout) {
  79. const slot = this._currentSlot();
  80. if (!slot.timeout) return; // Zero timeout means some debug mode - do not set a timeout.
  81. slot.timeout = timeout;
  82. this._timeoutRunner.updateTimeout(timeout);
  83. }
  84. currentRunnableType() {
  85. return this._runnable.type;
  86. }
  87. currentSlotDeadline() {
  88. return this._timeoutRunner.deadline();
  89. }
  90. _currentSlot() {
  91. var _this$_fixture;
  92. return ((_this$_fixture = this._fixture) === null || _this$_fixture === void 0 ? void 0 : _this$_fixture.slot) || this._runnable.slot || this._defaultSlot;
  93. }
  94. _updateRunnables(runnable, fixture) {
  95. let slot = this._currentSlot();
  96. slot.elapsed = this._timeoutRunner.elapsed();
  97. this._runnable = runnable;
  98. this._fixture = fixture;
  99. slot = this._currentSlot();
  100. this._timeoutRunner.updateTimeout(slot.timeout, slot.elapsed);
  101. }
  102. _createTimeoutError() {
  103. var _this$_fixture2;
  104. let message = '';
  105. const timeout = this._currentSlot().timeout;
  106. switch (this._runnable.type) {
  107. case 'afterHooks':
  108. case 'test':
  109. {
  110. if (this._fixture) {
  111. if (this._fixture.phase === 'setup') {
  112. message = `Test timeout of ${timeout}ms exceeded while setting up "${this._fixture.title}".`;
  113. } else {
  114. message = [`Test finished within timeout of ${timeout}ms, but tearing down "${this._fixture.title}" ran out of time.`, `Please allow more time for the test, since teardown is attributed towards the test timeout budget.`].join('\n');
  115. }
  116. } else {
  117. message = `Test timeout of ${timeout}ms exceeded.`;
  118. }
  119. break;
  120. }
  121. case 'afterEach':
  122. case 'beforeEach':
  123. message = `Test timeout of ${timeout}ms exceeded while running "${this._runnable.type}" hook.`;
  124. break;
  125. case 'beforeAll':
  126. case 'afterAll':
  127. message = `"${this._runnable.type}" hook timeout of ${timeout}ms exceeded.`;
  128. break;
  129. case 'teardown':
  130. {
  131. if (this._fixture) message = `Worker teardown timeout of ${timeout}ms exceeded while ${this._fixture.phase === 'setup' ? 'setting up' : 'tearing down'} "${this._fixture.title}".`;else message = `Worker teardown timeout of ${timeout}ms exceeded.`;
  132. break;
  133. }
  134. case 'skip':
  135. case 'slow':
  136. case 'fixme':
  137. case 'fail':
  138. message = `"${this._runnable.type}" modifier timeout of ${timeout}ms exceeded.`;
  139. break;
  140. }
  141. const fixtureWithSlot = (_this$_fixture2 = this._fixture) !== null && _this$_fixture2 !== void 0 && _this$_fixture2.slot ? this._fixture : undefined;
  142. if (fixtureWithSlot) message = `Fixture "${fixtureWithSlot.title}" timeout of ${timeout}ms exceeded during ${fixtureWithSlot.phase}.`;
  143. message = _utilsBundle.colors.red(message);
  144. const location = (fixtureWithSlot || this._runnable).location;
  145. return {
  146. message,
  147. // Include location for hooks, modifiers and fixtures to distinguish between them.
  148. stack: location ? message + `\n at ${location.file}:${location.line}:${location.column}` : undefined
  149. };
  150. }
  151. }
  152. exports.TimeoutManager = TimeoutManager;