From ee537a0dabc9d40d8dd3c1f4b3bf0edcb637915e Mon Sep 17 00:00:00 2001 From: Jonas Pommerening Date: Tue, 29 Jul 2014 11:28:36 +0200 Subject: [PATCH] fix(server): launch browsers when file_list is ready If we have a lot of files and a fast starting browser (PhantomJS), the browser might try to connect to the web server before the server is ready to reply, causing the connect mechanism to time out. Make sure to wait until fileList.refresh() is complete before starting the browsers. --- lib/server.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/server.js b/lib/server.js index c623b1e9c..7cb98c76f 100644 --- a/lib/server.js +++ b/lib/server.js @@ -34,7 +34,7 @@ var start = function(injector, config, launcher, globalEmitter, preprocess, file var filesPromise = fileList.refresh(); if (config.autoWatch) { - filesPromise.then(function() { + filesPromise = filesPromise.then(function() { injector.invoke(watcher.watch); }, function() { injector.invoke(watcher.watch); @@ -61,15 +61,21 @@ var start = function(injector, config, launcher, globalEmitter, preprocess, file // Some browsers did not get captured. var singleRunBrowserNotCaptured = false; - webServer.listen(config.port, function() { - log.info('Karma v%s server started at http://%s:%s%s', constant.VERSION, config.hostname, - config.port, config.urlRoot); - + var launchSingleRunBrowsers = function() { if (config.browsers && config.browsers.length) { injector.invoke(launcher.launch, launcher).forEach(function(browserLauncher) { singleRunDoneBrowsers[browserLauncher.id] = false; }); } + }; + + webServer.listen(config.port, function() { + log.info('Karma v%s server started at http://%s:%s%s', constant.VERSION, config.hostname, + config.port, config.urlRoot); + + // Wait until fileList.refresh() is done, or else we might + // not be ready to reply to the browser's capture request. + filesPromise.then(launchSingleRunBrowsers, launchSingleRunBrowsers); }); globalEmitter.on('browsers_change', function() {