Skip to content

Commit

Permalink
Fix balance monitor api tests (#4676)
Browse files Browse the repository at this point in the history
* Fix balance monitor api tests to handle the fact that balnces are now 'live' and not tied to the REST API /balances
timestamp.

Signed-off-by: Marc Kriguer <marc.kriguer@swirldslabs.com>
  • Loading branch information
MarcKriguerAtHedera authored Oct 18, 2022
1 parent 9829d17 commit 3196337
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 52 deletions.
57 changes: 7 additions & 50 deletions hedera-mirror-rest/monitoring/monitor_apis/balance_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/

import _ from 'lodash';
import * as math from 'mathjs';
import config from './config';

import {
Expand Down Expand Up @@ -73,48 +72,6 @@ const getBalancesCheck = async (server) => {
};
};

/**
* Verify balances call with time and limit query params provided
* @param {String} server API host endpoint
*/
const getBalancesWithTimeAndLimitParams = async (server) => {
let url = getUrl(server, balancesPath, {limit: 1});
const resp = await getAPIResponse(url);
let balances = resp instanceof Error ? resp : resp.balances;

const checkRunner = new CheckRunner()
.withCheckSpec(checkAPIResponseError)
.withCheckSpec(checkRespObjDefined, {message: 'balances is undefined'})
.withCheckSpec(checkRespArrayLength, {
limit: 1,
message: (elements) => `balances.length of ${elements.length} was expected to be 1`,
});
let result = checkRunner.run(balances);
if (!result.passed) {
return {url, ...result};
}

const {timestamp} = resp;
const plusOne = math.add(math.bignumber(timestamp), math.bignumber(1));
const minusOne = math.subtract(math.bignumber(timestamp), math.bignumber(1));
url = getUrl(server, balancesPath, {
timestamp: [`gt:${minusOne.toString()}`, `lt:${plusOne.toString()}`],
limit: 1,
});
balances = await getAPIResponse(url, jsonRespKey);

result = checkRunner.run(balances);
if (!result.passed) {
return {url, ...result};
}

return {
url,
passed: true,
message: 'Successfully called balances with time and limit params',
};
};

/**
* Verify single balance can be retrieved
* @param {String} server API host endpoint
Expand Down Expand Up @@ -168,7 +125,12 @@ const getSingleBalanceById = async (server) => {
* @param {String} server API host endpoint
*/
const checkBalanceFreshness = async (server) => {
return checkResourceFreshness(server, balancesPath, resource, (data) => data.timestamp);
const now = new Date().getTime() / 1000;
return checkResourceFreshness(server, balancesPath, resource, (data) => data.timestamp, undefined, {
timestamp: now,
limit: 1,
order: 'desc',
});
};

/**
Expand All @@ -179,12 +141,7 @@ const checkBalanceFreshness = async (server) => {
*/
const runTests = async (server, testResult) => {
const runTest = testRunner(server, testResult, resource);
return Promise.all([
runTest(getBalancesCheck),
runTest(getBalancesWithTimeAndLimitParams),
runTest(getSingleBalanceById),
runTest(checkBalanceFreshness),
]);
return Promise.all([runTest(getBalancesCheck), runTest(getSingleBalanceById), runTest(checkBalanceFreshness)]);
};

export default {
Expand Down
11 changes: 9 additions & 2 deletions hedera-mirror-rest/monitoring/monitor_apis/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,20 @@ const checkElementsOrder = (elements, option) => {
* @param jsonRespKey json response key to extract data from json response
* @return {Promise<>}
*/
const checkResourceFreshness = async (server, path, resource, timestamp, jsonRespKey) => {
const checkResourceFreshness = async (
server,
path,
resource,
timestamp,
jsonRespKey,
query = {limit: 1, order: 'desc'}
) => {
const {freshnessThreshold} = config[resource];
if (freshnessThreshold === 0) {
return {skipped: true};
}

const url = getUrl(server, path, {limit: 1, order: 'desc'});
const url = getUrl(server, path, query);
const resp = await getAPIResponse(url, jsonRespKey);

const checkRunner = new CheckRunner()
Expand Down

0 comments on commit 3196337

Please sign in to comment.