File tree 2 files changed +36
-2
lines changed
2 files changed +36
-2
lines changed Original file line number Diff line number Diff line change @@ -138,6 +138,8 @@ internals.Connection.prototype._init = function () {
138
138
139
139
internals . Connection . prototype . _start = function ( callback ) {
140
140
141
+ var self = this ;
142
+
141
143
if ( this . _started ) {
142
144
return process . nextTick ( callback ) ;
143
145
}
@@ -149,12 +151,26 @@ internals.Connection.prototype._start = function (callback) {
149
151
return process . nextTick ( callback ) ;
150
152
}
151
153
154
+ var onError = function ( err ) {
155
+
156
+ self . _started = false ;
157
+ return callback ( err ) ;
158
+ } ;
159
+
160
+ this . listener . once ( 'error' , onError ) ;
161
+
162
+ var finalize = function ( ) {
163
+
164
+ self . listener . removeListener ( 'error' , onError ) ;
165
+ callback ( ) ;
166
+ } ;
167
+
152
168
if ( this . type !== 'tcp' ) {
153
- this . listener . listen ( this . settings . port , callback ) ;
169
+ this . listener . listen ( this . settings . port , finalize ) ;
154
170
}
155
171
else {
156
172
var address = this . settings . address || this . settings . host || '0.0.0.0' ;
157
- this . listener . listen ( this . settings . port , address , callback ) ;
173
+ this . listener . listen ( this . settings . port , address , finalize ) ;
158
174
}
159
175
} ;
160
176
Original file line number Diff line number Diff line change @@ -399,6 +399,24 @@ describe('Connection', function () {
399
399
} ) ;
400
400
} ) ;
401
401
} ) ;
402
+
403
+ it ( 'will return an error if the port is aleady in use' , function ( done ) {
404
+
405
+ var server = new Hapi . Server ( ) ;
406
+ server . connection ( ) ;
407
+
408
+ server . start ( function ( err ) {
409
+
410
+ expect ( err ) . to . not . exist ( ) ;
411
+ server . connection ( { port : server . info . port } ) ;
412
+ server . start ( function ( err ) {
413
+
414
+ expect ( err ) . to . exist ( ) ;
415
+ expect ( err . message ) . to . match ( / E A D D R I N U S E / ) ;
416
+ done ( ) ;
417
+ } ) ;
418
+ } ) ;
419
+ } ) ;
402
420
} ) ;
403
421
404
422
describe ( '_stop()' , function ( ) {
You can’t perform that action at this time.
0 commit comments