From ffcddc1144ce2c28ef7811bacf6c11baf18c49c1 Mon Sep 17 00:00:00 2001 From: Marcin Gordel Date: Mon, 30 Oct 2023 10:47:20 +0100 Subject: [PATCH] test(work_context): unit and e2e tests for streaming results --- src/task/work.spec.ts | 11 +++++++++++ tests/e2e/tasks.spec.ts | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/task/work.spec.ts b/src/task/work.spec.ts index 362d07de3..11c4a5491 100644 --- a/src/task/work.spec.ts +++ b/src/task/work.spec.ts @@ -46,6 +46,17 @@ describe("Work Context", () => { }); }); + describe("runAndStream()", () => { + it("should execute runAndStream command", async () => { + const expectedResult = ActivityMock.createResult({ stdout: "Ok" }); + activity.mockResults([expectedResult]); + const streamOfResults = await context.runAndStream("rm -rf"); + for await (const result of streamOfResults) { + expect(result).toBe(expectedResult); + } + }); + }); + describe("transfer()", () => { it("should execute transfer command", async () => { const result = ActivityMock.createResult({ stdout: "Ok" }); diff --git a/tests/e2e/tasks.spec.ts b/tests/e2e/tasks.spec.ts index 0d070e86c..9375d0870 100644 --- a/tests/e2e/tasks.spec.ts +++ b/tests/e2e/tasks.spec.ts @@ -180,4 +180,23 @@ describe("Task Executor", function () { const result = await executor.run(async (ctx) => ctx.getIp()); expect(["192.168.0.2", "192.168.0.3"]).toContain(result); }); + + it("should run simple task and get results as stream", async () => { + executor = await TaskExecutor.create({ + package: "golem/alpine:latest", + logger, + }); + const streamOfResults = await executor.run(async (ctx) => ctx.runAndStream("echo 'Hello World'")); + expect(streamOfResults).toBeDefined(); + for await (const result of streamOfResults!) { + expect(result.stdout).toContain("Hello World"); + expect(result.result).toContain("Ok"); + } + expect(logger.logs).toContain("Demand published on the market"); + expect(logger.logs).toContain("New proposal has been received"); + expect(logger.logs).toContain("Proposal has been responded"); + expect(logger.logs).toContain("New proposal added to pool"); + expect(logger.logs).toMatch(/Agreement confirmed by provider/); + expect(logger.logs).toMatch(/Activity .* created/); + }); });