-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.test.ts
36 lines (30 loc) · 1.15 KB
/
connect.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { ApiPromise } from '@polkadot/api';
import { getApi } from './util';
test("sequence call only connect once", async () => {
const api1 = await getApi();
const block1 = await api1.rpc.chain.getBlock();
const api2 = await getApi();
const block2 = await api2.rpc.chain.getBlock();
expect(block1.block.header.number.toNumber()).toBe(block2.block.header.number.toNumber());
});
test("concurrent get api call only connect once", async () => {
const [api1, api2] = await Promise.all([
getApi(),
getApi(),
]);
const block1 = await api1.rpc.chain.getBlock();
const block2 = await api2.rpc.chain.getBlock();
expect(block1.block.header.number.toNumber()).toBe(block2.block.header.number.toNumber());
});
test("concurrent get block call only connect once", async () => {
const api = await getApi();
const [block1, block2] = await Promise.all([
getLatestBlock(api),
getLatestBlock(api),
]);
expect(block1).toBe(block2);
});
async function getLatestBlock(api: ApiPromise): Promise<number> {
const block = await api.rpc.chain.getBlock();
return block.block.header.number.toNumber();
}