@@ -16,33 +16,57 @@ var Result = function() {
16
16
} ;
17
17
18
18
19
- var Browser = function ( id , collection , emitter ) {
20
- var log = logger . create ( id ) ;
19
+ // The browser is ready to execute tests.
20
+ var READY = 1 ;
21
+
22
+ // The browser is executing the tests/
23
+ var EXECUTING = 2 ;
24
+
25
+ // The browser is not executing, but temporarily disconnected (waiting for reconnecting).
26
+ var READY_DISCONNECTED = 3 ;
27
+
28
+ // The browser is executing the tests, but temporarily disconnect (waiting for reconnecting).
29
+ var EXECUTING_DISCONNECTED = 4 ;
30
+
31
+ // The browser got permanently disconnected (being removed from the collection and destroyed).
32
+ var DISCONNECTED = 5 ;
33
+
34
+
35
+ var Browser = function ( id , fullName , /* capturedBrowsers */ collection , emitter , socket , timer ,
36
+ /* config.browserDisconnectTimeout */ disconnectDelay ) {
37
+
38
+ var name = helper . browserFullNameToShort ( fullName ) ;
39
+ var log = logger . create ( name ) ;
21
40
22
41
this . id = id ;
23
- this . name = id ;
24
- this . fullName = null ;
25
- this . isReady = true ;
42
+ this . fullName = fullName ;
43
+ this . name = name ;
44
+ this . state = READY ;
26
45
this . lastResult = new Result ( ) ;
27
46
47
+ this . init = function ( ) {
48
+ collection . add ( this ) ;
28
49
29
- this . toString = function ( ) {
30
- return this . name ;
31
- } ;
50
+ events . bindAll ( this , socket ) ;
32
51
33
- this . onRegister = function ( info ) {
34
- this . launchId = info . id ;
35
- this . fullName = info . name ;
36
- this . name = helper . browserFullNameToShort ( this . fullName ) ;
37
- log = logger . create ( this . name ) ;
38
- log . info ( 'Connected on socket id ' + this . id ) ;
52
+ log . info ( 'Connected on socket %s' , socket . id ) ;
39
53
40
- emitter . emit ( 'browser_register' , this ) ;
54
+ // TODO(vojta): move to collection
41
55
emitter . emit ( 'browsers_change' , collection ) ;
56
+
57
+ emitter . emit ( 'browser_register' , this ) ;
58
+ } ;
59
+
60
+ this . isReady = function ( ) {
61
+ return this . state === READY ;
62
+ } ;
63
+
64
+ this . toString = function ( ) {
65
+ return this . name ;
42
66
} ;
43
67
44
68
this . onError = function ( error ) {
45
- if ( this . isReady ) {
69
+ if ( this . isReady ( ) ) {
46
70
return ;
47
71
}
48
72
@@ -51,7 +75,7 @@ var Browser = function(id, collection, emitter) {
51
75
} ;
52
76
53
77
this . onInfo = function ( info ) {
54
- if ( this . isReady ) {
78
+ if ( this . isReady ( ) ) {
55
79
return ;
56
80
}
57
81
@@ -70,11 +94,11 @@ var Browser = function(id, collection, emitter) {
70
94
} ;
71
95
72
96
this . onComplete = function ( result ) {
73
- if ( this . isReady ) {
97
+ if ( this . isReady ( ) ) {
74
98
return ;
75
99
}
76
100
77
- this . isReady = true ;
101
+ this . state = READY ;
78
102
this . lastResult . totalTimeEnd ( ) ;
79
103
80
104
if ( ! this . lastResult . success ) {
@@ -85,21 +109,50 @@ var Browser = function(id, collection, emitter) {
85
109
emitter . emit ( 'browser_complete' , this , result ) ;
86
110
} ;
87
111
112
+ var self = this ;
113
+ var disconnect = function ( ) {
114
+ self . state = DISCONNECTED ;
115
+ log . warn ( 'Disconnected' ) ;
116
+ collection . remove ( self ) ;
117
+ } ;
118
+
119
+ var pendingDisconnect ;
88
120
this . onDisconnect = function ( ) {
89
- if ( ! this . isReady ) {
90
- this . isReady = true ;
91
- this . lastResult . totalTimeEnd ( ) ;
92
- this . lastResult . disconnected = true ;
93
- emitter . emit ( 'browser_complete' , this ) ;
121
+ if ( this . state === READY ) {
122
+ disconnect ( ) ;
123
+ } else if ( this . state === EXECUTING ) {
124
+ log . debug ( 'Disconnected during run, waiting for reconnecting.' ) ;
125
+ this . state = EXECUTING_DISCONNECTED ;
126
+
127
+ pendingDisconnect = timer . setTimeout ( function ( ) {
128
+ self . lastResult . totalTimeEnd ( ) ;
129
+ self . lastResult . disconnected = true ;
130
+ disconnect ( ) ;
131
+ emitter . emit ( 'browser_complete' , self ) ;
132
+ } , disconnectDelay ) ;
94
133
}
134
+ } ;
95
135
96
- log . warn ( 'Disconnected' ) ;
97
- collection . remove ( this ) ;
136
+ this . onReconnect = function ( newSocket ) {
137
+ if ( this . state === EXECUTING_DISCONNECTED ) {
138
+ this . state = EXECUTING ;
139
+ log . debug ( 'Reconnected.' ) ;
140
+ } else if ( this . state === EXECUTING || this . state === READY ) {
141
+ log . debug ( 'New connection, forgetting the old one.' ) ;
142
+ // TODO(vojta): this should only remove this browser.onDisconnect listener
143
+ socket . removeAllListeners ( 'disconnect' ) ;
144
+ }
145
+
146
+ socket = newSocket ;
147
+ events . bindAll ( this , newSocket ) ;
148
+ if ( pendingDisconnect ) {
149
+ timer . clearTimeout ( pendingDisconnect ) ;
150
+ }
98
151
} ;
99
152
100
153
this . onResult = function ( result ) {
101
154
// ignore - probably results from last run (after server disconnecting)
102
- if ( this . isReady ) {
155
+ if ( this . isReady ( ) ) {
103
156
return ;
104
157
}
105
158
@@ -119,11 +172,17 @@ var Browser = function(id, collection, emitter) {
119
172
return {
120
173
id : this . id ,
121
174
name : this . name ,
122
- isReady : this . isReady
175
+ isReady : this . state === READY
123
176
} ;
124
177
} ;
125
178
} ;
126
179
180
+ Browser . STATE_READY = READY ;
181
+ Browser . STATE_EXECUTING = EXECUTING ;
182
+ Browser . STATE_READY_DISCONNECTED = READY_DISCONNECTED ;
183
+ Browser . STATE_EXECUTING_DISCONNECTED = EXECUTING_DISCONNECTED ;
184
+ Browser . STATE_DISCONNECTED = DISCONNECTED ;
185
+
127
186
128
187
var Collection = function ( emitter , browsers ) {
129
188
browsers = browsers || [ ] ;
@@ -146,23 +205,29 @@ var Collection = function(emitter, browsers) {
146
205
return true ;
147
206
} ;
148
207
149
- this . setAllIsReadyTo = function ( value ) {
150
- var change = false ;
208
+ this . getById = function ( browserId ) {
209
+ for ( var i = 0 ; i < browsers . length ; i ++ ) {
210
+ if ( browsers [ i ] . id === browserId ) {
211
+ return browsers [ i ] ;
212
+ }
213
+ }
214
+
215
+ return null ;
216
+ } ;
217
+
218
+ this . setAllToExecuting = function ( ) {
151
219
browsers . forEach ( function ( browser ) {
152
- change = change || browser . isReady !== value ;
153
- browser . isReady = value ;
220
+ browser . state = EXECUTING ;
154
221
} ) ;
155
222
156
- if ( change ) {
157
- emitter . emit ( 'browsers_change' , this ) ;
158
- }
223
+ emitter . emit ( 'browsers_change' , this ) ;
159
224
} ;
160
225
161
226
this . areAllReady = function ( nonReadyList ) {
162
227
nonReadyList = nonReadyList || [ ] ;
163
228
164
229
browsers . forEach ( function ( browser ) {
165
- if ( ! browser . isReady ) {
230
+ if ( ! browser . isReady ( ) ) {
166
231
nonReadyList . push ( browser ) ;
167
232
}
168
233
} ) ;
@@ -222,12 +287,3 @@ Collection.$inject = ['emitter'];
222
287
exports . Result = Result ;
223
288
exports . Browser = Browser ;
224
289
exports . Collection = Collection ;
225
-
226
- exports . createBrowser = function ( socket , collection , emitter ) {
227
- var browser = new Browser ( socket . id , collection , emitter ) ;
228
-
229
- events . bindAll ( browser , socket ) ;
230
- collection . add ( browser ) ;
231
-
232
- return browser ;
233
- } ;
0 commit comments