Skip to content

Commit

Permalink
Merge pull request #1002 from forcedotcom/sm/tests-for-fileLocking
Browse files Browse the repository at this point in the history
test: ut for fileLocking
  • Loading branch information
mshanemc authored Dec 5, 2023
2 parents 83864b3 + 8cedbe2 commit 32e6641
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions test/unit/util/fileLockingTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { join } from 'node:path';
import * as fs from 'node:fs';
import { tmpdir } from 'node:os';
import { expect } from 'chai';
import { lockInit, lockInitSync } from '../../../src/util/fileLocking';
import { uniqid } from '../../../src/util/uniqid';

describe('fileLocking', () => {
describe('lockInit', () => {
const targetFolder = join(tmpdir(), uniqid('fileLockingTest'));
const file1 = join(targetFolder, 'file1.txt');

it('should create a dir and lock the directory if the file does not exist', async () => {
const lock = await lockInit(file1);
await lock.unlock();
expect(fs.existsSync(targetFolder)).to.be.true;
expect(fs.existsSync(file1)).to.be.false;
});
it('should write to locked file that does not exist', async () => {
const lock = await lockInit(file1);
await lock.writeAndUnlock('hey');
expect(fs.existsSync(targetFolder)).to.be.true;
expect(fs.readFileSync(file1, 'utf8')).to.equal('hey');
});

it('should lock the file if it exists', async () => {
const lock = await lockInit(file1);
await lock.unlock();
expect(fs.readFileSync(file1, 'utf8')).to.equal('hey');
});

it('should write to locked file that does exist', async () => {
const lock = await lockInit(file1);
await lock.writeAndUnlock('hey, you');
expect(fs.readFileSync(file1, 'utf8')).to.equal('hey, you');
});
});

describe('lockInitSync', () => {
const targetFolder = join(tmpdir(), uniqid('fileLockingTestSync'));
const file2 = join(targetFolder, 'file2.txt');

it('should create a dir and lock the directory if the file does not exist', () => {
const lock = lockInitSync(file2);
lock.unlock();
expect(fs.existsSync(targetFolder)).to.be.true;
expect(fs.existsSync(file2)).to.be.false;
});
it('should write to locked file that does not exist', () => {
const lock = lockInitSync(file2);
lock.writeAndUnlock('hey');
expect(fs.readFileSync(file2, 'utf8')).to.equal('hey');
});

it('should lock the file if it exists', () => {
const lock = lockInitSync(file2);
lock.unlock();
expect(fs.readFileSync(file2, 'utf8')).to.equal('hey');
});

it('should write to locked file that does exist', () => {
const lock = lockInitSync(file2);
lock.writeAndUnlock('hey, you');
expect(fs.readFileSync(file2, 'utf8')).to.equal('hey, you');
});
});

describe('validate lock behavior', () => {
const targetFolder = join(tmpdir(), uniqid('fileLockingTestConcurrency'));
describe('async', () => {
it('waits for a lock using writeAndUnlock', async () => {
const file3 = join(targetFolder, 'file3.txt');
const lock = await lockInit(file3);
const [lock2] = await Promise.all([lockInit(file3), lock.writeAndUnlock('hey')]);
await lock2.writeAndUnlock('hey, you');
expect(fs.readFileSync(file3, 'utf8')).to.equal('hey, you');
});
it('waits for a lock using unlock', async () => {
const file4 = join(targetFolder, 'file3.txt');
fs.writeFileSync(file4, 'hey, buddy');
const lock = await lockInit(file4);
const [lock2] = await Promise.all([lockInit(file4), lock.unlock()]);
await lock2.unlock();
expect(fs.readFileSync(file4, 'utf8')).to.equal('hey, buddy');
});
});
});
});

0 comments on commit 32e6641

Please sign in to comment.