aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/test/data_sources/contract-wrappers/utils_test.ts
blob: 06f1a5e86a46e61c5117d566bd2241f03242e8aa (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// tslint:disable:custom-no-magic-numbers
import * as chai from 'chai';
import { LogWithDecodedArgs } from 'ethereum-types';
import 'mocha';

import { _getEventsWithRetriesAsync } from '../../../src/data_sources/contract-wrappers/utils';
import { chaiSetup } from '../../utils/chai_setup';

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

const retryableMessage = 'network timeout: (simulated network timeout error)';
const retryableError = new Error(retryableMessage);

describe('data_sources/contract-wrappers/utils', () => {
    describe('_getEventsWithRetriesAsync', () => {
        it('sends a single request if it was successful', async () => {
            // Pre-declare values for the fromBlock and toBlock arguments.
            const expectedFromBlock = 100;
            const expectedToBlock = 200;
            const expectedLogs: Array<LogWithDecodedArgs<any>> = [
                {
                    logIndex: 123,
                    transactionIndex: 456,
                    transactionHash: '0x6dd106d002873746072fc5e496dd0fb2541b68c77bcf9184ae19a42fd33657fe',
                    blockHash: '0x6dd106d002873746072fc5e496dd0fb2541b68c77bcf9184ae19a42fd33657ff',
                    blockNumber: 789,
                    address: '0x6dd106d002873746072fc5e496dd0fb2541b68c77bcf9184ae19a42fd3365800',
                    data: 'fake raw data',
                    topics: [],
                    event: 'TEST_EVENT',
                    args: [1, 2, 3],
                },
            ];

            // mockGetEventsAsync checks its arguments, increments `callCount`
            // and returns `expectedLogs`.
            let callCount = 0;
            const mockGetEventsAsync = async (
                fromBlock: number,
                toBlock: number,
            ): Promise<Array<LogWithDecodedArgs<any>>> => {
                expect(fromBlock).equals(expectedFromBlock);
                expect(toBlock).equals(expectedToBlock);
                callCount += 1;
                return expectedLogs;
            };

            // Make sure that we get what we expected and that the mock function
            // was called exactly once.
            const gotLogs = await _getEventsWithRetriesAsync(mockGetEventsAsync, 3, expectedFromBlock, expectedToBlock);
            expect(gotLogs).deep.equals(expectedLogs);
            expect(callCount).equals(
                1,
                'getEventsAsync function was called more than once even though it was successful',
            );
        });
        it('retries and eventually succeeds', async () => {
            const numRetries = 5;
            let callCount = 0;
            // mockGetEventsAsync throws unless callCount == numRetries + 1.
            const mockGetEventsAsync = async (
                _fromBlock: number,
                _toBlock: number,
            ): Promise<Array<LogWithDecodedArgs<any>>> => {
                callCount += 1;
                if (callCount === numRetries + 1) {
                    return [];
                }
                throw retryableError;
            };
            await _getEventsWithRetriesAsync(mockGetEventsAsync, numRetries, 100, 300);
            expect(callCount).equals(numRetries + 1, 'getEventsAsync function was called the wrong number of times');
        });
        it('throws for non-retryable errors', async () => {
            const numRetries = 5;
            const expectedMessage = 'Non-retryable error';
            // mockGetEventsAsync always throws a non-retryable error.
            const mockGetEventsAsync = async (
                _fromBlock: number,
                _toBlock: number,
            ): Promise<Array<LogWithDecodedArgs<any>>> => {
                throw new Error(expectedMessage);
            };
            // Note(albrow): This does actually return a promise (or at least a
            // "promise-like object" and is a false positive in TSLint.
            // tslint:disable-next-line:await-promise
            await expect(_getEventsWithRetriesAsync(mockGetEventsAsync, numRetries, 100, 300)).to.be.rejectedWith(
                expectedMessage,
            );
        });
        it('throws after too many retries', async () => {
            const numRetries = 5;
            // mockGetEventsAsync always throws a retryable error.
            const mockGetEventsAsync = async (
                _fromBlock: number,
                _toBlock: number,
            ): Promise<Array<LogWithDecodedArgs<any>>> => {
                throw retryableError;
            };
            // Note(albrow): This does actually return a promise (or at least a
            // "promise-like object" and is a false positive in TSLint.
            // tslint:disable-next-line:await-promise
            await expect(_getEventsWithRetriesAsync(mockGetEventsAsync, numRetries, 100, 300)).to.be.rejectedWith(
                retryableMessage,
            );
        });
    });
});