<commit_before>// "use strict"
// 
// const stubs = require("../Common/stubs")
// stubs.stubLogs()
// 
// const cluster = require("cluster")
// //cluster.isWorker = true
// const Worker = require("../../Application/Worker")
// const sinon = require("sinon")
// const should = require("should")
// 
// process.setMaxListeners(0);
// 
// describe('Worker test', () => {
//     
// //     let forkStub;
// // 
// //     beforeEach(function () {
// //         let workerObj = {
// //             on: function () { }
// //         };
// //         forkStub = sinon.stub(cluster, "fork").returns(workerObj);
// //     });
// // 
// //     afterEach(function () {
// //         forkStub.restore();
// //     });
//     
//     it('consume the queue synchronously', () => {
//         let jobSpy = sinon.spy()
//         
//         process.send(jobSpy);
//     })
// })<commit_msg>worker test<commit_after>"use strict"

const stubs = require("../Common/stubs")
stubs.stubLogs()

const cluster = require("cluster")
const Worker = require("../../Application/Worker")
const sinon = require("sinon")
const should = require("should")
const EventEmitter = require('events')
const util = require('util')
const Message = require("../../Entities/Message")
const jobService = require("../../Services/JobService")

process.setMaxListeners(0);

describe('Worker test', () => {
    
    let forkStub;

    beforeEach(function () {
        let workerObj = {
            on: function () { }
        };
        forkStub = sinon.stub(cluster, "fork").returns(workerObj)
    });

    afterEach(function () {
        forkStub.restore();
    });
    
    it('worker error event', () => {
        
        let errorStub = sinon.stub(jobService, "error")
        let doneStub = sinon.stub(jobService, "done")
        
        function WorkerObj() {
            EventEmitter.call(this)
        }
        util.inherits(WorkerObj, EventEmitter)
        
        let workerObject = new WorkerObj()
        new Worker(workerObject)
        let message = new Message(1, "error")
        
        workerObject.emit('message', message)
        
        errorStub.calledWith(message.id, message.err).should.be.true()
        doneStub.called.should.be.false()
        
        errorStub.restore()
        doneStub.restore()
    })
    
    it('worker done', () => {
        
        let errorStub = sinon.stub(jobService, "error")
        let doneStub = sinon.stub(jobService, "done")
        
        function WorkerObj() {
            EventEmitter.call(this)
        }
        util.inherits(WorkerObj, EventEmitter)
        
        let workerObject = new WorkerObj()
        new Worker(workerObject)
        let message = new Message(1)
        
        workerObject.emit('message', message)
        
        errorStub.called.should.be.false()
        doneStub.calledWith(message.id).should.be.true()
        
        errorStub.restore()
        doneStub.restore()
    })
})
{'fraction_non_alphanumeric': 0.1671204901293397, 'fraction_numerical': 0.0013614703880190605, 'mean_word_length': 1.930209371884347, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3284561', 'n_tokens_mistral': 861, 'n_tokens_neox': 889, 'n_words': 155}