CImrie / mongomem

In-memory MongoDB Server. Ideal for testing.
58 stars 7 forks source link

MongoDB Process not terminated #4

Closed phkorn closed 5 years ago

phkorn commented 7 years ago

Hi! I have following helper class for my tests using ava:

const mongoose = require('mongoose');
const MongoDBServer = require('mongomem').MongoDBServer;
const UserModel = require('../../models/users');

// MongoDBServer.debug = true;
const serverHasStarted = MongoDBServer.start();

class Util {
    async getMongooseMock() {
        const mongooseInstance = new mongoose.Mongoose();
        mongooseInstance.Promise = Promise;
        await serverHasStarted;
        const db = await MongoDBServer.getConnectionString();
        await mongooseInstance.connect(db);
        mongooseInstance.model('Users', UserModel.schema);
        return mongooseInstance;
    }

    beforeEach(test, func = () => {}) {
        test.beforeEach(async (t) => {
            const db = await this.getMongooseMock();
            const User = db.model('Users');
            await new Promise((resolve, reject) => {
                User.on('index', (error) => {
                    if (error) {
                        return reject(error);
                    }
                    return resolve();
                });
            });
            t.context.db = db;
            func(t);
        });
    }

    afterEach(test) {
        test.afterEach((t) => {
            t.context.db.disconnect();
        });
    }

    after(test) {
        test.after.always('cleanup', () => {
            MongoDBServer.tearDown();
        });
    }
}

module.exports = new Util();

When the teardown in after.always is called the check in your code helper.mongoBin.childProcess.connected is false, so the kill on the childprocess doesn't get called.

If I check on Ubuntu using  top -c -p $(pgrep -d',' -f mongo) the mongo process is still running. If I skip the if-check and call helper.mongoBin.childProcess.kill(); the server get's killed.

Am I doing something wrong?

nodkz commented 7 years ago

You may follow to the new ava recipe: https://github.com/avajs/ava/blob/master/docs/recipes/endpoint-testing-with-mongoose.md

More discussion was here: https://github.com/avajs/ava/pull/1420#issuecomment-314377098

phkorn commented 7 years ago

@nodkz thx for your fast replay, but I will give this repo a shot before switching to yours :wink:

CImrie commented 7 years ago

@k0rn1 I'll try to replicate this in a test on my end and see if we can work to get it resolved. The package doesn't have huge uptake at the moment so it takes longer for little things like this to filter through.

CImrie commented 7 years ago

@k0rn1 I have tried to replicate this but struggled so far. Can you checkout the develop branch and see if that works for you? If not are you able to supply a test case that proves the error exists?

Thanks!

CImrie commented 5 years ago

Closing due to inactivity