Skip to content

Commit

Permalink
configurable batch size
Browse files Browse the repository at this point in the history
  • Loading branch information
agoldis committed Feb 22, 2023
1 parent d8fb994 commit bf1284c
Show file tree
Hide file tree
Showing 19 changed files with 122 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"configurations": [
{
"name": "Attach",
"port": 9229,
"port": 9777,
"request": "attach",
"skipFiles": ["<node_internals>/**"],
"type": "node"
Expand Down
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@

### Bug Fixes

- add CURRENTS_API_BASE_URL env var ([9635424](https://github.com/currents-dev/cypress-cloud/commit/9635424b54dcea00c4fd0485060b7ec3581b8fc5))
- add CURRENTS_API_URL env var ([9635424](https://github.com/currents-dev/cypress-cloud/commit/9635424b54dcea00c4fd0485060b7ec3581b8fc5))
- capture uploads stdout ([6489278](https://github.com/currents-dev/cypress-cloud/commit/648927803a6a8f43e2d6aff18fcd69c57f7be4f5))
- cypress start commands ([f79e9fd](https://github.com/currents-dev/cypress-cloud/commit/f79e9fd859f9bb8c802dbc17cd132ff5d9941eb8))
- get projectRoot from resolved cypress config ([7ada7c3](https://github.com/currents-dev/cypress-cloud/commit/7ada7c37d95ea04b185a99cc89581b87dfa7ecfa))

### Features

- add CURRENTS_API_BASE_URL to be able to change base URL ([b6b7980](https://github.com/currents-dev/cypress-cloud/commit/b6b798068c9d4afb33979dde091ce90a992b05b2))
- add CURRENTS_API_URL to be able to change base URL ([b6b7980](https://github.com/currents-dev/cypress-cloud/commit/b6b798068c9d4afb33979dde091ce90a992b05b2))
- add initial capture to uploaded results ([b064cfc](https://github.com/currents-dev/cypress-cloud/commit/b064cfc3fd8b46e9a46bb567c5f5439cf78a2964)), closes [#37](https://github.com/currents-dev/cypress-cloud/issues/37) [#23](https://github.com/currents-dev/cypress-cloud/issues/23)
- debug and logging ([34570be](https://github.com/currents-dev/cypress-cloud/commit/34570beac3d82cdf55b4a96631b6ac9810a1eb26))

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = {

Override the default configuration values via environment variables:

- `CURRENTS_API_BASE_URL` - sorry-cypress users - set the URL of your director service
- `CURRENTS_API_URL` - sorry-cypress users - set the URL of your director service
- `CURRENTS_PROJECT_ID` - set the `projectId`
- `CURRENTS_RECORD_KEY` - cloud service record key

Expand Down
2 changes: 1 addition & 1 deletion examples/webapp/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
CURRENTS_PROJECT_ID=
CURRENTS_RECORD_KEY=
CURRENTS_API_BASE_URL=
CURRENTS_API_URL=
2 changes: 1 addition & 1 deletion examples/webapp/currents-runner.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash

export CURRENTS_API_BASE_URL=http://localhost:1234
export CURRENTS_API_URL=http://localhost:1234

npx cypress-cloud --parallel --record --key ${CURRENTS_RECORD_KEY} --ci-build-id $(date +%s)
1 change: 1 addition & 0 deletions examples/webapp/currents.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
batchSize: 5, // how many specs to send in one batch
projectId: !!(process.env.GITHUB_ACTION || process.env.CIRCLE_BRANCH)
? "Ij0RfK"
: "l4zuz8",
Expand Down
80 changes: 58 additions & 22 deletions packages/cypress-cloud/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@ import {
import { findSpecs } from "./lib/specMatcher";
import { CurrentsRunParameters, SummaryResults } from "./types";

import { createInstances, createRun } from "./lib/api/api";
import { CreateInstancePayload } from "./lib/api/types/instance";
import Debug from "debug";
import { createInstance, createMultiInstances, createRun } from "./lib/api/api";
import {
CreateInstancePayload,
InstanceResponseSpecDetails,
} from "./lib/api/types/instance";
import { guessBrowser } from "./lib/browser";
import { getCI } from "./lib/ciProvider";
import { runSpecFileSafe } from "./lib/cypress";
import { isCurrents } from "./lib/env";
import { getGitInfo } from "./lib/git";
import { bold, divider, error, info, spacer, title, warn } from "./lib/log";
import { getPlatformInfo } from "./lib/platform";
import { summaryTable } from "./lib/table";

const debug = Debug("currents:index");

/**
* Run the Cypress tests and return the results.
*
Expand All @@ -35,8 +42,18 @@ import { summaryTable } from "./lib/table";
export async function run(params: CurrentsRunParameters) {
spacer();

const { key, projectId, group, parallel, ciBuildId, tag, testingType } =
params;
const {
key,
projectId,
group,
parallel,
ciBuildId,
tag,
testingType,
batchSize,
} = params;

debug("run params %o", params);

const config = await getConfig(params);
const specPattern = params.spec || config.specPattern;
Expand Down Expand Up @@ -88,13 +105,14 @@ export async function run(params: CurrentsRunParameters) {
specPattern: [specPattern].flat(2),
tags: tag,
testingType,
batchSize,
});

info(
"Params:",
`Tags: ${tag?.join(",") ?? false}; Group: ${group ?? false}; Parallel: ${
parallel ?? false
}`
}; Batch Size: ${batchSize}`
);
info("🎥 Run URL:", bold(run.runUrl));

Expand All @@ -113,7 +131,6 @@ export async function run(params: CurrentsRunParameters) {
params
);

console.log(results);
const testResults = summarizeTestResults(Object.values(results));

divider();
Expand Down Expand Up @@ -145,13 +162,40 @@ async function runTillDone(
const uploadTasks: Promise<any>[] = [];
let hasMore = true;

async function runSpecFile() {
const instances = await createInstances({
runId,
groupId,
machineId,
platform,
});
async function runSpecFiles() {
let instances = {
specs: [] as InstanceResponseSpecDetails[],
claimedInstances: 0,
totalInstances: 0,
};

if (isCurrents() || !!process.env.CURRENTS_BATCHED_ORCHESTRATION) {
debug("Running batched orchestration %d", cypressRunOptions.batchSize);
instances = await createMultiInstances({
runId,
groupId,
machineId,
platform,
batchSize: cypressRunOptions.batchSize,
});
} else {
const response = await createInstance({
runId,
groupId,
machineId,
platform,
});
if (response.spec !== null && response.instanceId !== null) {
instances.specs.push({
spec: response.spec,
instanceId: response.instanceId,
});
}
instances.claimedInstances = response.claimedInstances;
instances.totalInstances = response.totalInstances;
}

debug;

if (instances.specs.length === 0) {
hasMore = false;
Expand Down Expand Up @@ -183,8 +227,6 @@ async function runTillDone(
);
}

console.dir(cypressResult);

instances.specs.forEach((spec) => {
summary[spec.spec] = {
...cypressResult,
Expand Down Expand Up @@ -213,15 +255,9 @@ async function runTillDone(
resetCapture();
}

// bus.on("after", () => console.log("🔥 AFTER"));
// bus.on("after", async () => await runSpecFile());

while (hasMore) {
await runSpecFile();
// await new Promise((resolve) => setTimeout(resolve, 1000));
// uploadTasks.push(runSpecFile());
await runSpecFiles();
}
// waitUntil(() => !hasMore, 0, 1000);

await Promise.allSettled(uploadTasks);
return summary;
Expand Down
2 changes: 1 addition & 1 deletion packages/cypress-cloud/lib/api/__tests__/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const API_BASEURL = "http://localhost:1234";

describe("cloud/api", () => {
beforeAll(() => {
process.env.CURRENTS_API_BASE_URL = API_BASEURL;
process.env.CURRENTS_API_URL = API_BASEURL;
});

describe("createRun", () => {
Expand Down
19 changes: 5 additions & 14 deletions packages/cypress-cloud/lib/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { makeRequest } from "../httpClient";
import {
CreateInstanceCyPayload,
CreateInstancePayload,
CreateInstanceResponse,
CreateInstancesResponse,
Expand Down Expand Up @@ -49,24 +50,14 @@ export const createInstance = async ({
return respone.data;
};

export const createInstances = async ({
runId,
groupId,
machineId,
platform,
}: CreateInstancePayload) => {
export const createMultiInstances = async (data: CreateInstanceCyPayload) => {
const respone = await makeRequest<
CreateInstancesResponse,
CreateInstancePayload
CreateInstanceCyPayload
>({
method: "POST",
url: `runs/${runId}/cy/instances`,
data: {
runId,
groupId,
machineId,
platform,
},
url: `runs/${data.runId}/cy/instances`,
data,
});

return respone.data;
Expand Down
14 changes: 9 additions & 5 deletions packages/cypress-cloud/lib/api/types/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,22 @@ export type CreateInstancePayload = {
platform: Platform;
};

export type CreateInstanceCyPayload = CreateInstancePayload & {
batchSize: number;
};
export type CreateInstanceResponse = {
spec: string | null;
instanceId: string | null;
claimedInstances: number;
totalInstances: number;
};
export type CreateInstancesResponse = {
specs: Array<{
spec: string;
instanceId: string;
}>;

export type InstanceResponseSpecDetails = {
spec: string;
instanceId: string;
};
export type CreateInstancesResponse = {
specs: Array<InstanceResponseSpecDetails>;
claimedInstances: number;
totalInstances: number;
};
Expand Down
5 changes: 3 additions & 2 deletions packages/cypress-cloud/lib/api/types/run.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CiParams, CiProvider } from "cypress-cloud/lib/ciProvider";
import { Platform } from "cypress-cloud/types";
import { CiParams, CiProvider } from "@currents/cypress/lib/ciProvider";
import { Platform } from "@currents/cypress/types";

export type CreateRunPayload = {
ci: {
Expand All @@ -20,6 +20,7 @@ export type CreateRunPayload = {
tags?: string[];
testingType: "e2e" | "component";
timeout?: number;
batchSize: number;
};

export type CloudWarning = {
Expand Down
18 changes: 11 additions & 7 deletions packages/cypress-cloud/lib/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { isObject, omit, pickBy } from "lodash";
import {
CurrentsRunParameters,
StrippedCypressModuleAPIOptions,
TestingType,
} from "../../types";
import { getCurrentsConfig } from "../config";
import { sanitizeAndConvertNestedArgs } from "./parser";
Expand Down Expand Up @@ -100,7 +101,7 @@ export function parseOptions(
...args: Parameters<typeof program.parse>
) {
_program.parse(...args);
debug("parsed CLI options", _program.opts());
debug("parsed CLI flags %o", _program.opts());

const { e2e, component } = _program.opts();
if (e2e && component) {
Expand All @@ -126,6 +127,7 @@ export function getStrippedCypressOptions(
): StrippedCypressModuleAPIOptions {
return pickBy(
omit(params, [
"batchSize",
"projectId",
"record",
"key",
Expand Down Expand Up @@ -176,7 +178,7 @@ const dashed = (v: string) => v.replace(/[A-Z]/g, (m) => "-" + m.toLowerCase());
async function getRunParameters(
cliOptions: ReturnType<typeof program.opts>
): Promise<CurrentsRunParameters> {
const { projectId, recordKey } = await getCurrentsConfig();
const { projectId, recordKey, batchSize } = await getCurrentsConfig();
const key = cliOptions.key ?? process.env.CURRENTS_RECORD_KEY ?? recordKey;

if (!key) {
Expand All @@ -193,19 +195,21 @@ async function getRunParameters(
);
}

const result = omit({ ...cliOptions }, "e2e", "component", "tag");

return {
...result,
const result = {
...omit({ ...cliOptions }, "e2e", "component", "tag"),
config: sanitizeAndConvertNestedArgs(cliOptions.config, "config"),
env: sanitizeAndConvertNestedArgs(cliOptions.env, "env"),
reporterOptions: sanitizeAndConvertNestedArgs(
cliOptions.reporterOptions,
"reporterOptions"
),
tag: cliOptions.tag,
testingType: cliOptions.component ? "component" : "e2e",
testingType: cliOptions.component ? "component" : ("e2e" as TestingType),
key,
projectId: _projectId,
batchSize,
};

debug("parsed run params: %o", result);
return result;
}
21 changes: 16 additions & 5 deletions packages/cypress-cloud/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,32 @@ import Debug from "debug";
import path from "path";
import { CurrentsRunParameters, DetectedBrowser } from "../types";
import { bootCypress } from "./bootstrap";
import { warn } from "./log";
import { getRandomPort } from "./utils";
const debug = Debug("currents:config");

export type CurrentsConfig = { projectId?: string; recordKey?: string };
export type CurrentsConfig = {
projectId?: string;
recordKey?: string;
batchSize: number;
};

export async function getCurrentsConfig(): Promise<CurrentsConfig> {
const configFilePath = await getConfigFilePath();
debug("loading currents config file from '%s'", configFilePath);

let config: CurrentsConfig = {};
const defaultConfig: CurrentsConfig = {
batchSize: 3,
};
try {
config = require(configFilePath);
return config;
const fsConfig = require(configFilePath);
return {
...defaultConfig,
...fsConfig,
};
} catch (e) {
return {};
warn("failed to load currents config file: %s", configFilePath);
return defaultConfig;
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/cypress-cloud/lib/cypress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ export async function runSpecFile(
},
spec,
};
debug("%s: running cypress with options %o", spec, options);
debug("running cypress with options %o", options);
const result = await cypress.run(options);

debug("%s: cypress run result %o", spec, result);
debug("cypress run result %o", result);
return result;
}

Expand Down
Loading

0 comments on commit bf1284c

Please sign in to comment.