Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB Process not terminated #4

Closed
phkorn opened this issue Sep 15, 2017 · 5 comments
Closed

MongoDB Process not terminated #4

phkorn opened this issue Sep 15, 2017 · 5 comments

Comments

@phkorn
Copy link

phkorn commented Sep 15, 2017

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
Copy link

nodkz commented Sep 15, 2017

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: avajs/ava#1420 (comment)

@phkorn
Copy link
Author

phkorn commented Sep 15, 2017

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

@CImrie
Copy link
Owner

CImrie commented Sep 15, 2017

@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
Copy link
Owner

CImrie commented Oct 3, 2017

@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
Copy link
Owner

CImrie commented Jan 13, 2019

Closing due to inactivity

@CImrie CImrie closed this as completed Jan 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants