Skip to content

Commit

Permalink
test: ut for fileLocking
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Dec 4, 2023
1 parent 838f7f5 commit 8cedbe2
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');
});
});
});
});

2 comments on commit 8cedbe2

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - ubuntu-latest

Benchmark suite Current: 8cedbe2 Previous: 1830bce Ratio
Child logger creation 452426 ops/sec (±2.04%) 466013 ops/sec (±1.92%) 1.03
Logging a string on root logger 772163 ops/sec (±7.54%) 698015 ops/sec (±9.42%) 0.90
Logging an object on root logger 594028 ops/sec (±5.62%) 549253 ops/sec (±5.93%) 0.92
Logging an object with a message on root logger 11711 ops/sec (±199.08%) 16987 ops/sec (±187.75%) 1.45
Logging an object with a redacted prop on root logger 393991 ops/sec (±15.72%) 391346 ops/sec (±16.04%) 0.99
Logging a nested 3-level object on root logger 379532 ops/sec (±5.87%) 357970 ops/sec (±7.43%) 0.94

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - windows-latest

Benchmark suite Current: 8cedbe2 Previous: 1830bce Ratio
Child logger creation 340664 ops/sec (±0.49%) 317059 ops/sec (±0.95%) 0.93
Logging a string on root logger 905397 ops/sec (±8.58%) 732860 ops/sec (±5.63%) 0.81
Logging an object on root logger 632631 ops/sec (±5.95%) 644953 ops/sec (±7.32%) 1.02
Logging an object with a message on root logger 22507 ops/sec (±184.19%) 9208 ops/sec (±190.13%) 0.41
Logging an object with a redacted prop on root logger 470738 ops/sec (±5.51%) 433535 ops/sec (±18.00%) 0.92
Logging a nested 3-level object on root logger 324417 ops/sec (±5.32%) 328572 ops/sec (±4.62%) 1.01

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.