Skip to content

Commit

Permalink
cover chunker and converters (#6638)
Browse files Browse the repository at this point in the history
  • Loading branch information
avkos authored Dec 5, 2023
1 parent 5aa8b7f commit 5afcb54
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 457 deletions.
4 changes: 3 additions & 1 deletion packages/web3-utils/src/chunk_response_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ export class ChunkResponseParser {
private _clearQueues: (() => void) | undefined;
private readonly eventEmitter: EventEmitter;
private readonly autoReconnect: boolean;
private readonly chunkTimout: number;

public constructor(eventEmitter: EventEmitter, autoReconnect: boolean) {
this.eventEmitter = eventEmitter;
this.autoReconnect = autoReconnect;
this.chunkTimout = 1000 * 15;
}
private clearQueues(): void {
if (typeof this._clearQueues === 'function') {
Expand Down Expand Up @@ -81,7 +83,7 @@ export class ChunkResponseParser {
error: { code: 2, message: 'Chunk timeout' },
}),
);
}, 1000 * 15);
}, this.chunkTimout);
return;
}

Expand Down
25 changes: 25 additions & 0 deletions packages/web3-utils/test/fixtures/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ export const bytesToUint8ArrayValidData: [Bytes, Uint8Array][] = [
['0X12c6', new Uint8Array([18, 198])],
['0X01', new Uint8Array([1])],
['0X00', new Uint8Array([0])],
['0X00', new Uint8Array([0])],
['0x1234', new Uint8Array([18, 52])],
['0x1234', new Uint8Array([18, 52])],
[new Uint8Array(hexToBytes('0c12')), new Uint8Array(hexToBytes('0c12'))],
];
Expand All @@ -335,10 +337,33 @@ export const toBigIntValidData: [any, bigint][] = [
[24, BigInt(24)],
['123', BigInt(123)],
['0x04', BigInt(4)],
['-0x1', -BigInt(1)],
];

export const toBigIntInvalidData: [any, string][] = [
[new Uint8Array([]), 'can not parse as number data'],
['wwwww', ' Error: can not parse as number data'],
['zzzzee0xiiuu', ' Error: can not parse as number data'],
];

export const toBoolValidData: [boolean | string | number | unknown, boolean][] = [
[true, true],
[false, false],
[0, false],
[1, true],
[BigInt(1), true],
[BigInt(0), false],
['true', true],
['false', false],
['1', true],
['0', false],
['0x0', false],
['0x1', true],
];

export const toBoolInvalidData: [boolean | string | number | unknown, string][] = [
[100, 'not a valid boolean'],
[BigInt(100), 'not a valid boolean'],
['100', 'not a valid boolean'],
[{}, 'not a valid boolean'],
];
29 changes: 29 additions & 0 deletions packages/web3-utils/test/fixtures/hugeData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
export const hugeData = {
data: '{"jsonrpc":"2.0","id":"96aa3f13-077c-4c82-a64a-64b8626f8192","result":"0x1414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141"}\n{"jsonrpc":"2.0","id":"1b10c675-5eac-4c80-b300-36085d63d5f4","result":"0xd5"}\n{"jsonrpc":"2.0","id":"96aa3f13-077c-4c82-a64a-64b8626f819c","result":"0xd5"}\n{"jsonrpc":"2.0","id":"bba428bf-6726-425a-8cbc-2ed7c9448ee7","result":{"blockHash":"0xccfea468ce813e63d59be075dd71de76d4236fa3292dc3b4cc5e6f15c27c3ad1","blockNumber":"0xd4","contractAddress":null,"cumulativeGasUsed":"0xad8c516","effectiveGasPrice":"0x9502f907","from":"0x12b1d9d74d73b1c3a245b19c1c5501c653af1af9","gasUsed":"0x22b5a9e","logs":[],"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","status":"0x1","to":"0xf362b2285d41dc806fd61b92f7c192006b707f42","transactionHash":"0x01ab900543af6d9dbb27b21cf62339c07bfd6d1acdc01f66c932ed8f3c731412","transactionIndex":"0x4","type":"0x2"}}\n',
result: [
JSON.parse(
'{"jsonrpc":"2.0","id":"96aa3f13-077c-4c82-a64a-64b8626f8192","result":"0x1414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141"}',
),
JSON.parse('{"jsonrpc":"2.0","id":"1b10c675-5eac-4c80-b300-36085d63d5f4","result":"0xd5"}'),
JSON.parse('{"jsonrpc":"2.0","id":"96aa3f13-077c-4c82-a64a-64b8626f819c","result":"0xd5"}'),
JSON.parse(
'{"jsonrpc":"2.0","id":"bba428bf-6726-425a-8cbc-2ed7c9448ee7","result":{"blockHash":"0xccfea468ce813e63d59be075dd71de76d4236fa3292dc3b4cc5e6f15c27c3ad1","blockNumber":"0xd4","contractAddress":null,"cumulativeGasUsed":"0xad8c516","effectiveGasPrice":"0x9502f907","from":"0x12b1d9d74d73b1c3a245b19c1c5501c653af1af9","gasUsed":"0x22b5a9e","logs":[],"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","status":"0x1","to":"0xf362b2285d41dc806fd61b92f7c192006b707f42","transactionHash":"0x01ab900543af6d9dbb27b21cf62339c07bfd6d1acdc01f66c932ed8f3c731412","transactionIndex":"0x4","type":"0x2"}}\n',
),
],
};
74 changes: 74 additions & 0 deletions packages/web3-utils/test/unit/chunk_response_parser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

import { EventEmitter } from 'events';
import { InvalidResponseError } from 'web3-errors';
import { ChunkResponseParser } from '../../src/chunk_response_parser';
import { hugeData } from '../fixtures/hugeData';

jest.setTimeout(20000);
describe('chunk_response_parser', () => {
let parser: ChunkResponseParser;
let eventEmiter: EventEmitter;
beforeEach(() => {
eventEmiter = new EventEmitter();
parser = new ChunkResponseParser(eventEmiter, false);
});
it('clearQueue', () => {
const clearQueue = jest.fn();
parser.onError(clearQueue);
// @ts-expect-error call private method
parser.clearQueues();
expect(clearQueue).toHaveBeenCalled();
});
it('parse response', () => {
const res = parser.parseResponse(hugeData.data);
expect(res).toEqual(hugeData.result);
});

it('parse response with last chunk mechanism', () => {
parser.parseResponse(
'{"jsonrpc":"2.0","id":"96aa3f13-077c-4c82-a64a-64b8626f8192","result":"0x141414141',
);
parser.parseResponse('123');
const res = parser.parseResponse('14141"}\n');
expect(res[0]).toEqual({
jsonrpc: '2.0',
id: '96aa3f13-077c-4c82-a64a-64b8626f8192',
result: '0x14141414112314141',
});
});

it('lastChunkTimeout error', async () => {
// @ts-expect-error set private property
parser.chunkTimout = 10;
parser.parseResponse(
'{"jsonrpc":"2.0","id":"96aa3f13-077c-4c82-a64a-64b8626f8192","result":"0x141414141',
);
const onError = jest.fn();
eventEmiter.on('error', onError);
// eslint-disable-next-line no-promise-executor-return
await new Promise(resolve => setTimeout(resolve, 1000));
expect(onError).toHaveBeenCalledWith(
new InvalidResponseError({
id: 1,
jsonrpc: '2.0',
error: { code: 2, message: 'Chunk timeout' },
}),
);
});
});
17 changes: 17 additions & 0 deletions packages/web3-utils/test/unit/converters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
toChecksumAddress,
bytesToUint8Array,
toBigInt,
toBool,
} from '../../src/converters';

import {
Expand Down Expand Up @@ -71,6 +72,8 @@ import {
toBigIntInvalidData,
toCheckSumInvalidData,
numberToHexstrictValidData,
toBoolValidData,
toBoolInvalidData,
} from '../fixtures/converters';

describe('converters', () => {
Expand Down Expand Up @@ -403,4 +406,18 @@ describe('converters', () => {
});
});
});

describe('toBool', () => {
describe('valid cases', () => {
it.each(toBoolValidData)('%s', (input, output) => {
expect(toBool(input)).toEqual(output);
});
});

describe('invalid cases', () => {
it.each(toBoolInvalidData)('%s', (input, output) => {
expect(() => toBool(input)).toThrow(output);
});
});
});
});
70 changes: 0 additions & 70 deletions packages/web3/test/e2e_manual/data.test.ts

This file was deleted.

Loading

0 comments on commit 5afcb54

Please sign in to comment.