capture_timeout.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. var log = require('../logger').create('launcher')
  2. /**
  3. * Kill browser if it does not capture in given `captureTimeout`.
  4. */
  5. var CaptureTimeoutLauncher = function (timer, captureTimeout) {
  6. if (!captureTimeout) {
  7. return
  8. }
  9. var self = this
  10. var pendingTimeoutId = null
  11. this.on('start', function () {
  12. pendingTimeoutId = timer.setTimeout(function () {
  13. pendingTimeoutId = null
  14. if (self.state !== self.STATE_BEING_CAPTURED) {
  15. return
  16. }
  17. log.warn('%s have not captured in %d ms, killing.', self.name, captureTimeout)
  18. self.error = 'timeout'
  19. self.kill()
  20. }, captureTimeout)
  21. })
  22. this.on('done', function () {
  23. if (pendingTimeoutId) {
  24. timer.clearTimeout(pendingTimeoutId)
  25. pendingTimeoutId = null
  26. }
  27. })
  28. }
  29. CaptureTimeoutLauncher.decoratorFactory = function (timer,
  30. /* config.captureTimeout */ captureTimeout) {
  31. return function (launcher) {
  32. CaptureTimeoutLauncher.call(launcher, timer, captureTimeout)
  33. }
  34. }
  35. CaptureTimeoutLauncher.decoratorFactory.$inject = ['timer', 'config.captureTimeout']
  36. module.exports = CaptureTimeoutLauncher