aboutsummaryrefslogtreecommitdiffstats
path: root/packages/subproviders/test/unit/redundant_rpc_subprovider_test.ts
blob: ab540e202db8935be73217e1aadc98475fc4f0d3 (plain) (blame)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { JSONRPCResponsePayload } from '@0xproject/types';
import * as chai from 'chai';
import * as _ from 'lodash';
import Web3 = require('web3');
import Web3ProviderEngine = require('web3-provider-engine');
import RpcSubprovider = require('web3-provider-engine/subproviders/rpc');

import { RedundantRPCSubprovider } from '../../src';
import { DoneCallback } from '../../src/types';
import { chaiSetup } from '../chai_setup';
import { reportCallbackErrors } from '../utils/report_callback_errors';
import { subprovider as ganacheSubprovider } from '../utils/subprovider';

const expect = chai.expect;
chaiSetup.configure();

describe('RedundantRpcSubprovider', () => {
    let provider: Web3ProviderEngine;
    it('succeeds when supplied a healthy endpoint', (done: DoneCallback) => {
        provider = new Web3ProviderEngine();
        const endpoints = ['http://localhost:8545'];
        const redundantSubprovider = new RedundantRPCSubprovider(endpoints);
        // Hack: Hot-swap rpc with ganacheSubprovider
        (redundantSubprovider as any)._rpcs = [ganacheSubprovider];
        provider.addProvider(redundantSubprovider);
        provider.start();

        const payload = {
            jsonrpc: '2.0',
            method: 'eth_accounts',
            params: [],
            id: 1,
        };
        const callback = reportCallbackErrors(done)((err: Error, response: JSONRPCResponsePayload) => {
            expect(err).to.be.a('null');
            expect(response.result.length).to.be.equal(10);
            done();
        });
        provider.sendAsync(payload, callback);
    });
    it('succeeds when supplied at least one healthy endpoint', (done: DoneCallback) => {
        provider = new Web3ProviderEngine();
        const endpoints = ['http://does-not-exist:3000', 'http://localhost:8545'];
        const redundantSubprovider = new RedundantRPCSubprovider(endpoints);
        // Hack: Hot-swap rpcs with [nonExistentSubprovider, ganacheSubprovider]
        const nonExistentSubprovider = new RpcSubprovider({
            rpcUrl: 'http://does-not-exist:3000',
        });
        (redundantSubprovider as any)._rpcs = [nonExistentSubprovider, ganacheSubprovider];
        provider.addProvider(redundantSubprovider);
        provider.start();

        const payload = {
            jsonrpc: '2.0',
            method: 'eth_accounts',
            params: [],
            id: 1,
        };
        const callback = reportCallbackErrors(done)((err: Error, response: JSONRPCResponsePayload) => {
            expect(err).to.be.a('null');
            expect(response.result.length).to.be.equal(10);
            done();
        });
        provider.sendAsync(payload, callback);
    });
});