Skip to content

Commit

Permalink
Test more concurrent compilations
Browse files Browse the repository at this point in the history
  • Loading branch information
jerivas committed Nov 21, 2023
1 parent 032e045 commit 36db21b
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions js-api-spec/compiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {sandbox} from './sandbox';
import {spy, URL} from './utils';

const functions = {'foo($args)': (args: unknown) => new SassString(`${args}`)};

const importers = [
{
canonicalize: (url: string) => new URL(`u:${url}`),
Expand All @@ -18,15 +19,29 @@ const importers = [
}),
},
];

const asyncImporters = [
{
canonicalize: (url: string) =>
Promise.resolve(importers[0].canonicalize(url)),
load: (url: typeof URL) => Promise.resolve(importers[0].load(url)),
},
];

const getLogger = () => ({debug: spy(() => {})});

/* Sort the output of the example CSS so it can be compared */
const sortCompiled = (a: string, b: string) => {
const aMatch = a.match(/value: (\d+);/);
const bMatch = b.match(/value: (\d+);/);
if (!aMatch || !bMatch) {
throw new Error(
`Failed to parse ${a} or ${b} as numbers to determine sort order`
);
}
return Number(aMatch[1]) - Number(bMatch[1]);
};

describe('Compiler', () => {
let compiler: Compiler;

Expand Down Expand Up @@ -84,6 +99,7 @@ describe('Compiler', () => {

describe('AsyncCompiler', () => {
let compiler: AsyncCompiler;
const runs = 1000; // Number of concurrent compilations to run

beforeEach(async () => {
compiler = await initAsyncCompiler();
Expand All @@ -96,7 +112,7 @@ describe('AsyncCompiler', () => {
describe('compileStringAsync', () => {
it('handles multiple concurrent compilations', async () => {
const logger = getLogger();
const compilations = Array(5)
const compilations = Array(runs)
.fill(0)
.map((_, i) =>
compiler.compileStringAsync(
Expand All @@ -106,13 +122,13 @@ describe('AsyncCompiler', () => {
);
Array.from(await Promise.all(compilations))
.map((result: CompileResult) => result.css)
.sort()
.sort(sortCompiled)
.forEach((result, i) => {
expect(result).toEqualIgnoringWhitespace(
`.import {value: ${i};} .fn {value: "${i}";}`
);
});
expect(logger.debug).toHaveBeenCalledTimes(compilations.length);
expect(logger.debug).toHaveBeenCalledTimes(runs);
});

it('throws after being disposed', async () => {
Expand All @@ -133,7 +149,7 @@ describe('AsyncCompiler', () => {
it('handles multiple concurrent compilations', () =>
sandbox(async dir => {
const logger = getLogger();
const compilations = Array(5)
const compilations = Array(runs)
.fill(0)
.map((_, i) => {
const filename = `input-${i}.scss`;
Expand All @@ -148,13 +164,13 @@ describe('AsyncCompiler', () => {
});
Array.from(await Promise.all(compilations))
.map((result: CompileResult) => result.css)
.sort()
.sort(sortCompiled)
.forEach((result, i) => {
expect(result).toEqualIgnoringWhitespace(
`.import {value: ${i};} .fn {value: "${i}";}`
);
});
expect(logger.debug).toHaveBeenCalledTimes(compilations.length);
expect(logger.debug).toHaveBeenCalledTimes(runs);
}));

it('throws after being disposed', async () => {
Expand Down

0 comments on commit 36db21b

Please sign in to comment.