Skip to content

chore(shared-ini-file-loader): use Promises in slurpFile hash #3546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 40 additions & 19 deletions packages/shared-ini-file-loader/src/slurpFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,56 @@ describe("slurpFile", () => {
const getMockFileContents = (path: string, options = UTF8) => JSON.stringify({ path, options });

beforeEach(() => {
(promises.readFile as jest.Mock).mockImplementation((path, options) =>
Promise.resolve(getMockFileContents(path, options))
);
(promises.readFile as jest.Mock).mockImplementation(async (path, options) => {
await new Promise((resolve) => setTimeout(resolve, 100));
return getMockFileContents(path, options);
});
});

afterEach(() => {
jest.clearAllMocks();
});

it("makes one readFile call for a filepath irrepsective of slurpFile calls", (done) => {
jest.isolateModules(async () => {
const { slurpFile } = require("./slurpFile");
const mockPath = "/mock/path";
const mockPathContent = getMockFileContents(mockPath);
describe("makes one readFile call for a filepath irrespective of slurpFile calls", () => {
// @ts-ignore: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/34617
it.each([10, 100, 1000, 10000])("parallel calls: %d ", (num: number, done: Function) => {
jest.isolateModules(async () => {
const { slurpFile } = require("./slurpFile");
const mockPath = "/mock/path";
const mockPathContent = getMockFileContents(mockPath);

expect(promises.readFile).not.toHaveBeenCalled();
const fileContentArr = await Promise.all(Array(num).fill(slurpFile(mockPath)));
expect(fileContentArr).toStrictEqual(Array(num).fill(mockPathContent));

// There is one readFile call even through slurpFile is called in parallel num times.
expect(promises.readFile).toHaveBeenCalledTimes(1);
expect(promises.readFile).toHaveBeenCalledWith(mockPath, UTF8);
done();
});
});

expect(promises.readFile).not.toHaveBeenCalled();
const fileContentArr = await Promise.all([slurpFile(mockPath), slurpFile(mockPath)]);
expect(fileContentArr).toStrictEqual([mockPathContent, mockPathContent]);
it("two parallel calls and one sequential call", (done) => {
jest.isolateModules(async () => {
const { slurpFile } = require("./slurpFile");
const mockPath = "/mock/path";
const mockPathContent = getMockFileContents(mockPath);

// There is one readFile call even through slurpFile is called in parallel twice.
expect(promises.readFile).toHaveBeenCalledTimes(1);
expect(promises.readFile).toHaveBeenCalledWith(mockPath, UTF8);
expect(promises.readFile).not.toHaveBeenCalled();
const fileContentArr = await Promise.all([slurpFile(mockPath), slurpFile(mockPath)]);
expect(fileContentArr).toStrictEqual([mockPathContent, mockPathContent]);

const fileContent = await slurpFile(mockPath);
expect(fileContent).toStrictEqual(mockPathContent);
// There is one readFile call even through slurpFile is called in parallel twice.
expect(promises.readFile).toHaveBeenCalledTimes(1);
expect(promises.readFile).toHaveBeenCalledWith(mockPath, UTF8);

// There is one readFile call even through slurpFile is called for the third time.
expect(promises.readFile).toHaveBeenCalledTimes(1);
done();
const fileContent = await slurpFile(mockPath);
expect(fileContent).toStrictEqual(mockPathContent);

// There is one readFile call even through slurpFile is called for the third time.
expect(promises.readFile).toHaveBeenCalledTimes(1);
done();
});
});
});

Expand Down
48 changes: 7 additions & 41 deletions packages/shared-ini-file-loader/src/slurpFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,11 @@ import { promises as fsPromises } from "fs";

const { readFile } = fsPromises;

type callbacks = { resolve: Function; reject: Function };
type FileStatus = {
contents: string;
isReading: boolean;
requestQueue: callbacks[];
};

const fileStatusHash: { [key: string]: FileStatus } = {};
const filePromisesHash: { [key: string]: Promise<string> } = {};

export const slurpFile = (path: string) =>
new Promise<string>((resolve, reject) => {
if (!fileStatusHash[path]) {
// File not read yet, set file isReading to true and read file.
fileStatusHash[path] = { isReading: true, contents: "", requestQueue: [] };
fileStatusHash[path].requestQueue.push({ resolve, reject });
readFile(path, "utf8")
.then((data) => {
// File read successful
fileStatusHash[path].isReading = false;
fileStatusHash[path].contents = data;
const { requestQueue } = fileStatusHash[path];
while (requestQueue.length) {
const { resolve } = requestQueue.pop()!;
resolve(data);
}
})
.catch((err) => {
// File read failed;
fileStatusHash[path].isReading = false;
const { requestQueue } = fileStatusHash[path];
while (requestQueue.length) {
const { reject } = requestQueue.pop()!;
reject(err);
}
});
} else if (fileStatusHash[path].isReading) {
// File currently being read. Add callbacks to the request queue.
fileStatusHash[path].requestQueue.push({ resolve, reject });
} else {
resolve(fileStatusHash[path].contents);
}
});
export const slurpFile = (path: string) => {
if (!filePromisesHash[path]) {
filePromisesHash[path] = readFile(path, "utf8");
}
return filePromisesHash[path];
};