Skip to content

Commit b363873

Browse files
authored
Merge pull request #556 from embark-framework/bug_fix/hanging-test-node
Fix hanging tests when using a wrong node
2 parents 287998d + 1a91f3c commit b363873

File tree

3 files changed

+68
-46
lines changed

3 files changed

+68
-46
lines changed

lib/contracts/blockchain.js

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -256,45 +256,8 @@ class Blockchain {
256256
if (!self.contractsConfig || !self.contractsConfig.deployment || !self.contractsConfig.deployment.host) {
257257
return next();
258258
}
259-
const origin = self.blockchainConfig.wsOrigins.split(',')[0];
260-
const options = {
261-
protocolVersion: 13,
262-
perMessageDeflate: true,
263-
origin: origin,
264-
host: self.contractsConfig.deployment.host,
265-
port: self.contractsConfig.deployment.port
266-
};
267-
if (self.contractsConfig.deployment.type === 'ws') {
268-
options.headers = {
269-
'Sec-WebSocket-Version': 13,
270-
Connection: 'Upgrade',
271-
Upgrade: 'websocket',
272-
'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits',
273-
Origin: origin
274-
};
275-
}
276-
let req;
277-
// remove trailing api key from infura, ie rinkeby.infura.io/nmY8WtT4QfEwz2S7wTbl
278-
if(options.host.indexOf('/') > -1){
279-
options.host = options.host.split('/')[0];
280-
}
281-
if((self.contractsConfig.deployment.protocol || 'http') === 'https'){
282-
req = require('https').get(options);
283-
}else{
284-
req = require('http').get(options);
285-
}
286-
287-
req.on('error', (err) => {
288-
next(err);
289-
});
290-
291-
req.on('response', (_response) => {
292-
next();
293-
});
294-
295-
req.on('upgrade', (_res, _socket, _head) => {
296-
next();
297-
});
259+
const {host, port, type, protocol} = self.contractsConfig.deployment;
260+
utils.pingEndpoint(host, port, type, protocol, self.blockchainConfig.wsOrigins.split(',')[0], next);
298261
}
299262
], function (err) {
300263
if (!noLogs && err === NO_NODE_ERROR) {

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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,47 @@ function httpsGetJson(url, callback) {
7373
});
7474
}
7575

76+
function pingEndpoint(host, port, type, protocol, origin, callback) {
77+
const options = {
78+
protocolVersion: 13,
79+
perMessageDeflate: true,
80+
origin: origin,
81+
host: host,
82+
port: port
83+
};
84+
if (type === 'ws') {
85+
options.headers = {
86+
'Sec-WebSocket-Version': 13,
87+
Connection: 'Upgrade',
88+
Upgrade: 'websocket',
89+
'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits',
90+
Origin: origin
91+
};
92+
}
93+
let req;
94+
// remove trailing api key from infura, ie rinkeby.infura.io/nmY8WtT4QfEwz2S7wTbl
95+
if (options.host.indexOf('/') > -1){
96+
options.host = options.host.split('/')[0];
97+
}
98+
if (protocol === 'https') {
99+
req = require('https').get(options);
100+
} else {
101+
req = require('http').get(options);
102+
}
103+
104+
req.on('error', (err) => {
105+
callback(err);
106+
});
107+
108+
req.on('response', (_response) => {
109+
callback();
110+
});
111+
112+
req.on('upgrade', (_res, _socket, _head) => {
113+
callback();
114+
});
115+
}
116+
76117
function runCmd(cmd, options) {
77118
const shelljs = require('shelljs');
78119
let result = shelljs.exec(cmd, options || {silent: true});
@@ -267,6 +308,7 @@ module.exports = {
267308
httpGetJson: httpGetJson,
268309
httpsGetJson: httpsGetJson,
269310
hexToNumber: hexToNumber,
311+
pingEndpoint,
270312
decodeParams: decodeParams,
271313
runCmd: runCmd,
272314
cd: cd,

0 commit comments

Comments
 (0)