Skip to content

Commit

Permalink
Merge pull request #557 from golemfactory/master
Browse files Browse the repository at this point in the history
Sync beta with master
  • Loading branch information
grisha87 authored Aug 22, 2023
2 parents 7371bcd + 4fd34ba commit 567f1ae
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 96 deletions.
16 changes: 8 additions & 8 deletions .docs/summary-generator.cjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const path = require("path");
const fs = require("fs");
const docsDir = "docs";
const directoryPath = path.join(__dirname, "..", docsDir);

async function prepareDocAnchor(type, file) {
const filePath = path.join(directoryPath, type, file);
const fileContent = await fs.readFileSync(filePath, "utf-8");
async function prepareDocAnchor(docsDir, type, file) {
const filePath = path.join(docsDir, type, file);
const fileContent = fs.readFileSync(filePath, "utf-8");
const firstLine = (fileContent.match(/(^.*)/) || [])[1] || "";
return {
link: `${docsDir}/${type}/${file}`,
link: `${type}/${file.replace(".md", "")}`,
title: firstLine.substring(firstLine.indexOf(":") + 2),
};
}
Expand All @@ -18,7 +16,9 @@ function capitalizeFirstLetter(string) {
}

(async () => {
const logStream = fs.createWriteStream(path.join(directoryPath, ".SUMMARY.md"), { flags: "w" });
const docsDir = process.argv[2] || "docs";
const directoryPath = path.join(__dirname, "..", docsDir);
const logStream = fs.createWriteStream(path.join(process.argv[3] || docsDir, "overview.md"), { flags: "w" });

const types = fs
.readdirSync(directoryPath, { withFileTypes: true })
Expand All @@ -31,7 +31,7 @@ function capitalizeFirstLetter(string) {
const files = fs.readdirSync(path.join(directoryPath, type), { withFileTypes: true }).map((f) => f.name);

for (let file of files) {
const doc = await prepareDocAnchor(type, file);
const doc = await prepareDocAnchor(docsDir, type, file);
logStream.write(`\n\t* [${doc.title}](${doc.link})`);
}
}
Expand Down
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run format:check
npm run format:check && npm run lint
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"test:e2e": "jest --config tests/e2e/jest.config.json tests/e2e/**.spec.ts --runInBand --forceExit",
"test:e2e:no-goth": "jest tests/e2e/**.spec.ts --testTimeout=180000 --runInBand --forceExit",
"test:cypress": "cypress run",
"lint": "eslint .",
"lint": "npm run lint:ts && npm run lint:ts:tests && npm run lint:eslint",
"lint:ts": "tsc --project tsconfig.json --noEmit",
"lint:ts:tests": "tsc --project tests/tsconfig.json --noEmit",
"lint:eslint": "eslint .",
"format": "prettier -w .",
"format:check": "prettier -c .",
"prepare": "husky install"
Expand Down Expand Up @@ -73,7 +76,7 @@
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"buffer": "^6.0.3",
"cypress": "12.17.3",
"cypress": "12.17.4",
"eslint": "~8.47.0",
"eslint-config-prettier": "^9.0.0",
"husky": "^8.0.3",
Expand Down
13 changes: 7 additions & 6 deletions src/task/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,19 @@ export class Batch {
return this;
}

/**
* Executes the batch of commands added via {@link run} returning result for each of the steps.
*
* In case any of the commands will fail, the execution of the batch will be interrupted by the Provider.
*/
async end(): Promise<Result[]> {
await this.script.before();
await sleep(100, true);
const results = await this.activity.execute(this.script.getExeScriptRequest());
const allResults: Result[] = [];
return new Promise((resolve, reject) => {
results.on("data", (result) => {
allResults.push(result);
if (result.result === "Error") {
this.script.after(allResults).catch();
return reject(`Error: ${result.message}`);
}
results.on("data", (res) => {
allResults.push(res);
});

results.on("end", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/task/work.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { NetworkNode } from "../network";
export type Worker<InputType = unknown, OutputType = unknown> = (
ctx: WorkContext,
data?: InputType,
) => Promise<OutputType | undefined>;
) => Promise<OutputType>;

const DEFAULTS = {
activityPreparingTimeout: 300_000,
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/strategies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("Strategies", function () {
logger,
});
const data = ["one", "two", "three"];
const results = executor.map<string, string>(data, async (ctx, x) => {
const results = executor.map<string, string | undefined>(data, async (ctx, x) => {
const res = await ctx.run(`echo "${x}"`);
return res.stdout?.trim();
});
Expand All @@ -36,7 +36,7 @@ describe("Strategies", function () {
logger,
});
const data = ["one", "two", "three"];
const results = executor.map<string, string>(data, async (ctx, x) => {
const results = executor.map<string, string | undefined>(data, async (ctx, x) => {
const res = await ctx.run(`echo "${x}"`);
return res.stdout?.trim();
});
Expand All @@ -58,7 +58,7 @@ describe("Strategies", function () {
logger,
});
const data = ["one", "two"];
const results = executor.map<string, string>(data, async (ctx, x) => {
const results = executor.map<string, string | undefined>(data, async (ctx, x) => {
const res = await ctx.run(`echo "${x}"`);
return res.stdout?.trim();
});
Expand All @@ -76,7 +76,7 @@ describe("Strategies", function () {
logger,
});
const data = ["one", "two"];
const results = executor.map<string, string>(data, async (ctx, x) => {
const results = executor.map<string, string | undefined>(data, async (ctx, x) => {
const res = await ctx.run(`echo "${x}"`);
return res.stdout?.trim();
});
Expand Down
49 changes: 1 addition & 48 deletions tests/e2e/tasks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe("Task Executor", function () {
logger,
});
const data = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"];
const results = executor.map<string, string>(data, async (ctx, x) => {
const results = executor.map<string, string | undefined>(data, async (ctx, x) => {
const res = await ctx.run(`echo "${x}"`);
return res.stdout?.trim();
});
Expand Down Expand Up @@ -116,31 +116,6 @@ describe("Task Executor", function () {
expect(onEnd).toEqual("END");
});

it("should run simple batch script and catch error on stream", async () => {
executor = await TaskExecutor.create({
package: "9a3b5d67b0b27746283cb5f287c13eab1beaa12d92a9f536b747c7ae",
logger,
});
const outputs: string[] = [];
let expectedError = "";
await executor
.run(async (ctx) => {
const results = await ctx.beginBatch().run('echo "Hello Golem"').run("invalid_command").endStream();
results.on("data", ({ stdout }) => outputs.push(stdout?.trim()));
results.on("error", (error) => {
expectedError = error.toString();
});
})
.catch((e) => {
expect(e).toBeUndefined();
});
await logger.expectToInclude("Task 1 computed by provider", 5000);
expect(outputs[0]).toEqual("Hello Golem");
expect(expectedError).toEqual(
"Error: ExeScript command exited with code 127. Stdout: undefined. Stderr: sh: 1: invalid_command: not found",
);
});

it("should run simple batch script and get results as promise", async () => {
executor = await TaskExecutor.create({
package: "9a3b5d67b0b27746283cb5f287c13eab1beaa12d92a9f536b747c7ae",
Expand All @@ -165,28 +140,6 @@ describe("Task Executor", function () {
expect(outputs[2]).toEqual("OK");
});

it("should run simple batch script and catch error on promise", async () => {
executor = await TaskExecutor.create({
package: "9a3b5d67b0b27746283cb5f287c13eab1beaa12d92a9f536b747c7ae",
logger,
});
let results;
let error;
await executor
.run(async (ctx) => {
results = await ctx
.beginBatch()
.run('echo "Hello Golem"')
.run("invalid_command")
.end()
.catch((err) => (error = err));
})
.catch((e) => {
expect(e).toBeUndefined();
});
expect(error).toEqual("Error: ExeScript command exited with code 127");
});

it("should run transfer file", async () => {
executor = await TaskExecutor.create({
package: "9a3b5d67b0b27746283cb5f287c13eab1beaa12d92a9f536b747c7ae",
Expand Down
4 changes: 3 additions & 1 deletion tests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
"extends": "../tsconfig.json",
"include": ["."],
"exclude": ["./cypress"],
"types": ["jest"]
"compilerOptions": {
"types": ["jest"]
}
}
42 changes: 17 additions & 25 deletions tests/unit/work.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("Work Context", () => {
describe("Executing", () => {
it("should execute run command", async () => {
const activity = await Activity.create("test_agreement_id");
const worker: Worker<null, Result> = async (ctx) => ctx.run("some_shell_command");
const worker: Worker<void, Result> = async (ctx) => ctx.run("some_shell_command");
const ctx = new WorkContext(activity, { logger, activityStateCheckingInterval: 10, isRunning });
await ctx.before();
const results = await worker(ctx);
Expand All @@ -25,7 +25,7 @@ describe("Work Context", () => {

it("should execute upload file command", async () => {
const activity = await Activity.create("test_agreement_id");
const worker: Worker<null, Result> = async (ctx) => ctx.uploadFile("./file.txt", "/golem/file.txt");
const worker: Worker<void, Result> = async (ctx) => ctx.uploadFile("./file.txt", "/golem/file.txt");
const ctx = new WorkContext(activity, {
logger,
activityStateCheckingInterval: 10,
Expand All @@ -40,7 +40,7 @@ describe("Work Context", () => {

it("should execute upload json command", async () => {
const activity = await Activity.create("test_agreement_id");
const worker: Worker<null, Result> = async (ctx) => ctx.uploadJson({ test: true }, "/golem/file.txt");
const worker: Worker<void, Result> = async (ctx) => ctx.uploadJson({ test: true }, "/golem/file.txt");
const ctx = new WorkContext(activity, {
logger,
activityStateCheckingInterval: 10,
Expand All @@ -55,7 +55,7 @@ describe("Work Context", () => {

it("should execute download file command", async () => {
const activity = await Activity.create("test_agreement_id");
const worker: Worker<null, Result> = async (ctx) => ctx.downloadFile("/golem/file.txt", "./file.txt");
const worker: Worker<void, Result> = async (ctx) => ctx.downloadFile("/golem/file.txt", "./file.txt");
const ctx = new WorkContext(activity, {
logger,
activityStateCheckingInterval: 10,
Expand All @@ -71,7 +71,7 @@ describe("Work Context", () => {
describe("Batch", () => {
it("should execute batch as promise", async () => {
const activity = await Activity.create("test_agreement_id");
const worker: Worker<null, Result[]> = async (ctx) => {
const worker: Worker<void, Result[]> = async (ctx) => {
return ctx
.beginBatch()
.run("some_shell_command")
Expand Down Expand Up @@ -102,7 +102,7 @@ describe("Work Context", () => {

it("should execute batch as stream", async () => {
const activity = await Activity.create("test_agreement_id");
const worker: Worker<null, Readable> = async (ctx) => {
const worker: Worker<void, Readable> = async (ctx) => {
return ctx
.beginBatch()
.run("some_shell_command")
Expand Down Expand Up @@ -141,9 +141,9 @@ describe("Work Context", () => {
});
});
describe("Error handling", () => {
it("should catch error while executing batch as promise with invalid command", async () => {
it("should return a result with error in case the command to execute is invalid", async () => {
const activity = await Activity.create("test_agreement_id");
const worker: Worker<null, Result[]> = async (ctx) => ctx.beginBatch().run("invalid_shell_command").end();
const worker: Worker<void, Result[]> = async (ctx) => ctx.beginBatch().run("invalid_shell_command").end();
const ctx = new WorkContext(activity, {
logger,
activityStateCheckingInterval: 10,
Expand All @@ -152,18 +152,16 @@ describe("Work Context", () => {
});
const expectedStdout = [{ result: "Error", stderr: "error", message: "Some error occurred" }];
activityMock.setExpectedExeResults(expectedStdout);
let expectedError;
try {
await worker(ctx);
} catch (err) {
expectedError = err;
}
expect(expectedError).toEqual(`Error: ${expectedStdout[0].message}`);

const [result] = await worker(ctx);

expect(result.result).toEqual("Error");
expect(result.message).toEqual("Some error occurred");
});

it("should catch error while executing batch as stream with invalid command", async () => {
const activity = await Activity.create("test_agreement_id");
const worker: Worker<null, Readable> = async (ctx) => ctx.beginBatch().run("invalid_shell_command").endStream();
const worker: Worker<void, Readable> = async (ctx) => ctx.beginBatch().run("invalid_shell_command").endStream();
const ctx = new WorkContext(activity, {
logger,
activityStateCheckingInterval: 10,
Expand All @@ -173,15 +171,9 @@ describe("Work Context", () => {
const expectedStdout = [{ result: "Error", stderr: "error", message: "Some error occurred" }];
activityMock.setExpectedExeResults(expectedStdout);
const results = await worker(ctx);
await new Promise((res, rej) => {
results?.on("error", (error) => {
try {
expect(error.message).toEqual("Some error occurred. Stdout: test_result. Stderr: error");
} catch (e) {
rej(e);
}
res(true);
});

results.once("error", (error) => {
expect(error.message).toEqual("Some error occurred. Stdout: test_result. Stderr: error");
});
});
});
Expand Down

0 comments on commit 567f1ae

Please sign in to comment.