web3.js/test/batch.js

203 lines
7.2 KiB
JavaScript
Raw Normal View History

2015-05-12 05:01:33 +00:00
var chai = require('chai');
var assert = chai.assert;
2015-10-08 02:09:51 +00:00
var Web3 = require('../index');
var web3 = new Web3();
2015-05-12 05:01:33 +00:00
var FakeHttpProvider = require('./helpers/FakeHttpProvider');
var bn = require('bignumber.js');
2015-05-12 05:01:33 +00:00
describe('lib/web3/batch', function () {
describe('execute', function () {
it('should execute batch request', function (done) {
var provider = new FakeHttpProvider();
web3.setProvider(provider);
web3.reset();
var result = '0x126';
var result2 = '0x127';
provider.injectBatchResults([result, result2]);
var counter = 0;
var callback = function (err, r) {
counter++;
assert.deepEqual(new bn(result), r);
2015-05-12 05:01:33 +00:00
};
var callback2 = function (err, r) {
assert.equal(counter, 1);
assert.deepEqual(new bn(result2), r);
2015-05-12 05:01:33 +00:00
done();
};
2015-06-30 11:01:30 +00:00
provider.injectValidation(function (payload) {
var first = payload[0];
var second = payload[1];
assert.equal(first.method, 'eth_getBalance');
assert.deepEqual(first.params, ['0x0000000000000000000000000000000000000000', 'latest']);
assert.equal(second.method, 'eth_getBalance');
assert.deepEqual(second.params, ['0x0000000000000000000000000000000000000005', 'latest']);
});
2015-05-12 05:01:33 +00:00
var batch = web3.createBatch();
batch.add(web3.eth.getBalance.request('0x0000000000000000000000000000000000000000', 'latest', callback));
batch.add(web3.eth.getBalance.request('0x0000000000000000000000000000000000000005', 'latest', callback2));
batch.execute();
});
2015-07-15 15:58:23 +00:00
it('should execute batch request for async properties', function (done) {
var provider = new FakeHttpProvider();
web3.setProvider(provider);
web3.reset();
var result = [];
var result2 = '0xb';
provider.injectBatchResults([result, result2]);
var counter = 0;
var callback = function (err, r) {
counter++;
assert.isArray(result, r);
};
var callback2 = function (err, r) {
assert.equal(counter, 1);
assert.equal(r, 11);
done();
};
provider.injectValidation(function (payload) {
var first = payload[0];
var second = payload[1];
assert.equal(first.method, 'eth_accounts');
assert.deepEqual(first.params, []);
assert.equal(second.method, 'net_peerCount');
assert.deepEqual(second.params, []);
});
var batch = web3.createBatch();
batch.add(web3.eth.getAccounts.request(callback));
batch.add(web3.net.getPeerCount.request(callback2));
batch.execute();
});
it('should execute batch request with contract', function (done) {
var provider = new FakeHttpProvider();
web3.setProvider(provider);
web3.reset();
var abi = [{
"name": "balance(address)",
"type": "function",
"inputs": [{
"name": "who",
"type": "address"
}],
"constant": true,
"outputs": [{
"name": "value",
"type": "uint256"
}]
}];
2015-06-30 11:01:30 +00:00
var address = '0x1000000000000000000000000000000000000001';
var result = '0x126';
var result2 = '0x0000000000000000000000000000000000000000000000000000000000000123';
var counter = 0;
var callback = function (err, r) {
counter++;
assert.deepEqual(new bn(result), r);
};
var callback2 = function (err, r) {
assert.equal(counter, 1);
assert.deepEqual(new bn(result2), r);
done();
};
2015-06-30 11:01:30 +00:00
provider.injectValidation(function (payload) {
var first = payload[0];
var second = payload[1];
assert.equal(first.method, 'eth_getBalance');
assert.deepEqual(first.params, ['0x0000000000000000000000000000000000000000', 'latest']);
assert.equal(second.method, 'eth_call');
assert.deepEqual(second.params, [{
'to': '0x1000000000000000000000000000000000000001',
'data': '0xe3d670d70000000000000000000000001000000000000000000000000000000000000001'
}]);
});
var batch = web3.createBatch();
batch.add(web3.eth.getBalance.request('0x0000000000000000000000000000000000000000', 'latest', callback));
batch.add(web3.eth.contract(abi).at(address).balance.request(address, callback2));
provider.injectBatchResults([result, result2]);
batch.execute();
});
2015-07-15 15:49:55 +00:00
it('should execute batch requests and receive errors', function (done) {
var provider = new FakeHttpProvider();
web3.setProvider(provider);
web3.reset();
var abi = [{
"name": "balance(address)",
"type": "function",
"inputs": [{
"name": "who",
"type": "address"
}],
"constant": true,
"outputs": [{
"name": "value",
"type": "uint256"
}]
}];
var address = '0x1000000000000000000000000000000000000001';
var result = 'Something went wrong';
var result2 = 'Something went wrong 2';
var counter = 0;
var callback = function (err, r) {
counter++;
assert.isNotNull(err);
};
var callback2 = function (err, r) {
assert.equal(counter, 1);
assert.isNotNull(err);
done();
};
provider.injectValidation(function (payload) {
var first = payload[0];
var second = payload[1];
assert.equal(first.method, 'eth_getBalance');
assert.deepEqual(first.params, ['0x0000000000000000000000000000000000000000', 'latest']);
assert.equal(second.method, 'eth_call');
assert.deepEqual(second.params, [{
'to': '0x1000000000000000000000000000000000000001',
'data': '0xe3d670d70000000000000000000000001000000000000000000000000000000000000001'
}]);
});
var batch = web3.createBatch();
batch.add(web3.eth.getBalance.request('0x0000000000000000000000000000000000000000', 'latest', callback));
batch.add(web3.eth.contract(abi).at(address).balance.request(address, callback2));
provider.injectBatchResults([result, result2], true); // injects error
batch.execute();
});
2015-05-12 05:01:33 +00:00
});
});