-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for try-catching in
run
and invoke
functions
- Loading branch information
Showing
6 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
...ot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/InvokeFailureFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/TryCatchRunFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...-spring-boot-demo/src/test/java/com/inngest/springbootdemo/StepErrorsIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |