github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/kat/src/test/ethereum/lib/batch-manager-test.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 assert from 'assert';
    16  import sinon, { SinonStub } from 'sinon';
    17  import * as database from '../../../clients/database';
    18  import { BatchManager } from '../../../lib/batch-manager';
    19  import { BatchProcessor } from '../../../lib/batch-processor';
    20  
    21  export const testBatchManager = async () => {
    22  
    23  describe('BatchManager', () => {
    24  
    25    let batchRetriever: SinonStub;
    26    let processorInit: SinonStub;
    27  
    28    beforeEach(() => {
    29      batchRetriever = sinon.stub(database, 'retrieveBatches');
    30      processorInit = sinon.stub(BatchProcessor.prototype, 'init');
    31    });
    32  
    33    afterEach(() => {
    34      batchRetriever.restore();
    35      processorInit.restore();
    36    })
    37  
    38    it('inits all the right processors', async () => {
    39  
    40      batchRetriever.resolves([
    41        {
    42          type: 't1',
    43          author: 'author1',
    44          batchID: 'author1-batch1',
    45        },
    46        {
    47          type: 't1',
    48          author: 'author2',
    49          batchID: 'author2-batch1',
    50        },
    51        {
    52          type: 't1',
    53          author: 'author3',
    54          batchID: 'author3-batch1',
    55        },
    56        {
    57          type: 't1',
    58          author: 'author2',
    59          batchID: 'author2-batch2',
    60        },
    61        {
    62          type: 't1',
    63          author: 'author1',
    64          batchID: 'author1-batch2',
    65        },
    66      ]);
    67  
    68      const bm = new BatchManager('t1', sinon.stub());
    69      await bm.init();
    70  
    71      sinon.assert.calledWith(processorInit, [
    72        {
    73          type: 't1',
    74          author: 'author1',
    75          batchID: 'author1-batch1',
    76        },
    77        {
    78          type: 't1',
    79          author: 'author1',
    80          batchID: 'author1-batch2',
    81        }
    82      ]);
    83  
    84      sinon.assert.calledWith(processorInit, [
    85        {
    86          type: 't1',
    87          author: 'author2',
    88          batchID: 'author2-batch1',
    89        },
    90        {
    91          type: 't1',
    92          author: 'author2',
    93          batchID: 'author2-batch2',
    94        }
    95      ]);
    96  
    97      sinon.assert.calledWith(processorInit, [
    98        {
    99          type: 't1',
   100          author: 'author3',
   101          batchID: 'author3-batch1',
   102        }
   103      ]);
   104    });
   105  
   106    it('caches batch processors', async () => {
   107  
   108      class TestBatchManagerWrapper extends BatchManager {
   109        public processorCompleteCallback(author: string) {
   110          return super.processorCompleteCallback(author);
   111        }
   112      }
   113  
   114      const bm = new TestBatchManagerWrapper('t1', sinon.stub());
   115      const bp1 = bm.getProcessor('author1');
   116  
   117      // Check it caches
   118      assert(bp1 === bm.getProcessor('author1'));
   119  
   120      // Check it doesn't clear the wrong entry
   121      bm.processorCompleteCallback('author2');
   122      assert(bp1 === bm.getProcessor('author1'));
   123  
   124      // Check it clears the right entry
   125      bm.processorCompleteCallback('author1');
   126      assert(bp1 !== bm.getProcessor('author1'));
   127  
   128    })
   129  
   130  });
   131  };