From 8330bc045def8bd55c188a19da672a42f9674ab5 Mon Sep 17 00:00:00 2001 From: Riadh <22998716+KiKoS0@users.noreply.github.com> Date: Thu, 29 Aug 2024 22:47:45 +0200 Subject: [PATCH] Add tests for try-catching in `run` and `invoke` functions --- .../testfunctions/InvokeFailureFunction.java | 36 +++++++++++ .../NonRetriableErrorFunction.java | 2 +- .../testfunctions/TryCatchRunFunction.java | 36 +++++++++++ .../springbootdemo/DemoTestConfiguration.java | 2 + .../ErrorsInStepsIntegrationTest.java | 2 +- .../StepErrorsIntegrationTest.java | 60 +++++++++++++++++++ 6 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/InvokeFailureFunction.java create mode 100644 inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/TryCatchRunFunction.java create mode 100644 inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/StepErrorsIntegrationTest.java diff --git a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/InvokeFailureFunction.java b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/InvokeFailureFunction.java new file mode 100644 index 00000000..d1c8c951 --- /dev/null +++ b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/InvokeFailureFunction.java @@ -0,0 +1,36 @@ +package com.inngest.springbootdemo.testfunctions; + +import com.inngest.*; +import org.jetbrains.annotations.NotNull; + +import java.util.LinkedHashMap; + +public class InvokeFailureFunction extends InngestFunction { + + @NotNull + @Override + public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) { + return builder + .id("invoke-failure-fn") + .name("Invoke Function") + .triggerEvent("test/invoke.failure"); + } + + @Override + public String execute(FunctionContext ctx, Step step) { + try { + step.invoke( + "failing-function", + "spring_test_demo", + "non-retriable-fn", + new LinkedHashMap(), + null, + Object.class); + } catch (StepError e) { + return e.getMessage(); + } + + return "An error should have been thrown and this message should not be returned"; + } +} diff --git a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/NonRetriableErrorFunction.java b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/NonRetriableErrorFunction.java index b6f9527a..16d5464e 100644 --- a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/NonRetriableErrorFunction.java +++ b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/NonRetriableErrorFunction.java @@ -17,7 +17,7 @@ public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) @Override public String execute(FunctionContext ctx, Step step) { step.run("fail-step", () -> { - throw new NonRetriableError("something fatally went wrong"); + throw new NonRetriableError("Something fatally went wrong"); }, String.class); return "Success"; diff --git a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/TryCatchRunFunction.java b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/TryCatchRunFunction.java new file mode 100644 index 00000000..d76473b2 --- /dev/null +++ b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/TryCatchRunFunction.java @@ -0,0 +1,36 @@ +package com.inngest.springbootdemo.testfunctions; + +import com.inngest.*; +import org.jetbrains.annotations.NotNull; + +class CustomException extends RuntimeException { + public CustomException(String message) { + super(message); + } +} + +public class TryCatchRunFunction extends InngestFunction { + + @NotNull + @Override + public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) { + return builder + .id("try-catch-run-fn") + .name("Try catch run") + .triggerEvent("test/try.catch.run") + .retries(0); + } + + @Override + public String execute(FunctionContext ctx, Step step) { + try { + step.run("fail-step", () -> { + throw new CustomException("Something fatally went wrong"); + }, String.class); + } catch (StepError e) { + return e.getMessage(); + } + + return "An error should have been thrown and this message should not be returned"; + } +} diff --git a/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoTestConfiguration.java b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoTestConfiguration.java index bd0d60f1..3ade1350 100644 --- a/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoTestConfiguration.java +++ b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoTestConfiguration.java @@ -21,6 +21,8 @@ protected HashMap functions() { addInngestFunction(functions, new NonRetriableErrorFunction()); addInngestFunction(functions, new RetriableErrorFunction()); addInngestFunction(functions, new ZeroRetriesFunction()); + addInngestFunction(functions, new InvokeFailureFunction()); + addInngestFunction(functions, new TryCatchRunFunction()); return functions; } diff --git a/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/ErrorsInStepsIntegrationTest.java b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/ErrorsInStepsIntegrationTest.java index b73c1ad5..71ad51f0 100644 --- a/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/ErrorsInStepsIntegrationTest.java +++ b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/ErrorsInStepsIntegrationTest.java @@ -43,7 +43,7 @@ void testNonRetriableShouldFail() throws Exception { assertNotNull(run.getEnded_at()); assert output.get("name").contains("NonRetriableError"); assert output.get("stack").contains("NonRetriableErrorFunction.lambda$execute"); - assertEquals(output.get("message"), "something fatally went wrong"); + assertEquals(output.get("message"), "Something fatally went wrong"); } @Test diff --git a/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/StepErrorsIntegrationTest.java b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/StepErrorsIntegrationTest.java new file mode 100644 index 00000000..b14e1506 --- /dev/null +++ b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/StepErrorsIntegrationTest.java @@ -0,0 +1,60 @@ +package com.inngest.springbootdemo; + +import com.inngest.CommHandler; +import com.inngest.Inngest; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; +import org.springframework.beans.factory.annotation.Autowired; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@IntegrationTest +@Execution(ExecutionMode.CONCURRENT) +class StepErrorsIntegrationTest { + @BeforeAll + static void setup(@Autowired CommHandler handler) { + handler.register("http://localhost:8080"); + } + + @Autowired + private DevServerComponent devServer; + + static int sleepTime = 5000; + + @Autowired + private Inngest client; + + @Test + void testShouldCatchStepErrorWhenInvokeThrows() throws Exception { + String eventId = InngestFunctionTestHelpers.sendEvent(client, "test/invoke.failure").first(); + + Thread.sleep(sleepTime); + + RunEntry run = devServer.runsByEvent(eventId).first(); + String output = (String) run.getOutput(); + + assertEquals("Completed", run.getStatus() ); + assertNotNull(run.getEnded_at()); + + assertEquals("Something fatally went wrong", output); + } + + @Test + void testShouldCatchStepErrorWhenRunThrows() throws Exception { + String eventId = InngestFunctionTestHelpers.sendEvent(client, "test/try.catch.run").first(); + + Thread.sleep(sleepTime); + + RunEntry run = devServer.runsByEvent(eventId).first(); + String output = (String) run.getOutput(); + + assertEquals("Completed", run.getStatus()); + assertNotNull(run.getEnded_at()); + + assertEquals("Something fatally went wrong", output); + } + +}