@@ -5,30 +5,51 @@ var config = require('./config');
55var fs = require ( 'fs' ) ;
66var path = require ( 'path' ) ;
77var spawn = require ( 'win-spawn' ) ;
8- var spawnFailed = false ;
98var tcpPortUsed = require ( 'tcp-port-used' ) ;
9+ var bluebird = require ( 'bluebird' ) ;
1010
1111// wait for redis to be listening in
1212// all three modes (ipv4, ipv6, socket).
13- function waitForRedis ( available , cb ) {
13+ function waitForRedis ( available , cb , port ) {
1414 if ( process . platform === 'win32' ) return cb ( ) ;
1515
16- var ipV4 = false ;
16+ var time = Date . now ( ) ;
17+ var running = false ;
18+ var socket = '/tmp/redis.sock' ;
19+ if ( port ) {
20+ // We have to distinguishe the redis sockets if we have more than a single redis instance running
21+ socket = '/tmp/redis' + port + '.sock' ;
22+ }
23+ port = port || config . PORT ;
1724 var id = setInterval ( function ( ) {
18- tcpPortUsed . check ( config . PORT , '127.0.0.1' ) . then ( function ( _ipV4 ) {
19- ipV4 = _ipV4 ;
20- return tcpPortUsed . check ( config . PORT , '::1' ) ;
21- } ) . then ( function ( ipV6 ) {
22- if ( ipV6 === available && ipV4 === available && fs . existsSync ( '/tmp/redis.sock' ) === available ) {
23- clearInterval ( id ) ;
24- return cb ( ) ;
25+ if ( running ) return ;
26+ running = true ;
27+ bluebird . join (
28+ tcpPortUsed . check ( port , '127.0.0.1' ) ,
29+ tcpPortUsed . check ( port , '::1' ) ,
30+ function ( ipV4 , ipV6 ) {
31+ if ( ipV6 === available && ipV4 === available ) {
32+ if ( fs . existsSync ( socket ) === available ) {
33+ clearInterval ( id ) ;
34+ return cb ( ) ;
35+ }
36+ // The same message applies for can't stop but we ignore that case
37+ throw new Error ( 'Port ' + port + ' is already in use. Tests can\'t start.\n' ) ;
2538 }
39+ if ( Date . now ( ) - time > 6000 ) {
40+ throw new Error ( 'Redis could not start on port ' + ( port || config . PORT ) + '\n' ) ;
41+ }
42+ running = false ;
43+ } ) . catch ( function ( err ) {
44+ console . error ( '\x1b[31m' + err . stack + '\x1b[0m\n' ) ;
45+ process . exit ( 1 ) ;
2646 } ) ;
2747 } , 100 ) ;
2848}
2949
3050module . exports = {
31- start : function ( done , conf ) {
51+ start : function ( done , conf , port ) {
52+ var spawnFailed = false ;
3253 // spawn redis with our testing configuration.
3354 var confFile = conf || path . resolve ( __dirname , '../conf/redis.conf' ) ;
3455 var rp = spawn ( "redis-server" , [ confFile ] , { } ) ;
@@ -53,15 +74,15 @@ module.exports = {
5374 rp . once ( "exit" , function ( code ) {
5475 var error = null ;
5576 if ( code !== null && code !== 0 ) {
56- error = Error ( 'Redis shutdown failed with code ' + code ) ;
77+ error = new Error ( 'Redis shutdown failed with code ' + code ) ;
5778 }
5879 waitForRedis ( false , function ( ) {
5980 return done ( error ) ;
60- } ) ;
81+ } , port ) ;
6182 } ) ;
6283 rp . kill ( "SIGTERM" ) ;
6384 }
6485 } ) ;
65- } ) ;
86+ } , port ) ;
6687 }
6788} ;
0 commit comments