github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/kat/src/test/ethereum/lib/config.ts (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  import * as utils from '../../../lib/utils';
    16  import nock from 'nock';
    17  import sinon from 'sinon';
    18  import assert from 'assert';
    19  
    20  export const testUtils = async () => {
    21  
    22  describe('Utils', () => {
    23  
    24    before(() => {
    25      sinon.replace(utils, 'constants', { ...utils.constants, REST_API_CALL_MAX_ATTEMPTS: 3, REST_API_CALL_RETRY_DELAY_MS: 0 });
    26    });
    27  
    28    describe('Axios with retry', () => {
    29  
    30      it('First attempt successful', async () => {
    31        nock('https://kaleido.io')
    32          .get('/test')
    33          .reply(200, { data: 'test' });
    34        const result = await utils.axiosWithRetry({
    35          url: 'https://kaleido.io/test'
    36        });
    37        assert.deepStrictEqual(result.data, { data: 'test' });
    38      });
    39  
    40      it('1 failed attempt', async () => {
    41        nock('https://kaleido.io')
    42          .get('/test')
    43          .reply(500)
    44          .get('/test')
    45          .reply(200, { data: 'test' });
    46  
    47        const result = await utils.axiosWithRetry({
    48          url: 'https://kaleido.io/test'
    49        });
    50        assert.deepStrictEqual(result.data, { data: 'test' });
    51      });
    52  
    53      it('2 failed attempts', async () => {
    54        nock('https://kaleido.io')
    55          .get('/test')
    56          .reply(500)
    57          .get('/test')
    58          .reply(500)
    59          .get('/test')
    60          .reply(200, { data: 'test' });
    61        const result = await utils.axiosWithRetry({
    62          url: 'https://kaleido.io/test'
    63        });
    64        assert.deepStrictEqual(result.data, { data: 'test' });
    65      });
    66  
    67      it('3 failed attempts', async () => {
    68        nock('https://kaleido.io')
    69          .get('/test')
    70          .reply(500)
    71          .get('/test')
    72          .reply(500)
    73          .get('/test')
    74          .reply(500)
    75        try {
    76          await utils.axiosWithRetry({
    77            url: 'https://kaleido.io/test'
    78          });
    79        } catch (err) {
    80          assert.strictEqual(err.response.status, 500);
    81        }
    82      });
    83  
    84      it('Not found should return immediately', async () => {
    85        nock('https://kaleido.io')
    86          .get('/test')
    87          .reply(404);
    88        try {
    89          await utils.axiosWithRetry({
    90            url: 'https://kaleido.io/test'
    91          });
    92        } catch (err) {
    93          assert.deepStrictEqual(err.response.status, 404);
    94        }
    95      });
    96  
    97    });
    98  
    99  });
   100  };