From d27edeaa7e5ff06ae0b247842e5633aeae492f82 Mon Sep 17 00:00:00 2001
From: Ivan Socolsky <jungans@gmail.com>
Date: Fri, 3 Apr 2015 15:43:22 -0300
Subject: [PATCH] add delay in async scan execution

---
 lib/lock.js                | 7 ++++---
 lib/server.js              | 4 ++--
 test/integration/server.js | 9 ++-------
 3 files changed, 8 insertions(+), 12 deletions(-)

diff --git a/lib/lock.js b/lib/lock.js
index 1713907..6ca71a4 100644
--- a/lib/lock.js
+++ b/lib/lock.js
@@ -1,13 +1,14 @@
 var _ = require('lodash');
+var $ = require('preconditions').singleton();
 
 var locks = {};
 
-var Lock = function () {
+var Lock = function() {
   this.taken = false;
   this.queue = [];
 };
 
-Lock.prototype.free = function () {
+Lock.prototype.free = function() {
   if (this.queue.length > 0) {
     var f = this.queue.shift();
     f(this);
@@ -16,7 +17,7 @@ Lock.prototype.free = function () {
   }
 };
 
-Lock.get = function (key, callback) {
+Lock.get = function(key, callback) {
   if (_.isUndefined(locks[key])) {
     locks[key] = new Lock();
   }
diff --git a/lib/server.js b/lib/server.js
index 07ba841..86e6fa1 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -1230,9 +1230,9 @@ WalletService.prototype.startScan = function(opts, cb) {
 
       setTimeout(function() {
         self.scan(opts, scanFinished);
-      }, 0);
+      }, 100);
 
-      return cb()
+      return cb();
     });
   });
 };
diff --git a/test/integration/server.js b/test/integration/server.js
index 9b33315..cf65be4 100644
--- a/test/integration/server.js
+++ b/test/integration/server.js
@@ -2688,13 +2688,12 @@ describe('Wallet service', function() {
     });
     it('should start multiple asynchronous scans for different wallets', function(done) {
       helpers.stubAddressActivity(['3K2VWMXheGZ4qG35DyGjA2dLeKfaSr534A']);
-      WalletService.scanConfig.SCAN_WINDOW = 2;
+      WalletService.scanConfig.SCAN_WINDOW = 1;
 
       var scans = 0;
       WalletService.onNotification(function(n) {
         if (n.type == 'ScanFinished') {
           scans++;
-          // console.log('*** [server.js ln2697] n:', n); // TODO
           if (scans == 2) done();
         }
       });
@@ -2718,17 +2717,13 @@ describe('Wallet service', function() {
         server.joinWallet(copayerOpts, function(err, result) {
           should.not.exist(err);
           helpers.getAuthServer(result.copayerId, function(server2) {
-            server.startScan({
-              includeCopayerBranches: true
-            }, function(err) {
+            server.startScan({}, function(err) {
               should.not.exist(err);
               scans.should.equal(0);
-              // console.log('*** [server.js ln2726] scans:', scans); // TODO
             });
             server2.startScan({}, function(err) {
               should.not.exist(err);
               scans.should.equal(0);
-              // console.log('*** [server.js ln2731] scans:', scans); // TODO
             });
             scans.should.equal(0);
           });