Skip to content

Commit

Permalink
add and fix old test cases for multiple subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad-Altabba committed Jun 22, 2023
1 parent 768fa9b commit 8727ea1
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 4 deletions.
21 changes: 17 additions & 4 deletions packages/web3-eth/test/integration/subscription_heads.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ describeIf(isSocket)('subscription', () => {
let times = 0;
const pr = new Promise((resolve: Resolve, reject) => {
sub.on('data', (data: BlockHeaderOutput) => {
if (data.parentHash) {
times += 1;
}
expect(typeof data.parentHash).toBe('string');

times += 1;
expect(times).toBeGreaterThanOrEqual(times);
if (times >= checkTxCount) {
resolve();
Expand All @@ -65,12 +65,25 @@ describeIf(isSocket)('subscription', () => {
await web3.eth.subscriptionManager?.removeSubscription(sub);
await closeOpenConnection(web3.eth);
});
it(`clear`, async () => {
it(`remove at subscriptionManager`, async () => {
const web3Eth = new Web3Eth(clientUrl);
await waitForOpenConnection(web3Eth);
const sub: NewHeadsSubscription = await web3Eth.subscribe('newHeads');
expect(sub.id).toBeDefined();
const subId = sub.id as string;
await web3Eth.subscriptionManager?.removeSubscription(sub);
expect(web3Eth.subscriptionManager.subscriptions.has(subId)).toBe(false);
expect(sub.id).toBeUndefined();
await closeOpenConnection(web3Eth);
});
it(`remove at subscribe object`, async () => {
const web3Eth = new Web3Eth(clientUrl);
await waitForOpenConnection(web3Eth);
const sub: NewHeadsSubscription = await web3Eth.subscribe('newHeads');
expect(sub.id).toBeDefined();
const subId = sub.id as string;
await sub.unsubscribe();
expect(web3Eth.subscriptionManager.subscriptions.has(subId)).toBe(false);
expect(sub.id).toBeUndefined();
await closeOpenConnection(web3Eth);
});
Expand Down
114 changes: 114 additions & 0 deletions packages/web3-eth/test/integration/subscription_on_2_events.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
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/>.
*/
// eslint-disable-next-line import/no-extraneous-dependencies
import { BlockHeaderOutput, Web3 } from 'web3';
import {
closeOpenConnection,
describeIf,
getSystemTestProvider,
isSocket,
sendFewSampleTxs,
waitForOpenConnection,
} from '../fixtures/system_test_utils';
import { Resolve } from './helper';

const checkTxCount = 2;
describeIf(isSocket)('subscription on multiple events', () => {
test(`catch the data of pendingTransactions and newHeads`, async () => {
const web3 = new Web3(getSystemTestProvider());
const web3Eth = web3.eth;
await waitForOpenConnection(web3Eth);
const pendingTransactionsSub = await web3Eth.subscribe('pendingTransactions');

let pendingTransactionsCount = 0;
const pendingTransactionsData = new Promise((resolve: Resolve, reject) => {
(() => {
pendingTransactionsSub.on('data', (data: string) => {
expect(typeof data).toBe('string');

pendingTransactionsCount += 1;
if (pendingTransactionsCount >= checkTxCount) {
resolve();
}
});
pendingTransactionsSub.on('error', error => {
reject(error);
});
})();
});

const newHeadsSub = await web3.eth.subscribe('newHeads');
let newHeadsCount = 0;
const newHeadsData = new Promise((resolve: Resolve, reject) => {
newHeadsSub.on('data', (data: BlockHeaderOutput) => {
expect(typeof data.parentHash).toBe('string');

newHeadsCount += 1;
if (newHeadsCount >= checkTxCount) {
resolve();
}
});
newHeadsSub.on('error', error => {
reject(error);
});
});

await sendFewSampleTxs(2);

await pendingTransactionsData;
await newHeadsData;

await closeOpenConnection(web3Eth);
});

test(`catch the data of an event even after subscribing off another one`, async () => {
const web3 = new Web3(getSystemTestProvider());
const web3Eth = web3.eth;
await waitForOpenConnection(web3Eth);
const pendingTransactionsSub = await web3Eth.subscribe('pendingTransactions');

// eslint-disable-next-line @typescript-eslint/no-empty-function
pendingTransactionsSub.on('data', () => {});
pendingTransactionsSub.on('error', error => {
throw error;
});

const newHeadsSub = await web3.eth.subscribe('newHeads');
let times = 0;
const newHeadsData = new Promise((resolve: Resolve, reject) => {
newHeadsSub.on('data', (data: BlockHeaderOutput) => {
expect(typeof data.parentHash).toBe('string');

times += 1;
if (times >= checkTxCount) {
resolve();
}
});
newHeadsSub.on('error', error => {
reject(error);
});
});

await pendingTransactionsSub.unsubscribe();

await sendFewSampleTxs(2);

await newHeadsData;

await closeOpenConnection(web3Eth);
});
});

0 comments on commit 8727ea1

Please sign in to comment.