Files
api/packages/rpc-core/src/cached.spec.ts
T
Jaco GreeffandAmaury Martiny 011e24bd49 Pass registry to all codec types (#1592)
* Pass registry to all codec types

* Pass through registry inside API (where the rubber meets the road)

* Small additions

* Derive w/ registry

* Types are more-or-less covered

* Some test adjustments (guided by linting)

* Rid the world of some ../injector

* Additional cleanups, merge injector into registry

* Cleanup extrinsics (always passing registry)

* Remove injector imports (not all replaced)

* Typ spec cleanups

* Phase 1 - types seem lint-free

* rpc-{core,provider} lint free

* Start working through metadata

* Small metadata additions

* Some e2e tests (out-of-order)

* Down to a handful

* Fixup tests for packages/types/src/codec/*.spec.ts

* packages/types/src/codec passes all tests

* packages/types ok (Convert storage with registry fn)

* progress (sort out injection, dreaded find-before msg)

* Down to 1 lint issue...

* Linting done... along with on-demand import

* Exposed ordering

* Remove global injects

* Cleanup comments

* Add CHANGELOG & UPGRADING

* Apply suggestions from code review

Co-Authored-By: Amaury Martiny <amaury.martiny@protonmail.com>

* Update Metadata description
2019-11-21 16:32:39 +01:00

100 lines
3.2 KiB
TypeScript

// Copyright 2017-2019 @polkadot/rpc-core authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import testingPairs from '@polkadot/keyring/testingPairs';
import MockProvider from '@polkadot/rpc-provider/mock';
import { createType, TypeRegistry } from '@polkadot/types';
import Rpc from '.';
describe('Cached Observables', (): void => {
const registry = new TypeRegistry();
let rpc: Rpc;
const keyring = testingPairs();
beforeEach((): void => {
rpc = new Rpc(registry, new MockProvider(registry));
});
it('creates a single observable for subscriptions (multiple calls)', (): void => {
const observable1 = rpc.state.subscribeStorage([123]);
const observable2 = rpc.state.subscribeStorage([123]);
expect(observable2).toBe(observable1);
});
it('creates a single observable for subscriptions (multiple calls, no arguments)', (): void => {
const observable1 = rpc.chain.subscribeNewHeads();
const observable2 = rpc.chain.subscribeNewHeads();
expect(observable2).toBe(observable1);
});
it('creates a single observable (multiple calls, different arguments that should be cached together)', (): void => {
const observable1 = rpc.state.subscribeStorage([keyring.alice.address]);
const observable2 = rpc.state.subscribeStorage([createType(registry, 'AccountId', keyring.alice.address)]);
expect(observable2).toBe(observable1);
});
it('creates multiple observables for different values', (): void => {
const observable1 = rpc.chain.getBlockHash(123);
const observable2 = rpc.chain.getBlockHash(456);
expect(observable2).not.toBe(observable1);
});
it('subscribes to the same one if within the period (unbsub delay)', (done): void => {
const observable1 = rpc.chain.subscribeNewHeads();
const sub1 = observable1.subscribe();
sub1.unsubscribe();
setTimeout((): void => {
const observable2 = rpc.chain.subscribeNewHeads();
const sub2 = observable2.subscribe();
expect(observable1).toBe(observable2);
sub2.unsubscribe();
done();
}, 500);
});
it('clears cache if there are no more subscribers', (done): void => {
const observable1 = rpc.chain.subscribeNewHeads();
const observable2 = rpc.chain.subscribeNewHeads();
const sub1 = observable1.subscribe();
const sub2 = observable2.subscribe();
expect(observable1).toBe(observable2);
sub1.unsubscribe();
sub2.unsubscribe();
setTimeout((): void => {
// No more subscribers, now create a new observable
const observable3 = rpc.chain.subscribeNewHeads();
expect(observable3).not.toBe(observable1);
done();
}, 3500);
});
it('creates different observables for different methods but same arguments', (): void => {
// params do not match here
const observable1 = (rpc.chain as any).subscribeNewHeads([123]);
const observable2 = rpc.state.subscribeStorage([123]);
expect(observable2).not.toBe(observable1);
});
it('creates multiple observables for one-shots', (): void => {
const observable1 = rpc.chain.getBlockHash(123);
const observable2 = rpc.chain.getBlockHash(123);
expect(observable2).not.toBe(observable1);
});
});