Skip to content

Commit

Permalink
Refactoring index test
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyudmil Danailov authored and Lyudmil Danailov committed Jan 23, 2024
1 parent bea9610 commit 50bafc6
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const BaseWorker = require('../lib/worker_base');
const { Exporter } = require('../lib/kafka_storage');
const { worker } = require('../blockchains/eth/eth_worker');
const zkClientAsync = require('../lib/zookeeper_client_async');
const TaskManager = require('../lib/task_manager');

describe('Main', () => {
const constants = {
Expand Down Expand Up @@ -159,21 +160,24 @@ describe('Main', () => {
mainInstance.exporter = new Exporter('test-exporter');
sinon.stub(worker.prototype, 'init').resolves();
sinon.stub(mainInstance, 'handleInitPosition').resolves();
sinon.stub(TaskManager.prototype, 'initQueue').resolves();
mainInstance.lastProcessedPosition = { blockNumber: 10, primaryKey: 1 };

await mainInstance.initWorker();
assert(mainInstance.handleInitPosition.calledOnce);
assert.strictEqual(mainInstance.taskManager.lastPushedToQueue, 0);
});

it('workLoop throws error when worker can\'t be initialised', async () => {
sinon.stub(BaseWorker.prototype, 'work').rejects(new Error('Error in worker "work" method'));
const mainInstance = new Main();
mainInstance.worker = new BaseWorker(constants);
try {
await mainInstance.workLoop();
expect.fail('workLoop should have thrown an error');
} catch (err) {
assert.strictEqual(err.message, 'Error in worker "work" method');
}
mainInstance.taskManager = new TaskManager(0, 50);
await mainInstance.taskManager.initQueue(1);
sinon.spy(mainInstance, 'workLoop');
await mainInstance.workLoop();
assert(mainInstance.workLoop.calledOnce);
assert.strictEqual(mainInstance.shouldWork, false);
});

it('workLoop throws error when storeEvents() fails', async () => {
Expand All @@ -184,6 +188,8 @@ describe('Main', () => {
const mainInstance = new Main();
mainInstance.worker = new BaseWorker(constants);
mainInstance.exporter = new Exporter('test-exporter');
mainInstance.taskManager = new TaskManager(0, 50);
await mainInstance.taskManager.initQueue(1);
try {
await mainInstance.workLoop();
expect.fail('workLoop should have thrown an error');
Expand All @@ -201,6 +207,8 @@ describe('Main', () => {
const mainInstance = new Main();
mainInstance.worker = new BaseWorker(constants);
mainInstance.exporter = new Exporter('test-exporter');
mainInstance.taskManager = new TaskManager(0, 50);
await mainInstance.taskManager.initQueue(1);
try {
await mainInstance.workLoop();
expect.fail('workLoop should have thrown an error');
Expand Down

0 comments on commit 50bafc6

Please sign in to comment.