Added db tests.
This commit is contained in:
parent
2bd769cce0
commit
2bf06956e1
@ -122,8 +122,8 @@ Node.prototype._startService = function(serviceInfo, callback) {
|
||||
var config;
|
||||
if (serviceInfo.config) {
|
||||
assert(_.isObject(serviceInfo.config));
|
||||
assert(serviceInfo.config.node);
|
||||
assert(serviceInfo.config.name);
|
||||
assert(!serviceInfo.config.node);
|
||||
assert(!serviceInfo.config.name);
|
||||
config = serviceInfo.config;
|
||||
} else {
|
||||
config = {};
|
||||
@ -190,6 +190,7 @@ Node.prototype.start = function(callback) {
|
||||
self._startService(service, next);
|
||||
},
|
||||
function(err) {
|
||||
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
@ -54,11 +54,11 @@ DB.prototype._onError = function(err) {
|
||||
DB.prototype._setDataPath = function() {
|
||||
assert(fs.existsSync(this.node.datadir), 'Node is expected to have a "datadir" property');
|
||||
if (this.node.network === 'livenet' || this.node.network === 'mainnet') {
|
||||
this.dataPath = this.node.datadir + '/bitcore-node.db';
|
||||
this.dataPath = this.node.datadir + '/bitcorenode.db';
|
||||
} else if (this.node.network === 'regtest') {
|
||||
this.dataPath = this.node.datadir + '/regtest/bitcore-node.db';
|
||||
this.dataPath = this.node.datadir + '/regtest/bitcorenode.db';
|
||||
} else if (this.node.network === 'testnet') {
|
||||
this.dataPath = this.node.datadir + '/testnet/bitcore-node.db';
|
||||
this.dataPath = this.node.datadir + '/testnet/bitcorenode.db';
|
||||
} else {
|
||||
throw new Error('Unknown network: ' + this.network);
|
||||
}
|
||||
@ -139,7 +139,7 @@ DB.prototype.batch = function(ops, callback) {
|
||||
var self = this;
|
||||
|
||||
if (self._stopping) {
|
||||
return;
|
||||
return callback();
|
||||
}
|
||||
|
||||
for(var i = 0; i < ops.length; i++) {
|
||||
@ -158,24 +158,20 @@ DB.prototype.batch = function(ops, callback) {
|
||||
};
|
||||
|
||||
DB.prototype.createReadStream = function(op) {
|
||||
var stream;
|
||||
if (!this._stopping) {
|
||||
stream = this._store.createReadStream(op);
|
||||
}
|
||||
if (stream) {
|
||||
stream.on('error', this._onError.bind(this));
|
||||
if (this._stopping) {
|
||||
return;
|
||||
}
|
||||
var stream = this._store.createReadStream(op);
|
||||
stream.on('error', this._onError.bind(this));
|
||||
return stream;
|
||||
};
|
||||
|
||||
DB.prototype.createKeyStream = function(op) {
|
||||
var stream;
|
||||
if (!this._stopping) {
|
||||
stream = this._store.createKeyStream(op);
|
||||
}
|
||||
if (stream) {
|
||||
stream.on('error', this.onError.bind(this));
|
||||
if (this._stopping) {
|
||||
return;
|
||||
}
|
||||
var stream = this._store.createKeyStream(op);
|
||||
stream.on('error', this._onError.bind(this));
|
||||
return stream;
|
||||
};
|
||||
|
||||
@ -187,6 +183,7 @@ DB.prototype.stop = function(callback) {
|
||||
DB.prototype.close = function(callback) {
|
||||
if (this._store && this._store.isOpen()) {
|
||||
this._store.close(callback);
|
||||
return;
|
||||
}
|
||||
setImmediate(callback);
|
||||
};
|
||||
|
||||
@ -191,7 +191,6 @@ P2P.prototype._getBestHeight = function() {
|
||||
|
||||
// we should only choose from a list of peers that sync'ed
|
||||
P2P.prototype._getPeer = function() {
|
||||
|
||||
return this._peer;
|
||||
};
|
||||
|
||||
@ -222,10 +221,9 @@ P2P.prototype._initPool = function() {
|
||||
if (this._configPeers) {
|
||||
opts.addrs = this._configPeers;
|
||||
}
|
||||
// TODO: bcoin stuff goes here
|
||||
opts.dnsSeed = false;
|
||||
opts.maxPeers = this._maxPeers;
|
||||
opts.network = this.node.getNetworkName();
|
||||
opts.network = this.node.network;
|
||||
this._pool = new p2p.Pool(opts);
|
||||
};
|
||||
|
||||
|
||||
@ -7,10 +7,12 @@ var expect = chai.expect;
|
||||
var DBService = require('../../../lib/services/db');
|
||||
var sinon = require('sinon');
|
||||
var Levelup = require('levelup');
|
||||
var Tx = require('bcoin').tx;
|
||||
|
||||
describe('DB', function() {
|
||||
|
||||
var dbService;
|
||||
var tx = Tx.fromRaw( '0100000004de9b4bb17f627096a9ee0b4528e4eae17df5b5c69edc29704c2e84a7371db29f010000006b483045022100f5b1a0d33b7be291c3953c25f8ae39d98601aa7099a8674daf638a08b86c7173022006ce372da5ad088a1cc6e5c49c2760a1b6f085eb1b51b502211b6bc9508661f9012102ec5e3731e54475dd2902326f43602a03ae3d62753324139163f81f20e787514cffffffff7a1d4e5fc2b8177ec738cd723a16cf2bf493791e55573445fc0df630fe5e2d64010000006b483045022100cf97f6cb8f126703e9768545dfb20ffb10ba78ae3d101aa46775f5a239b075fc02203150c4a89a11eaf5e404f4f96b62efa4455e9525765a025525c7105a7e47b6db012102c01e11b1d331f999bbdb83e8831de503cd52a01e3834a95ccafd615c67703d77ffffffff9e52447116415ca0d0567418a1a4ef8f27be3ff5a96bf87c922f3723d7db5d7c000000006b483045022100f6c117e536701be41a6b0b544d7c3b1091301e4e64a6265b6eb167b15d16959d022076916de4b115e700964194ce36a24cb9105f86482f4abbc63110c3f537cd5770012102ddf84cc7bee2d6a82ac09628a8ad4a26cd449fc528b81e7e6cc615707b8169dfffffffff5815d9750eb3572e30d6fd9df7afb4dbd76e042f3aa4988ac763b3fdf8397f80010000006a473044022028f4402b736066d93d2a32b28ccd3b7a21d84bb58fcd07fe392a611db94cdec5022018902ee0bf2c3c840c1b81ead4e6c87c88c48b2005bf5eea796464e561a620a8012102b6cdd1a6cd129ef796faeedb0b840fcd0ca00c57e16e38e46ee7028d59812ae7ffffffff0220a10700000000001976a914c342bcd1a7784d9842f7386b8b3b8a3d4171a06e88ac59611100000000001976a91449f8c749a9960dc29b5cbe7d2397cea7d26611bb88ac00000000', 'hex');
|
||||
|
||||
var sandbox;
|
||||
beforeEach(function() {
|
||||
@ -72,7 +74,7 @@ describe('DB', function() {
|
||||
|
||||
it('should set the data path', function() {
|
||||
dbService._setDataPath();
|
||||
dbService.dataPath.should.equal('/tmp/regtest/bitcore-node.db');
|
||||
dbService.dataPath.should.equal('/tmp/regtest/bitcorenode.db');
|
||||
});
|
||||
|
||||
});
|
||||
@ -134,21 +136,189 @@ describe('DB', function() {
|
||||
});
|
||||
|
||||
describe('#batch', function() {
|
||||
|
||||
it('should save a batch of operations', function(done) {
|
||||
|
||||
var batch = sandbox.stub().callsArgWith(1, null);
|
||||
dbService._store = { batch: batch };
|
||||
|
||||
dbService.batch([], function(err) {
|
||||
|
||||
if(err) {
|
||||
return done(err);
|
||||
}
|
||||
|
||||
batch.callCount.should.equal(1);
|
||||
done();
|
||||
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
it('should not call batch whilst shutting down', function(done) {
|
||||
|
||||
dbService._stopping = true;
|
||||
|
||||
var batch = sandbox.stub().callsArgWith(1, null);
|
||||
dbService._store = { batch: batch };
|
||||
|
||||
dbService.batch(batch, function(err) {
|
||||
|
||||
if(err) {
|
||||
return done(err);
|
||||
}
|
||||
|
||||
batch.callCount.should.equal(0);
|
||||
done();
|
||||
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
describe('#createReadStream', function() {
|
||||
|
||||
it('should get a read stream', function() {
|
||||
|
||||
var on = sandbox.stub();
|
||||
var stream = { on: on };
|
||||
var createReadStream = sandbox.stub().returns(stream);
|
||||
dbService._store = { createReadStream: createReadStream };
|
||||
dbService.createReadStream([]).should.deep.equal(stream);
|
||||
createReadStream.callCount.should.equal(1);
|
||||
|
||||
});
|
||||
|
||||
it('should not get a read stream if the node is stopping', function() {
|
||||
|
||||
dbService._stopping = true;
|
||||
|
||||
var on = sandbox.stub();
|
||||
var stream = { on: on };
|
||||
var createReadStream = sandbox.stub().returns(stream);
|
||||
dbService._store = { createReadStream: createReadStream };
|
||||
var stream = dbService.createReadStream([]);
|
||||
expect(stream).to.be.undefined;
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#createKeyStream', function() {
|
||||
|
||||
it('should get a key stream', function() {
|
||||
|
||||
var on = sandbox.stub();
|
||||
var stream = { on: on };
|
||||
var createKeyStream = sandbox.stub().returns(stream);
|
||||
dbService._store = { createKeyStream: createKeyStream };
|
||||
dbService.createKeyStream([]).should.deep.equal(stream);
|
||||
createKeyStream.callCount.should.equal(1);
|
||||
|
||||
});
|
||||
|
||||
it('should not get a key stream if the node is stopping', function() {
|
||||
|
||||
dbService._stopping = true;
|
||||
|
||||
var on = sandbox.stub();
|
||||
var stream = { on: on };
|
||||
var createKeyStream = sandbox.stub().returns(stream);
|
||||
dbService._store = { createKeyStream: createKeyStream };
|
||||
var stream = dbService.createKeyStream([]);
|
||||
expect(stream).to.be.undefined;
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
describe('#close', function() {
|
||||
it('should close the store if there is a store and it is open', function(done) {
|
||||
|
||||
var close = sandbox.stub().callsArgWith(0, null);
|
||||
dbService._store = { isOpen: sinon.stub().returns(true), close: close };
|
||||
|
||||
dbService.close(function(err) {
|
||||
if(err) {
|
||||
return done(err);
|
||||
}
|
||||
close.callCount.should.equal(1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getServiceTip', function() {
|
||||
it('should get service tip for previously saved', function(done) {
|
||||
|
||||
var tipBuf = Buffer.concat([ new Buffer('deadbeef', 'hex'), new Buffer(tx.txid(), 'hex') ]);
|
||||
var get = sandbox.stub(dbService, 'get').callsArgWith(1, null, tipBuf);
|
||||
dbService.getServiceTip('test', function(err, tip) {
|
||||
|
||||
if(err) {
|
||||
return done(err);
|
||||
}
|
||||
|
||||
get.callCount.should.equal(1);
|
||||
tip.height.should.equal(0xdeadbeef);
|
||||
tip.hash.should.equal(tx.txid());
|
||||
done();
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
it('should get service tip for not previously saved', function(done) {
|
||||
|
||||
var get = sandbox.stub(dbService, 'get').callsArgWith(1, null, null);
|
||||
dbService.getServiceTip('test', function(err, tip) {
|
||||
|
||||
if(err) {
|
||||
return done(err);
|
||||
}
|
||||
|
||||
get.callCount.should.equal(1);
|
||||
tip.height.should.equal(0);
|
||||
tip.hash.should.equal('0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206');
|
||||
done();
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getPrefix', function() {
|
||||
|
||||
it('should get the db prefix for a service when one already exists', function(done) {
|
||||
var get = sandbox.stub(dbService, 'get').callsArgWith(1, null, new Buffer('0000', 'hex'));
|
||||
dbService.getPrefix('test', function(err, prefix) {
|
||||
|
||||
if(err) {
|
||||
return done(err);
|
||||
}
|
||||
|
||||
get.callCount.should.equal(1);
|
||||
prefix.toString('hex').should.equal('0000');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should get the db prefix for a service when one does not already exist', function(done) {
|
||||
var put = sandbox.stub(dbService, 'put').callsArgWith(2, null);
|
||||
var get = sandbox.stub(dbService, 'get');
|
||||
get.onCall(0).callsArgWith(1, null, null);
|
||||
get.onCall(1).callsArgWith(1, null, new Buffer('eeee', 'hex'));
|
||||
dbService.getPrefix('test', function(err, prefix) {
|
||||
|
||||
if(err) {
|
||||
return done(err);
|
||||
}
|
||||
|
||||
get.callCount.should.equal(2);
|
||||
put.callCount.should.equal(2);
|
||||
put.args[1][1].toString('hex').should.equal('eeef');
|
||||
prefix.toString('hex').should.equal('eeee');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user