-
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.
Implement throttling configuration for functions
A test is included, but it may be challenging to verify if the function is throttled since it returns a status of Running. If the test fails due to timing issues, it might not be worth keeping.
- Loading branch information
Showing
5 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...g-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/ThrottledFunction.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,28 @@ | ||
package com.inngest.springbootdemo.testfunctions; | ||
|
||
import com.inngest.FunctionContext; | ||
import com.inngest.InngestFunction; | ||
import com.inngest.InngestFunctionConfigBuilder; | ||
import com.inngest.Step; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.time.Duration; | ||
|
||
public class ThrottledFunction extends InngestFunction { | ||
|
||
@NotNull | ||
@Override | ||
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) { | ||
return builder | ||
.id("ThrottledFunction") | ||
.name("Throttled Function") | ||
.triggerEvent("test/throttled") | ||
.throttle(1, Duration.ofSeconds(10), 1, "throttled"); | ||
} | ||
|
||
@Override | ||
public Integer execute(FunctionContext ctx, Step step) { | ||
return step.run("result", () -> 42, Integer.class); | ||
} | ||
} | ||
|
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
39 changes: 39 additions & 0 deletions
39
...g-boot-demo/src/test/java/com/inngest/springbootdemo/ThrottleFunctionIntegrationTest.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,39 @@ | ||
package com.inngest.springbootdemo; | ||
|
||
import com.inngest.Inngest; | ||
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; | ||
|
||
@IntegrationTest | ||
@Execution(ExecutionMode.CONCURRENT) | ||
class ThrottleFunctionIntegrationTest { | ||
@Autowired | ||
private DevServerComponent devServer; | ||
|
||
@Autowired | ||
private Inngest client; | ||
|
||
@Test | ||
void testThrottledFunctionShouldNotRunConcurrently() throws Exception { | ||
String firstEvent = InngestFunctionTestHelpers.sendEvent(client, "test/throttled").first(); | ||
Thread.sleep(500); | ||
String secondEvent = InngestFunctionTestHelpers.sendEvent(client, "test/throttled").first(); | ||
|
||
Thread.sleep(5000); | ||
|
||
// Without throttling, both events would have been completed by now | ||
RunEntry<Object> firstRun = devServer.runsByEvent(firstEvent).first(); | ||
RunEntry<Object> secondRun = devServer.runsByEvent(secondEvent).first(); | ||
assertEquals("Completed", firstRun.getStatus()); | ||
assertEquals("Running", secondRun.getStatus()); | ||
|
||
Thread.sleep(10000); | ||
|
||
RunEntry<Object> secondRunAfterWait = devServer.runsByEvent(secondEvent).first(); | ||
assertEquals("Completed", secondRunAfterWait.getStatus()); | ||
} | ||
} |
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