jeudi 30 juillet 2015

Sqlite 3 locking in node.js mocha tests

I'm writing a test suite for my codebase, but I'm having trouble with concurrent database operations locking certain tests out. For example, I have a test for my code which builds and fills knex tables

describe('InitializeDBService', function(){
    var init;
    var Knex;
    before(function(){
        init = require('../../app/services').InitializeDBService;
        //use our test database
        Knex = require('../../config/db').test();
    });
    beforeEach(function(done){
        this.timeout(5000);
        return Knex.schema.dropTableIfExists("Metrics")
        .then(function(){return Knex.schema.dropTableIfExists("Deployment")})
        .then(function(){return Knex.schema.dropTableIfExists("History")})
        .then(function(){return Knex.schema.dropTableIfExists("Users")})
        .then(function(){
            done();
        });
    });
    describe('Deployment Table', function(){
        it("shouldn't exist already", function(done){
            this.timeout(1000);
            Knex.schema.hasTable('Deployment').then(function(exists){
                expect(exists).to.be.false;
                done();
            });
        });
        it("creates a Deployment table", function(done){
            this.timeout(10000);
            init.createDeploymentTable().then(function(){
                Knex.schema.hasTable('Deployment').then(function(exists){
                    expect(exists).to.be.true;
                }).then(function(){
                    //make sure that the table can be inserted into.  If this fails, the table is not set up right
                    Knex.insert({
                        key: "key",
                        dueDate: new Date(),
                        currentVersion: "4.0.0",
                        version: "5.0.0",
                        components: "somecomponent",
                        deploymentMajorAffects: "4.0"
                    }).into("Deployment").catch(function(err){
                        expect(err).to.not.exist;
                    }).then(done());
                });
            });
        });
        it('ensures the Deployment table exists', function(done){
            this.timeout(10000);
            init.ensureDeploymentTable({fill: false}).then(function(value){
                expect(value).to.not.be.true;
            }).then(function(){
                init.ensureDeploymentTable({fill: false}).then(function(secondValue){
                    expect(secondValue).to.be.true;
                    done();
                });
            });
        });
    });    
});

however, I have a separate test that deals with the Deployment table as well. Especially since I load a very long string of data into the test database.

function readyDeploymentTable(){
    var Promise = require('bluebird');
    return Knex.schema.dropTableIfExists("Deployment")
    .then(function(){
        return services.InitializeDBService.createDeploymentTable();
    })
    .then(function(){
        return Promise.map(dataDump(), function(deployment){
            return Knex.insert(deployment).into('Deployment').then();
        }).all();

    });
}


describe('DeploymentDatapointsService', function(){
    before(function(){
        this.timeout(20000);
        readyDeploymentTable().then(function(){
            done();
        });
    })

    describe('getDeploymentDatapoints()', function(){
        it('should succeed', function(){
            expect(true).to.be.true;
        });
    });
});


//a full deployment query
function dataDump(){
    return [{"big set of entries": stuff}]
}

How do I reconcile this? Am I not using Knex securely? Is there a way to avoid these blocking conditions that completely stop my tests? Should I be running a synchronous test suite if this is unavoidable?

Aucun commentaire:

Enregistrer un commentaire