giorgio-zamparelli / mongo-in-memory

in memory mocking engine for mongo db
25 stars 4 forks source link

EBUSY: resource busy or locked when stop server in tests #4

Open StarpTech opened 7 years ago

StarpTech commented 7 years ago
EBUSY: resource busy or locked, unlink 'C:\Users\nk.dusdeu468\Documents\Repositorys\example\news\node_modules\mongo-in-memory\.data-iosi15q\mongod.lock'
      at Object.fs.unlinkSync (fs.js:1066:18)
      at rimrafSync (C:\Users\nk.dusdeu468\Documents\Repositorys\example\news\node_modules\rimraf\rimraf.js:305:17)
      at C:\Users\nk.dusdeu468\Documents\Repositorys\example\news\node_modules\rimraf\rimraf.js:341:5
      at Array.forEach (native)
      at rmkidsSync (C:\Users\nk.dusdeu468\Documents\Repositorys\example\news\node_modules\rimraf\rimraf.js:340:26)
StarpTech commented 7 years ago

I run into the issue when I run multiple tests in serie. The data directories won't be deleted.

smeijer commented 6 years ago

Having this one also; it's a big problem when using it with Wallaby.js. It created 1500 .data directories within a day.

smeijer commented 6 years ago

Not really a solution; but I'm using a custom stop method to cleanup sibling directories that's been left earlier. This to reduce the noise a bit.

const getDataDirs = root => fs.readdirSync(root)
  .filter(f => fs.statSync(path.join(root, f)).isDirectory())
  .filter(d => /.data-[a-z0-9]{7}/.test(d))
  .map(d => path.join(root, d));

const tryDelete = dir => {
  try { rimraf.sync(dir) }
  catch (e) { }
};

async function stopMongo(server) {
  try {
    await server.stop();
  }
  catch (ex) {
    if (ex.code !== 'EBUSY' || ex.syscall !== 'unlink') {
      throw ex;
    }
  }
  finally {
    const root = server.databasePath.substr(0, server.databasePath.length - 13);
    getDataDirs(root).forEach(tryDelete);
  }
}