-
Notifications
You must be signed in to change notification settings - Fork 5
/
tests.js
105 lines (99 loc) · 3.38 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* global describe it */
/* eslint import/no-extraneous-dependencies: 0 */
/* eslint no-console: 0 */
/* eslint no-unused-expressions: 0 */
const chai = require('chai');
const RpcClient = require('./index');
const async = require('async');
const expect = chai.expect;
const client = new RpcClient({
prot: 'http',
host: process.env.BITCOIND_HOST || 'localhost',
port: process.env.BITCOIND_PORT || 18332,
user: process.env.BITCOIND_USER,
pass: process.env.BITCOIND_PASS,
cookie: process.env.BITCOIND_COOKIE,
});
function checkBitcoind(mcb) {
let waitForBitcoind = true;
let connFailurePrint = true;
let syncPrint = true;
let error = null;
const expiry = new Date().getTime() + 1000;
async.whilst(
() => waitForBitcoind && expiry > new Date().getTime(),
(cb) => {
client.getBlockCount((err, gbt) => {
error = err;
if (err) {
if (!gbt || (err.code && err.code === -9)) {
// ECONNREFUSED, err === {} && !gbt
// err.code === -9 for a split second during bitcoind startup
if (connFailurePrint) {
if (err.message === 'bitcoin JSON-RPC connection rejected: 401 unauthorized') {
console.log('bitcoin JSON-RPC connection rejected: 401 unauthorized; the ' +
'username/password combination is invalid. Please set the BITCOIND_USER and ' +
'BITCOIND_PASS environment variables and try again.');
waitForBitcoind = false;
cb();
return;
}
connFailurePrint = false;
}
setTimeout(cb, 200);
} else if (err.code && err.code === -10) {
// getBlockTemplate returns error code -10 while "Bitcoin is downloading blocks..."
if (syncPrint) {
console.log('bitcoind is syncing blocks ... waiting for completion');
syncPrint = false;
}
setTimeout(cb, 1000);
} else {
// FATAL: unknown other error
console.log('unknown bitcoind error; make sure BITCOIND_HOST and BITCOIND_PORT ' +
'environment variables are set; e.g.\n' +
' BITCOIND_HOST=localhost BITCOIND_PORT=20003 npm test');
console.log(`error = ${JSON.stringify(err)}`);
waitForBitcoind = false;
cb();
}
} else {
waitForBitcoind = false;
cb();
}
});
},
() => {
if (waitForBitcoind) {
// expired
console.log('timeout waiting for bitcoind; make sure BITCOIND_HOST and BITCOIND_PORT ' +
'environment variables are set; e.g.\n' +
' BITCOIND_HOST=localhost BITCOIND_PORT=20003 npm test');
error = new Error('timeout waiting for bitcoind; ensure BITCOIND_HOST and BITCOIND_PORT ' +
'environment variables are set; e.g.\n' +
' BITCOIND_HOST=localhost BITCOIND_PORT=20003 npm test');
}
if (error) {
console.log('bitcoind error');
}
mcb(error);
}
);
}
describe('BitcoinD', () => {
it('is running', (done) => {
checkBitcoind((err) => {
expect(err).to.be.null;
done();
});
});
});
describe('bcrpc', () => {
it('can get info', (done) => {
client.getConnectionCount((err, info) => {
expect(err).to.be.null;
expect(info).to.not.be.null;
done();
});
});
});