Skip to content

Commit

Permalink
Mock classes for testing
Browse files Browse the repository at this point in the history
- Added mockup classes for use in testing dependants
- Update classes for easier mockup
- Added api request events that allow setting result, error, or cancelling request
- Fixed comments
- Update tests
  • Loading branch information
JCThePants committed May 22, 2020
1 parent eb8b687 commit 8d51427
Show file tree
Hide file tree
Showing 11 changed files with 2,584 additions and 44 deletions.
64 changes: 53 additions & 11 deletions libs/class.BeamExplorerClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,18 @@ class BeamExplorerClient extends EventEmitter {
*/
static get EVENT_SOCKET_ERROR() { return 'socketError' }


/**
* The name of the event emitted when the API server responds with a status code other than 200.
* @returns {string}
*/
static get EVENT_API_ERROR() { return 'apiError' }

/**
* The name of the event emitted when an api request is made.
* @returns {string}
*/
static get EVENT_API_REQUEST() { return 'apiRequest'}


/**
* Get the host value.
Expand Down Expand Up @@ -98,7 +103,7 @@ class BeamExplorerClient extends EventEmitter {
const _ = this;
const callback = args.callback;

_._get(`status`, _.$createConnectArgs('getStatus', args), callback);
_.$get(`status`, _.$createConnectArgs('getStatus', args), callback);
}


Expand Down Expand Up @@ -140,15 +145,15 @@ class BeamExplorerClient extends EventEmitter {
const id = args.id;
const callback = args.callback;

_._get(`block?hash=${id}`, _.$createConnectArgs('getBlock', args), callback);
_.$get(`block?hash=${id}`, _.$createConnectArgs('getBlock', args), callback);
}


/**
* Get block info by block height.
*
* @param args
* @param args.id {string}
* @param args.height {number}
* @param args.callback {function(err:*, { found: boolean, height: number }|{
* chainwork: string,
* difficulty: number,
Expand Down Expand Up @@ -182,7 +187,7 @@ class BeamExplorerClient extends EventEmitter {
const height = args.height;
const callback = args.callback;

_._get(`block?height=${height}`, _.$createConnectArgs('getBlockAt', args), callback);
_.$get(`block?height=${height}`, _.$createConnectArgs('getBlockAt', args), callback);
}


Expand Down Expand Up @@ -224,7 +229,7 @@ class BeamExplorerClient extends EventEmitter {
const id = args.id;
const callback = args.callback;

_._get(`block?kernel=${id}`, _.$createConnectArgs('getBlockByKernel', args), callback);
_.$get(`block?kernel=${id}`, _.$createConnectArgs('getBlockByKernel', args), callback);
}


Expand Down Expand Up @@ -269,7 +274,7 @@ class BeamExplorerClient extends EventEmitter {
const count = args.count;
const callback = args.callback;

_._get(`blocks?height=${height}&n=${count}`, _.$createConnectArgs('getBlocks', args), callback);
_.$get(`blocks?height=${height}&n=${count}`, _.$createConnectArgs('getBlocks', args), callback);
}


Expand Down Expand Up @@ -342,7 +347,7 @@ class BeamExplorerClient extends EventEmitter {
if (shouldRetry) {
connectArgs.retryCount++;
setTimeout(() => {
_._get(path, connectArgs, callback);
_.$get(path, connectArgs, callback);
}, retryDelayMs);
}
else {
Expand Down Expand Up @@ -382,7 +387,7 @@ class BeamExplorerClient extends EventEmitter {
if (shouldRetry) {
connectArgs.retryCount++;
setTimeout(() => {
_._get(path, connectArgs, callback);
_.$get(path, connectArgs, callback);
}, retryDelayMs);
}
else {
Expand All @@ -391,11 +396,48 @@ class BeamExplorerClient extends EventEmitter {
}


_get(path, connectArgs, callback) {
$get(path, connectArgs, callback) {
const _ = this;
const isSecure = connectArgs.isSecure;
const options = _.$createHttpOptions(connectArgs, path);

let localError = null;
let localResult = null;
let shouldCancel = false;

_.emit(BeamExplorerClient.EVENT_API_REQUEST, {
get path() { return path },
set path(p) {
precon.string(p, 'path');
path = p;
},
get connectArgs() { return connectArgs },
setError(error) {
localError = error;
},
setResult(result) {
localResult = result;
},
cancel() {
shouldCancel = true;
}
});

if (shouldCancel) {
callback(new Error(`Explorer API request cancelled: ${path}`));
return;
}

if (localError) {
_.$handleSocketError(localError, path, connectArgs, callback);
return;
}

if (localResult) {
callback(null, localResult);
return;
}

const req = (isSecure ? https : http).request(options, (res) => {

if (res.statusCode !== 200) {
Expand All @@ -407,7 +449,7 @@ class BeamExplorerClient extends EventEmitter {
const buffer = new JsonBuffer();
const messagesArr = [];
const timeout = setTimeout(() => {
callback && callback(new Error(`Timed out: ${path}`));
callback && callback(new Error(`Explorer API request timed out: ${path}`));
callback = null;
}, 20000);

Expand Down
16 changes: 8 additions & 8 deletions libs/class.BeamMiningClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class BeamMiningClient extends EventEmitter {
_._isConnected = true;
_._connectArgs = _.$createConnectArgs();

_._connect(_._connectArgs, callback);
_.$connect(_._connectArgs, callback);
}


Expand Down Expand Up @@ -213,7 +213,7 @@ class BeamMiningClient extends EventEmitter {
return;
}

_._send({
_.$send({
id: beamJobIdHex,
method: 'solution',
nonce: nonceHex,
Expand Down Expand Up @@ -325,7 +325,7 @@ class BeamMiningClient extends EventEmitter {
_._connectArgs.reconnectCount++;
_._isConnected = true;
_._connectTimeout = setTimeout(() => {
_._connect(_._connectArgs, err => {
_.$connect(_._connectArgs, err => {
reconnectCallbackArr.forEach(callback => { callback(err) });
});
}, retryDelayMs);
Expand All @@ -342,7 +342,7 @@ class BeamMiningClient extends EventEmitter {
}


_connect(connectArgs, callback) {
$connect(connectArgs, callback) {

const _ = this;

Expand All @@ -352,7 +352,7 @@ class BeamMiningClient extends EventEmitter {

netSocket.off('error', onConnectError);

_._login(connectArgs.apiKey, (err) => {
_.$login(connectArgs.apiKey, (err) => {

if (!err)
connectArgs.reconnectCount = 0;
Expand All @@ -376,10 +376,10 @@ class BeamMiningClient extends EventEmitter {
}


_login(apiKey, callback) {
$login(apiKey, callback) {

const _ = this;
_._send({
_.$send({
id: 'login',
method: 'login',
api_key: apiKey,
Expand All @@ -395,7 +395,7 @@ class BeamMiningClient extends EventEmitter {
}


_send(data, callback) {
$send(data, callback) {
precon.funct(callback);
const _ = this;
_._awaitResult(data.id, callback);
Expand Down
Loading

0 comments on commit 8d51427

Please sign in to comment.