Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / karma / lib / launchers / base.js
1 var KarmaEventEmitter = require('../events').EventEmitter
2 var EventEmitter = require('events').EventEmitter
3 var q = require('q')
4 var log = require('../logger').create('launcher')
5
6 var BEING_CAPTURED = 1
7 var CAPTURED = 2
8 var BEING_KILLED = 3
9 var FINISHED = 4
10 var RESTARTING = 5
11 var BEING_FORCE_KILLED = 6
12
13 /**
14  * Base launcher that any custom launcher extends.
15  */
16 var BaseLauncher = function (id, emitter) {
17   if (this.start) {
18     return
19   }
20
21   // TODO(vojta): figure out how to do inheritance with DI
22   Object.keys(EventEmitter.prototype).forEach(function (method) {
23     this[method] = EventEmitter.prototype[method]
24   }, this)
25   KarmaEventEmitter.call(this)
26
27   this.id = id
28   this.state = null
29   this.error = null
30
31   var self = this
32   var killingPromise
33   var previousUrl
34
35   this.start = function (url) {
36     previousUrl = url
37
38     this.error = null
39     this.state = BEING_CAPTURED
40     this.emit('start', url + '?id=' + this.id)
41   }
42
43   this.kill = function () {
44     // Already killed, or being killed.
45     if (killingPromise) {
46       return killingPromise
47     }
48
49     killingPromise = this.emitAsync('kill').then(function () {
50       self.state = FINISHED
51     })
52
53     this.state = BEING_KILLED
54
55     return killingPromise
56   }
57
58   this.forceKill = function () {
59     this.kill()
60     this.state = BEING_FORCE_KILLED
61
62     return killingPromise
63   }
64
65   this.restart = function () {
66     if (this.state === BEING_FORCE_KILLED) {
67       return
68     }
69
70     if (!killingPromise) {
71       killingPromise = this.emitAsync('kill')
72     }
73
74     killingPromise.then(function () {
75       if (self.state === BEING_FORCE_KILLED) {
76         self.state = FINISHED
77       } else {
78         killingPromise = null
79         log.debug('Restarting %s', self.name)
80         self.start(previousUrl)
81       }
82     })
83
84     self.state = RESTARTING
85   }
86
87   this.markCaptured = function () {
88     if (this.state === BEING_CAPTURED) {
89       this.state = CAPTURED
90     }
91   }
92
93   this.isCaptured = function () {
94     return this.state === CAPTURED
95   }
96
97   this.toString = function () {
98     return this.name
99   }
100
101   this._done = function (error) {
102     killingPromise = killingPromise || q()
103
104     this.error = this.error || error
105     this.emit('done')
106
107     if (this.error && this.state !== BEING_FORCE_KILLED && this.state !== RESTARTING) {
108       emitter.emit('browser_process_failure', this)
109     }
110
111     this.state = FINISHED
112   }
113
114   this.STATE_BEING_CAPTURED = BEING_CAPTURED
115   this.STATE_CAPTURED = CAPTURED
116   this.STATE_BEING_KILLED = BEING_KILLED
117   this.STATE_FINISHED = FINISHED
118   this.STATE_RESTARTING = RESTARTING
119   this.STATE_BEING_FORCE_KILLED = BEING_FORCE_KILLED
120 }
121
122 BaseLauncher.decoratorFactory = function (id, emitter) {
123   return function (launcher) {
124     BaseLauncher.call(launcher, id, emitter)
125   }
126 }
127
128 module.exports = BaseLauncher