CImrie / mongomem

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

Can not remove Temp dir #6

Closed haaohao closed 5 years ago

haaohao commented 6 years ago

Hi. I'm trying to run the test, use 'ava' command, but when call method 'server.tmpFile.removeCallback();', it throw a error as below: [Error: EBUSY: resource busy or locked, unlink 'C:\Users\LIJO9\AppData\Local\Temp\mongomem-268688a5fvz9AA6ck\mongod .lock']

1523706612 1

I don't know why the .lock file can not be deleted, do you have any suggestion? Thanks a lot.

CImrie commented 6 years ago

@haaohao Can you show me the code in test/initial.test.js? It's possible you are tearing down the mongodb server before it has finished operating - otherwise it might be a bug in this library or the tmp library (a dependency of mongomem).

haaohao commented 6 years ago

Hi Cimrie, thanks for you reply. 1 It is your origin project, and I just run the test, but it return the error like the picture I showed you before,The initial.test.js is as below :

import test from 'ava'; import { MongoDBServer as server } from '../src';

let tmpServer = null;

test.before('setup mongo server', async t => { server.debug = true; tmpServer = await server.start(); });

test('random mongod server is started once', async t => { server.tearDown(); t.pass(); });

test('always use same server instance if available', async t => { t.is(tmpServer, await server.start()); });

2 I hava a another question is for my new project witch use the 'mongomem', it is about the model's schema definition, why i use the same 'domainName', can create it twice in the db, where 'domainName' is defined to 'unique : true', the code is as below, do you have any suggetion?

helper.js

let mongoose = require('mongoose'); let mem = require('mongomem');

// MongoDBServer.debug = true; let MongoDBServer = mem.MongoDBServer; MongoDBServer.debug = true; let serverHasStarted = MongoDBServer.start();

let getMongooseMock =async()=>{ const mongooseInstance = new mongoose.Mongoose(); await serverHasStarted;

// MongoDBServer.getConnectionString() returns a new UUID4 on every call. // This allows you to get a unique database whilst utilising one in-memory server. let db = await MongoDBServer.getConnectionString(); await mongooseInstance.connect(db, {promiseLibrary: Promise});

// Re-assign original mongose models to new Mongoose connection Object.keys(mongoose.models).forEach(name => { const model = mongoose.models[name]; mongooseInstance.model(name, model.schema); }); return mongooseInstance; }; exports.getMongooseMock = getMongooseMock;

domain-model.js

'use strict';

let mongoose = require('mongoose'), Schema = mongoose.Schema, domainSchema = new Schema({ domainName: {type: String, unique: true}, remark: String, updateDate: String, updateBy: String }, { collection: 'domain' }); mongoose.model('Domain', domainSchema);

mytest.js

require('../../app/modules/domainComponent/domain-model'); let helper = require('./helper'); let test = require('ava'); let rewire = require('rewire');

let mongooseInstance; let mockDomainModule; test.before('before test', async t => { mongooseInstance = await helper.getMongooseMock(); mockDomainModule = mongooseInstance.model('Domain'); });

test.cb('test..', t =>{ mockDomainModule.create({domainName : 'TEST_ADD'}, function (err, result) { console.log(err + ' : err1'); console.log(result); t.end(); }); }); test.cb('test..', t =>{ mockDomainModule.create({domainName : 'TEST_ADD'}, function (err, result) { console.log(err + ' : err1'); console.log(result); t.end(); }); });

the result after the test is as below, it create two data in db, i think it's wrong, because it should be unique for the field 'domainName', any suggetion?

image

CImrie commented 5 years ago

Hi @haaohao

I know it has been a very long time since I replied to you, so my apologies for that. 😔

Off-topic: I've cleaned up your comment quite a bit - GitHub uses a flavour of Markdown - part of the reason I didn't get around to replying to you was due to the horrible formatting of your message. If you try to use Markdown more effectively you will probably get more replies, as I spent a bit of effort grok'ing your message.

If you are still having this issue - can you check if the MongoDB process is still running in your task manager after that test? I use MacOS myself so it's possible this is due to how Windows works.

For the second question, if you haven't come up with a solution to this can you repost it as clearly as possible as a new issue - it's best to keep things single-threaded so that conversations are easy to manage and reference for anyone coming after you.

Thanks, Connor