Skip to content

Commit

Permalink
Refactor acceptance tests structure (#211)
Browse files Browse the repository at this point in the history
* refactor: overall refactor of acceptance structure

Signed-off-by: Ivo Yankov <ivo@devlabs.bg>

* fix: correctly load env for integration tests

Signed-off-by: Ivo Yankov <ivo@devlabs.bg>

* chore: test refactoring

Signed-off-by: Daniel Ivanov <daniel.k.ivanov95@gmail.com>

* chore: test refactoring

Signed-off-by: Daniel Ivanov <daniel.k.ivanov95@gmail.com>

* chore: enable part of the acceptance tests

Signed-off-by: Daniel Ivanov <daniel.k.ivanov95@gmail.com>

* chore: remove invalid child logger

Signed-off-by: Daniel Ivanov <daniel.k.ivanov95@gmail.com>

* chore: test-cicd

Signed-off-by: Daniel Ivanov <daniel.k.ivanov95@gmail.com>

* feat: partially enable acceptance tests

Signed-off-by: Daniel Ivanov <daniel.k.ivanov95@gmail.com>

* feat: fix more tests

Signed-off-by: Daniel Ivanov <daniel.k.ivanov95@gmail.com>

* chore: remove unused methods

Signed-off-by: Daniel Ivanov <daniel.k.ivanov95@gmail.com>

* chore: enable more tests

Signed-off-by: Daniel Ivanov <daniel.k.ivanov95@gmail.com>

* chore: enable more tests

Signed-off-by: Daniel Ivanov <daniel.k.ivanov95@gmail.com>

* feat: fix all tests

Signed-off-by: Daniel Ivanov <daniel.k.ivanov95@gmail.com>

* feat: resolve comments

Signed-off-by: Daniel Ivanov <daniel.k.ivanov95@gmail.com>

* feat: resolve last comment

Signed-off-by: Daniel Ivanov <daniel.k.ivanov95@gmail.com>

Co-authored-by: Daniel Ivanov <daniel.k.ivanov95@gmail.com>
  • Loading branch information
Ivo-Yankov and Daniel-K-Ivanov authored Jun 17, 2022
1 parent 94328ad commit 75625e1
Show file tree
Hide file tree
Showing 12 changed files with 1,091 additions and 1,004 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
]
},
"scripts": {
"acceptancetest": "ts-mocha ./packages/server/tests/acceptance.spec.ts --exit",
"acceptancetest": "ts-mocha packages/server/tests/acceptance/*.spec.ts --exit",
"build": "npx lerna run build",
"build-and-test": "npx lerna run build && npx lerna run test",
"build:docker": "docker build . -t ${npm_package_name}",
Expand Down
4 changes: 2 additions & 2 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
"build": "pnpm clean && pnpm compile",
"clean": "rm -rf ./dist && rm -rf tsconfig.tsbuildinfo",
"compile": "tsc -b tsconfig.json",
"acceptancetest": "ts-mocha ./tests/acceptance.spec.ts",
"acceptancetest": "ts-mocha tests/acceptance/*.spec.ts",
"start": "node dist/index.js",
"test": "nyc ts-mocha --recursive ./tests/server.spec.ts"
"test": "nyc ts-mocha --recursive ./tests/integration/server.spec.ts"
},
"nyc": {
"check-coverage": false,
Expand Down
566 changes: 0 additions & 566 deletions packages/server/tests/acceptance.spec.ts

This file was deleted.

504 changes: 504 additions & 0 deletions packages/server/tests/acceptance/rpc.spec.ts

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions packages/server/tests/clients/mirrorClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*-
*
* Hedera JSON RPC Relay
*
* Copyright (C) 2022 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import Axios, { AxiosInstance } from 'axios';
import axiosRetry from 'axios-retry';
import { Logger } from 'pino';

export default class MirrorClient {

private readonly logger: Logger;
private readonly client: AxiosInstance;

constructor(mirrorNodeUrl: string, logger: Logger) {
this.logger = logger;

const mirrorNodeClient = Axios.create({
baseURL: `${mirrorNodeUrl}/api/v1`,
responseType: 'json' as const,
headers: {
'Content-Type': 'application/json'
},
method: 'GET',
timeout: 5 * 1000
});

// allow retries given mirror node waits for consensus, record stream serialization, export and import before parsing and exposing
axiosRetry(mirrorNodeClient, {
retries: 5,
retryDelay: (retryCount) => {
this.logger.info(`Retry delay ${retryCount * 1000} s`);
return retryCount * 1000;
},
retryCondition: (error) => {
this.logger.error(error, `Request failed`);

// if retry condition is not specified, by default idempotent requests are retried
return error?.response?.status === 400 || error?.response?.status === 404;
}
});

this.client = mirrorNodeClient;
}

async get(path: string) {
this.logger.debug(`[GET] MirrorNode ${path} endpoint`);
return (await this.client.get(path)).data;
};

}
109 changes: 109 additions & 0 deletions packages/server/tests/clients/relayClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*-
*
* Hedera JSON RPC Relay
*
* Copyright (C) 2022 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import { ethers, providers } from 'ethers';
import { Logger } from 'pino';
import Assertions from '../helpers/assertions';

export default class RelayClient {

private readonly provider: providers.JsonRpcProvider;
private readonly logger: Logger;

constructor(relayUrl: string, logger: Logger) {
this.logger = logger;
this.provider = new ethers.providers.JsonRpcProvider(relayUrl);
}

/**
* Calls the specified methodName with the provided params
* @param methodName
* @param params
*/
async call(methodName: string, params: any[]) {
const result = await this.provider.send(methodName, params);
this.logger.trace(`[POST] to relay '${methodName}' with params [${params}] returned ${JSON.stringify(result)}`);
return result;
};

/**
* Calls the specified methodName with the provided params and asserts that it fails
* @param methodName
* @param params
*/
async callFailing(methodName: string, params: any[]) {
try {
const res = await this.call(methodName, params);
this.logger.trace(`[POST] to relay '${methodName}' with params [${params}] returned ${JSON.stringify(res)}`);
Assertions.expectedError();
} catch (err) {
Assertions.unknownResponse(err);
}
}

/**
* Calls the specified methodName and asserts that it is not supported
* @param methodName
* @param params
*/
async callUnsupported(methodName: string, params: any[]) {
try {
const res = await this.call(methodName, params);
this.logger.trace(`[POST] to relay '${methodName}' with params [${params}] returned ${JSON.stringify(res)}`);
Assertions.expectedError();
} catch (err) {
Assertions.unsupportedResponse(err);
return err;
}
};

/**
* Gets the account balance by executing `eth_getBalance`
* @param address
* @param block
*/
async getBalance(address, block = 'latest') {
this.logger.debug(`[POST] to relay eth_getBalance for address ${address}]`);
return this.provider.getBalance(address, block);
};

/**
* @param evmAddress
*
* Returns: The nonce of the account with the provided `evmAddress`
*/
async getAccountNonce(evmAddress): Promise<number> {
this.logger.debug(`[POST] to relay for eth_getTransactionCount for address ${evmAddress}`);
const nonce = await this.provider.send('eth_getTransactionCount', [evmAddress, 'latest']);
return Number(nonce);
};

/**
* This invokes the relay logic from eth.ts/sendRawTransaction.
*
* Returns: Transaction hash
* @param signedTx
*/
async sendRawTransaction(signedTx): Promise<string> {
this.logger.debug(`[POST] to relay for eth_sendRawTransaction`);
return this.provider.send('eth_sendRawTransaction', [signedTx]);
};

}
Loading

0 comments on commit 75625e1

Please # to comment.