Merge pull request #753 from yemel/performance/index-discovery-test

Small test refactor for performance
This commit is contained in:
Ryan X. Charles 2014-06-25 10:54:53 -07:00
commit a5e7b54d19
1 changed files with 74 additions and 68 deletions

View File

@ -331,6 +331,7 @@ describe('Wallet model', function() {
var w = cachedCreateW2(); var w = cachedCreateW2();
var spy = sinon.spy(w, 'scheduleConnect'); var spy = sinon.spy(w, 'scheduleConnect');
var callCount = 3; var callCount = 3;
w.reconnectDelay = 25;
w.netStart(); w.netStart();
setTimeout(function() { setTimeout(function() {
sinon.assert.callCount(spy, callCount); sinon.assert.callCount(spy, callCount);
@ -660,88 +661,93 @@ describe('Wallet model', function() {
}); });
}); });
var mockFakeActivity = function(w, f) {
var ADDRESSES_CHANGE = w.deriveAddresses(0, 20, true); describe('#indexDiscovery', function() {
var ADDRESSES_RECEIVE = w.deriveAddresses(0, 20, false); var ADDRESSES_CHANGE, ADDRESSES_RECEIVE, w;
w.blockchain.checkActivity = function(addresses, cb) {
var activity = new Array(addresses.length); before(function() {
for (var i = 0; i < addresses.length; i++) { w = cachedCreateW2();
var a1 = ADDRESSES_CHANGE.indexOf(addresses[i]); ADDRESSES_CHANGE = w.deriveAddresses(0, 20, true);
var a2 = ADDRESSES_RECEIVE.indexOf(addresses[i]); ADDRESSES_RECEIVE = w.deriveAddresses(0, 20, false);
activity[i] = f(Math.max(a1, a2)); });
var mockFakeActivity = function(f) {
w.blockchain.checkActivity = function(addresses, cb) {
var activity = new Array(addresses.length);
for (var i = 0; i < addresses.length; i++) {
var a1 = ADDRESSES_CHANGE.indexOf(addresses[i]);
var a2 = ADDRESSES_RECEIVE.indexOf(addresses[i]);
activity[i] = f(Math.max(a1, a2));
}
cb(null, activity);
} }
cb(null, activity);
} }
}
it('#indexDiscovery should work without found activities', function(done) { it('#indexDiscovery should work without found activities', function(done) {
var w = cachedCreateW2(); mockFakeActivity(function(index) {
mockFakeActivity(w, function(index) { return false;
return false });
w.indexDiscovery(0, false, 5, function(e, lastActive) {
lastActive.should.equal(-1);
done();
});
}); });
w.indexDiscovery(0, false, 5, function(e, lastActive) {
lastActive.should.equal(-1);
done();
});
});
it('#indexDiscovery should continue scanning', function(done) { it('#indexDiscovery should continue scanning', function(done) {
var w = cachedCreateW2(); mockFakeActivity(function(index) {
mockFakeActivity(w, function(index) { return index <= 7;
return index <= 7 });
w.indexDiscovery(0, false, 5, function(e, lastActive) {
lastActive.should.equal(7);
done();
});
}); });
w.indexDiscovery(0, false, 5, function(e, lastActive) {
lastActive.should.equal(7);
done();
});
});
it('#indexDiscovery should not found beyond the scannWindow', function(done) { it('#indexDiscovery should not found beyond the scannWindow', function(done) {
var w = cachedCreateW2(); mockFakeActivity(function(index) {
mockFakeActivity(w, function(index) { return index <= 10 || index == 17;
return index <= 10 || index == 17 });
w.indexDiscovery(0, false, 5, function(e, lastActive) {
lastActive.should.equal(10);
done();
});
}); });
w.indexDiscovery(0, false, 5, function(e, lastActive) {
lastActive.should.equal(10);
done();
});
});
it('#indexDiscovery should look for activity along the scannWindow', function(done) { it('#indexDiscovery should look for activity along the scannWindow', function(done) {
var w = cachedCreateW2(); mockFakeActivity(function(index) {
mockFakeActivity(w, function(index) { return index <= 14 && index % 2 == 0;
return index <= 14 && index % 2 == 0 });
w.indexDiscovery(0, false, 5, function(e, lastActive) {
lastActive.should.equal(14);
done();
});
}); });
w.indexDiscovery(0, false, 5, function(e, lastActive) {
lastActive.should.equal(14);
done();
});
});
it('#updateIndexes should update correctly', function(done) { it('#updateIndexes should update correctly', function(done) {
var w = cachedCreateW2(); mockFakeActivity(function(index) {
mockFakeActivity(w, function(index) { return index <= 14 && index % 2 == 0;
return index <= 14 && index % 2 == 0 });
});
w.updateIndexes(function(err) {
w.publicKeyRing.indexes.receiveIndex.should.equal(15);
w.publicKeyRing.indexes.changeIndex.should.equal(15);
done();
});
});
it('#updateIndexes should store and emit event', function(done) { w.updateIndexes(function(err) {
var w = cachedCreateW2(); w.publicKeyRing.indexes.receiveIndex.should.equal(15);
mockFakeActivity(w, function(index) { w.publicKeyRing.indexes.changeIndex.should.equal(15);
return index <= 14 && index % 2 == 0 done();
});
}); });
var spyStore = sinon.spy(w, 'store');
var spyEmit = sinon.spy(w, 'emit'); it('#updateIndexes should store and emit event', function(done) {
w.updateIndexes(function(err) { mockFakeActivity(function(index) {
sinon.assert.callCount(spyStore, 1); return index <= 14 && index % 2 == 0;
sinon.assert.callCount(spyEmit, 1); });
done(); var spyStore = sinon.spy(w, 'store');
var spyEmit = sinon.spy(w, 'emit');
w.updateIndexes(function(err) {
sinon.assert.callCount(spyStore, 1);
sinon.assert.callCount(spyEmit, 1);
done();
});
}); });
}); });
it('#deriveAddresses', function(done) { it('#deriveAddresses', function(done) {