<commit_before>'use strict';

require('../../helper');
var assert = require('../../support/assert');
var redisUtils = require('../../support/redis_utils');

var metadataBackend = require('cartodb-redis')({ pool: redisUtils.getPool() });
var JobPublisher = require('../../../batch/pubsub/job-publisher');
var JobQueue = require('../../../batch/job_queue');

var JobBackend = require('../../../batch/job_backend');
var JobService = require('../../../batch/job_service');
var UserDatabaseMetadataService = require('../../../batch/user_database_metadata_service');
var JobCanceller = require('../../../batch/job_canceller');
var metadataBackend = require('cartodb-redis')({ pool: redisUtils.getPool() });

describe('job queue', function () {
    var pool = redisUtils.getPool();
    var jobPublisher = new JobPublisher(pool);
    var jobQueue =  new JobQueue(metadataBackend, jobPublisher);
    var jobBackend = new JobBackend(metadataBackend, jobQueue);
    var userDatabaseMetadataService = new UserDatabaseMetadataService(metadataBackend);
    var jobCanceller = new JobCanceller(userDatabaseMetadataService);
    var jobService = new JobService(jobBackend, jobCanceller);

    var userA = 'userA';
    var userB = 'userB';

    beforeEach(function () {
        this.jobQueue = new JobQueue(metadataBackend, jobPublisher);
    });

    afterEach(function (done) {
        redisUtils.clean('batch:*', done);
    });

    it('should find queues for one user', function (done) {
        var self = this;

        this.jobQueue.enqueue(userA, 'wadus-wadus-wadus-wadus', function(err) {
            if (err) {
                return done(err);
            }

            self.jobQueue.scanQueues(function (err, queues) {
                assert.ifError(err);
                assert.equal(queues.length, 1);
                assert.equal(queues[0], userA);
                return done();
            });
        });
    });

    it('should find queues for more than one user', function (done) {
        var self = this;

        this.jobQueue.enqueue(userA, 'wadus-wadus-wadus-wadus', function(err) {
            if (err) {
                return done(err);
            }
            self.jobQueue.enqueue(userB, 'wadus-wadus-wadus-wadus', function(err) {
                if (err) {
                    return done(err);
                }

                self.jobQueue.scanQueues(function (err, queues) {
                    assert.ifError(err);
                    assert.equal(queues.length, 2);
                    assert.ok(queues[0] === userA || queues[0] === userB);
                    assert.ok(queues[1] === userA || queues[1] === userB);

                    return done();
                });
            });
        });
    });

    it('.scanQueues() should feed queue index', function (done) {
        var self = this;

        var data = {
            user: 'vizzuality',
            query: 'select 1 as cartodb_id',
            host: 'localhost'<commit_msg>Add test to validate old jobs will get discovered<commit_after>'use strict';

require('../../helper');
var assert = require('../../support/assert');
var redisUtils = require('../../support/redis_utils');

var metadataBackend = require('cartodb-redis')({ pool: redisUtils.getPool() });
var JobPublisher = require('../../../batch/pubsub/job-publisher');
var JobQueue = require('../../../batch/job_queue');

var JobBackend = require('../../../batch/job_backend');
var JobService = require('../../../batch/job_service');
var UserDatabaseMetadataService = require('../../../batch/user_database_metadata_service');
var JobCanceller = require('../../../batch/job_canceller');
var metadataBackend = require('cartodb-redis')({ pool: redisUtils.getPool() });

describe('job queue', function () {
    var pool = redisUtils.getPool();
    var jobPublisher = new JobPublisher(pool);
    var jobQueue =  new JobQueue(metadataBackend, jobPublisher);
    var jobBackend = new JobBackend(metadataBackend, jobQueue);
    var userDatabaseMetadataService = new UserDatabaseMetadataService(metadataBackend);
    var jobCanceller = new JobCanceller(userDatabaseMetadataService);
    var jobService = new JobService(jobBackend, jobCanceller);

    var userA = 'userA';
    var userB = 'userB';

    beforeEach(function () {
        this.jobQueue = new JobQueue(metadataBackend, jobPublisher);
    });

    afterEach(function (done) {
        redisUtils.clean('batch:*', done);
    });

    it('should find queues for one user', function (done) {
        var self = this;

        this.jobQueue.enqueue(userA, 'wadus-wadus-wadus-wadus', function(err) {
            if (err) {
                return done(err);
            }

            self.jobQueue.scanQueues(function (err, queues) {
                assert.ifError(err);
                assert.equal(queues.length, 1);
                assert.equal(queues[0], userA);
                return done();
            });
        });
    });

    it('should find queues for more than one user', function (done) {
        var self = this;

        this.jobQueue.enqueue(userA, 'wadus-wadus-wadus-wadus', function(err) {
            if (err) {
                return done(err);
            }
            self.jobQueue.enqueue(userB, 'wadus-wadus-wadus-wadus', function(err) {
                if (err) {
                    return done(err);
                }

                self.jobQueue.scanQueues(function (err, queues) {
                    assert.ifError(err);
                    assert.equal(queues.length, 2);
                    assert.ok(queues[0] === userA || queues[0] === userB);
                    assert.ok(queues[1] === userA || queues[1] === userB);

                    return done();
                });
            });
        });
    });

    it('should find queues from jobs not using new Redis SETs for users', function(done) {
        var self = this;
        var redisArgs = [JobQueue.QUEUE.PREFIX + userA, 'wadus-id'];
        metadataBackend.redisCmd(JobQueue.QUEUE.DB, 'LPUSH', redisArgs, function (err) {
            assert.ok(!err, err);
            self.jobQueue.scanQueues(function (err, queues) {
                assert.ok(!err, err);

                assert.equal(queues.length, 1);
                assert.equal(queues[0], userA);

                return done();
            });
        });
    });

    it('.scanQueues() should feed queue index', function (done) {
        var self = this;

        var data = {
            user: 'vizzuality',
            query: 'select 1 as cartodb_id',
            host: 'localhost'
{'fraction_non_alphanumeric': 0.14928112572652188, 'fraction_numerical': 0.0027531355154481493, 'mean_word_length': 2.13621103117506, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3212200', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1871, 'n_words': 400}