Skip to content

Commit

Permalink
add test to ensure that temp directory is created and removed
Browse files Browse the repository at this point in the history
  • Loading branch information
W-A-James committed Dec 5, 2024
1 parent a9a0893 commit 60f3262
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions packages/bson-bench/test/unit/task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,71 @@ describe('Task', function () {
expect(maybeError).to.be.instanceOf(Error);
expect(maybeError).to.have.property('message', 'failed to serialize input object');
});

it('deletes the temp directory', async function () {
const task = new Task({
documentPath: 'test/documents/array.json',
library: 'bson@5',
operation: 'deserialize',
warmup: 100,
iterations: 100,
options: {}
});

// bson throws error when passed array as top-level input
const maybeError = await task.run().catch(e => e);

expect(maybeError).to.be.instanceOf(Error);
expect(maybeError).to.have.property('message', 'failed to serialize input object');

const tmpdirExists = await exists(Task.packageInstallLocation);
expect(tmpdirExists).to.be.false;
});
});

it('creates a temp directory for packages', async function () {
const task = new Task({
documentPath: 'test/documents/long_largeArray.json',
library: 'bson@5',
operation: 'deserialize',
warmup: 100,
iterations: 10000,
options: {}
});

const checkForDirectory = async () => {
for (let i = 0; i < 10; i++) {
if (await exists(Task.packageInstallLocation)) return true;
}
return false;
};
const taskRunPromise = task.run().catch(e => e);

const result = await Promise.race([checkForDirectory(), taskRunPromise]);
expect(typeof result).to.equal('boolean');
expect(result).to.be.true;

const taskRunResult = await taskRunPromise;
expect(taskRunResult).to.not.be.instanceOf(Error);
});

context('after completing successfully', function () {
it('deletes the temp directory', async function () {
const task = new Task({
documentPath: 'test/documents/long_largeArray.json',
library: 'bson@5',
operation: 'deserialize',
warmup: 100,
iterations: 100,
options: {}
});

const maybeError = await task.run().catch(e => e);
expect(maybeError).to.not.be.instanceOf(Error);

const tmpdirExists = await exists(Task.packageInstallLocation);
expect(tmpdirExists).to.be.false;
});
});
});

Expand Down

0 comments on commit 60f3262

Please sign in to comment.