Skip to content
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

infra: attempt to identify and reduce CI flakiness #2644

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
49 changes: 37 additions & 12 deletions packages/e2e-test-kit/src/cli-test-kit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { fork, spawnSync, ChildProcess } from 'child_process';
import { on } from 'events';
import type { Readable } from 'stream';
import { sleep } from 'promise-assist';

type ActionResponse = void | { sleep?: number };

Expand Down Expand Up @@ -41,16 +42,11 @@ export function createCliTester() {
const found: { message: string; time: number }[] = [];
const startTime = Date.now();

return Promise.race([
onTimeout(timeout, () => new Error(`${JSON.stringify(found, null, 3)}\n\n${output()}`)),
return timeoutPromise(
runSteps(),
]);

function onTimeout(ms: number, rejectWith?: () => unknown) {
return new Promise<{ output(): string }>((resolve, reject) =>
setTimeout(() => (rejectWith ? reject(rejectWith()) : resolve({ output })), ms)
);
}
timeout,
() => `${JSON.stringify(found, null, 3)}\n\n${output()}`
);

async function runSteps() {
for await (const line of readLines(process.stdout!)) {
Expand All @@ -63,10 +59,10 @@ export function createCliTester() {
});

if (step.action) {
const { sleep } = (await step.action()) || {};
const { sleep: sleepMs } = (await step.action()) || {};

if (typeof sleep === 'number') {
await onTimeout(sleep);
if (typeof sleepMs === 'number') {
await sleep(sleepMs);
}
}

Expand All @@ -90,6 +86,35 @@ export function createCliTester() {
};
}

export function timeoutPromise<T>(
originalPromise: Promise<T>,
ms: number,
timeoutMessage: string | (() => string) = `timed out after ${ms}ms`
): Promise<T> {
return new Promise((resolve, reject) => {
const timerId = setTimeout(
() =>
reject(
new Error(
typeof timeoutMessage === 'function' ? timeoutMessage() : timeoutMessage
)
),
ms
);

originalPromise.then(
(resolvedValue) => {
clearTimeout(timerId);
resolve(resolvedValue);
},
(rejectReason) => {
clearTimeout(timerId);
reject(rejectReason);
}
);
});
}

async function* readLines(readable: Readable) {
let buffer = '';
for await (const e of on(readable, 'data')) {
Expand Down
131 changes: 67 additions & 64 deletions packages/webpack-plugin/test/e2e/watched-project.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,73 +9,76 @@ const project = 'watched-project';
const projectDir = dirname(
require.resolve(`@stylable/webpack-plugin/test/e2e/projects/${project}/webpack.config`)
);

describe(`(${project})`, () => {
const projectRunner = StylableProjectRunner.mochaSetup(
{
projectDir,
launchOptions: {
// headless: false
let i = 50;
while (--i > 0) {
describe(`(${project})`, () => {
const projectRunner = StylableProjectRunner.mochaSetup(
{
projectDir,
launchOptions: {
// headless: false
},
watchMode: true,
useTempDir: true,
log: true,
},
watchMode: true,
useTempDir: true,
},
before,
afterEach,
after
);
it('renders css', async () => {
const { page } = await projectRunner.openInBrowser();
const styleElements = await page.evaluate(browserFunctions.getStyleElementsMetadata, {
includeCSSContent: true,
});
before,
afterEach,
after
);
it('renders css', async () => {
const { page } = await projectRunner.openInBrowser();
const styleElements = await page.evaluate(browserFunctions.getStyleElementsMetadata, {
includeCSSContent: true,
});

expect(styleElements[0]).to.include({
id: './src/index.st.css',
depth: '3',
});
expect(styleElements[0]).to.include({
id: './src/index.st.css',
depth: '3',
});

expect(styleElements[0].css!.replace(/\s\s*/gm, ' ').trim()).to.match(
/\.index\d+__root \{ color: red; font-size: 3em; z-index: 1; \}/
);
expect(styleElements[0].css!.replace(/\s\s*/gm, ' ').trim()).to.match(
/\.index\d+__root \{ color: red; font-size: 3em; z-index: 1; \}/
);

await projectRunner.actAndWaitForRecompile(
'invalidate dependency',
() => {
return writeFile(
join(projectRunner.testDir, 'src', 'mixin-b.st.css'),
'.b{ color: green; }'
);
},
async () => {
const { page } = await projectRunner.openInBrowser();
const styleElements = await page.evaluate(
browserFunctions.getStyleElementsMetadata,
{
includeCSSContent: true,
}
);
expect(styleElements[0].css!.replace(/\s\s*/gm, ' ').trim()).to.match(
/\.index\d+__root \{ color: green; font-size: 3em; z-index: 1; \}/
);
}
);
});
await projectRunner.actAndWaitForRecompile(
'invalidate dependency',
() => {
return writeFile(
join(projectRunner.testDir, 'src', 'mixin-b.st.css'),
'.b{ color: green; }'
);
},
async () => {
const { page } = await projectRunner.openInBrowser();
const styleElements = await page.evaluate(
browserFunctions.getStyleElementsMetadata,
{
includeCSSContent: true,
}
);
expect(styleElements[0].css!.replace(/\s\s*/gm, ' ').trim()).to.match(
/\.index\d+__root \{ color: green; font-size: 3em; z-index: 1; \}/
);
}
);
});

it('allow stylable imports with missing files', async () => {
await projectRunner.actAndWaitForRecompile(
'rename files with invalid dependencies',
() => {
return rename(
join(projectRunner.testDir, 'src', 'index.st.css'),
join(projectRunner.testDir, 'src', 'xxx.st.css')
);
},
() => {
// if we got here we finished to recompile with the missing file.
// when this test is broken the compiler had en error and exit the process.
expect('finish recompile');
}
);
it('allow stylable imports with missing files', async () => {
await projectRunner.actAndWaitForRecompile(
'rename files with invalid dependencies',
() => {
return rename(
join(projectRunner.testDir, 'src', 'index.st.css'),
join(projectRunner.testDir, 'src', 'xxx.st.css')
);
},
() => {
// if we got here we finished to recompile with the missing file.
// when this test is broken the compiler had en error and exit the process.
expect('finish recompile');
}
);
});
});
});
}