Skip to content

Commit

Permalink
Add tests for try-catching in run and invoke functions
Browse files Browse the repository at this point in the history
  • Loading branch information
KiKoS0 committed Aug 30, 2024
1 parent 653679b commit 8330bc0
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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<String,
String>(),
null,
Object.class);
} catch (StepError e) {
return e.getMessage();
}

return "An error should have been thrown and this message should not be returned";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ protected HashMap<String, InngestFunction> functions() {
addInngestFunction(functions, new NonRetriableErrorFunction());
addInngestFunction(functions, new RetriableErrorFunction());
addInngestFunction(functions, new ZeroRetriesFunction());
addInngestFunction(functions, new InvokeFailureFunction());
addInngestFunction(functions, new TryCatchRunFunction());

return functions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object> 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<Object> run = devServer.runsByEvent(eventId).first();
String output = (String) run.getOutput();

assertEquals("Completed", run.getStatus());
assertNotNull(run.getEnded_at());

assertEquals("Something fatally went wrong", output);
}

}

0 comments on commit 8330bc0

Please sign in to comment.