Skip to content

Commit 1a91f3c

Browse files
committed
ping endpoint before connecting to see if available
1 parent aa02aeb commit 1a91f3c

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

lib/tests/test.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const Events = require('../core/events');
77
const cloneDeep = require('clone-deep');
88
const AccountParser = require('../contracts/accountParser');
99
const Provider = require('../contracts/provider');
10+
const utils = require('../utils/utils');
1011

1112
const EmbarkJS = require('../../js/embark_node');
1213

@@ -44,18 +45,29 @@ class Test {
4445

4546
initWeb3Provider(callback) {
4647
if (this.simOptions.host) {
47-
const protocol = (this.simOptions.type === "rpc") ? 'http' : 'ws';
48+
let {host, port, type, protocol, accounts} = this.simOptions;
49+
if (!protocol) {
50+
protocol = (this.simOptions.type === "rpc") ? 'http' : 'ws';
51+
}
52+
const endpoint = `${protocol}://${host}:${port}`;
4853
const providerOptions = {
4954
web3: this.web3,
50-
type: this.simOptions.type,
51-
accountsConfig: this.simOptions.accounts,
55+
type,
56+
accountsConfig: accounts,
5257
blockchainConfig: this.engine.config.blockchainConfig,
5358
logger: this.engine.logger,
5459
isDev: false,
55-
web3Endpoint: `${protocol}://${this.simOptions.host}:${this.simOptions.port}`
60+
web3Endpoint: endpoint
5661
};
57-
this.provider = new Provider(providerOptions);
58-
return this.provider.startWeb3Provider(callback);
62+
console.info(`Connecting to node at ${endpoint}`.cyan);
63+
return utils.pingEndpoint(host, port, type, protocol, this.engine.config.blockchainConfig.wsOrigins.split(',')[0], (err) => {
64+
if (err) {
65+
console.error(`Error connecting to the node, there might be an error in ${endpoint}`.red);
66+
return callback(err);
67+
}
68+
this.provider = new Provider(providerOptions);
69+
return this.provider.startWeb3Provider(callback);
70+
});
5971
}
6072

6173
if (this.simOptions.accounts) {
@@ -214,7 +226,12 @@ class Test {
214226
next(null, accounts);
215227
});
216228
}
217-
], callback);
229+
], (err, accounts) => {
230+
if (err) {
231+
process.exit(1);
232+
}
233+
callback(null, accounts);
234+
});
218235
}
219236

220237
_deploy(config, callback) {

lib/utils/utils.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ function httpsGetJson(url, callback) {
7373
});
7474
}
7575

76-
function pingEndpoint(host, port, type, protocol, wsOrigins, callback) {
77-
const origin = wsOrigins.split(',')[0];
76+
function pingEndpoint(host, port, type, protocol, origin, callback) {
7877
const options = {
7978
protocolVersion: 13,
8079
perMessageDeflate: true,
@@ -93,10 +92,10 @@ function pingEndpoint(host, port, type, protocol, wsOrigins, callback) {
9392
}
9493
let req;
9594
// remove trailing api key from infura, ie rinkeby.infura.io/nmY8WtT4QfEwz2S7wTbl
96-
if(options.host.indexOf('/') > -1){
95+
if (options.host.indexOf('/') > -1){
9796
options.host = options.host.split('/')[0];
9897
}
99-
if(protocol === 'https') {
98+
if (protocol === 'https') {
10099
req = require('https').get(options);
101100
} else {
102101
req = require('http').get(options);

0 commit comments

Comments
 (0)