|
| 1 | +/* |
| 2 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +* you may not use this file except in compliance with the License. |
| 4 | +* You may obtain a copy of the License at |
| 5 | +* |
| 6 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +* |
| 8 | +* Unless required by applicable law or agreed to in writing, software |
| 9 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +* See the License for the specific language governing permissions and |
| 12 | +* limitations under the License. |
| 13 | +*/ |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +const mockery = require('mockery'); |
| 18 | +const path = require('path'); |
| 19 | +const RecordRate = require('../../../lib/worker/rate-control/recordRate'); |
| 20 | +const TestMessage = require('../../../lib/common/messages/testMessage'); |
| 21 | +const MockRate = require('./mockRate'); |
| 22 | +const TransactionStatisticsCollector = require('../../../lib/common/core/transaction-statistics-collector'); |
| 23 | + |
| 24 | +const chai = require('chai'); |
| 25 | +chai.should(); |
| 26 | +const sinon = require('sinon'); |
| 27 | + |
| 28 | +describe('RecordRate controller', () => { |
| 29 | + before(() => { |
| 30 | + mockery.enable({ |
| 31 | + warnOnReplace: false, |
| 32 | + warnOnUnregistered: false, |
| 33 | + useCleanCache: true |
| 34 | + }); |
| 35 | + |
| 36 | + mockery.registerMock(path.join(__dirname, '../../../lib/worker/rate-control/noRate.js'), MockRate); |
| 37 | + }); |
| 38 | + |
| 39 | + after(() => { |
| 40 | + mockery.deregisterAll(); |
| 41 | + mockery.disable(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should apply rate control to the recorded rate controller', async () => { |
| 45 | + const msgContent = { |
| 46 | + label: 'test', |
| 47 | + rateControl: { |
| 48 | + "type": "record-rate", |
| 49 | + "opts": { |
| 50 | + "rateController": { |
| 51 | + "type": "zero-rate" |
| 52 | + }, |
| 53 | + "pathTemplate": "../tx_records_client<C>_round<R>.txt", |
| 54 | + "outputFormat": "TEXT", |
| 55 | + "logEnd": true |
| 56 | + } |
| 57 | + }, |
| 58 | + workload: { |
| 59 | + module: 'module.js' |
| 60 | + }, |
| 61 | + testRound: 0, |
| 62 | + txDuration: 250, |
| 63 | + totalWorkers: 2 |
| 64 | + }; |
| 65 | + |
| 66 | + const testMessage = new TestMessage('test', [], msgContent); |
| 67 | + const stubStatsCollector = sinon.createStubInstance(TransactionStatisticsCollector); |
| 68 | + const rateController = RecordRate.createRateController(testMessage, stubStatsCollector, 0); |
| 69 | + const mockRate = MockRate.createRateController(); |
| 70 | + mockRate.reset(); |
| 71 | + mockRate.isApplyRateControlCalled().should.equal(false); |
| 72 | + await rateController.applyRateControl(); |
| 73 | + mockRate.isApplyRateControlCalled().should.equal(true); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should throw an error if the rate controller to record is unknown', async () => { |
| 77 | + const msgContent = { |
| 78 | + label: 'test', |
| 79 | + rateControl: { |
| 80 | + "type": "record-rate", |
| 81 | + "opts": { |
| 82 | + "rateController": { |
| 83 | + "type": "nonexistent-rate" |
| 84 | + }, |
| 85 | + "pathTemplate": "../tx_records_client<C>_round<R>.txt", |
| 86 | + "outputFormat": "TEXT", |
| 87 | + "logEnd": true |
| 88 | + } |
| 89 | + }, |
| 90 | + workload: { |
| 91 | + module: 'module.js' |
| 92 | + }, |
| 93 | + testRound: 0, |
| 94 | + txDuration: 250, |
| 95 | + totalWorkers: 2 |
| 96 | + }; |
| 97 | + const testMessage = new TestMessage('test', [], msgContent); |
| 98 | + |
| 99 | + const stubStatsCollector = sinon.createStubInstance(TransactionStatisticsCollector); |
| 100 | + (() => { |
| 101 | + RecordRate.createRateController(testMessage, stubStatsCollector, 0) |
| 102 | + }).should.throw(/Module "nonexistent-rate" could not be loaded/); |
| 103 | + }); |
| 104 | +}); |
0 commit comments