-
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.
Very similar to throttle implementation in #66
- Loading branch information
1 parent
bf5da4f
commit c925bd6
Showing
5 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/RateLimitedFunction.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 RateLimitedFunction extends InngestFunction { | ||
|
||
@NotNull | ||
@Override | ||
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) { | ||
return builder | ||
.id("RateLimitedFunction") | ||
.name("RateLimited Function") | ||
.triggerEvent("test/rateLimit") | ||
.rateLimit(3, Duration.ofSeconds(2)); | ||
} | ||
|
||
@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
42 changes: 42 additions & 0 deletions
42
...oot-demo/src/test/java/com/inngest/springbootdemo/RateLimitedFunctionIntegrationTest.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,42 @@ | ||
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 RateLimitedFunctionIntegrationTest { | ||
@Autowired | ||
private DevServerComponent devServer; | ||
|
||
@Autowired | ||
private Inngest client; | ||
|
||
@Test | ||
void testFunctionIsRateLimited() throws Exception { | ||
String event1 = InngestFunctionTestHelpers.sendEvent(client, "test/rateLimit").getIds()[0]; | ||
Thread.sleep(500); | ||
String event2 = InngestFunctionTestHelpers.sendEvent(client, "test/rateLimit").getIds()[0]; | ||
Thread.sleep(500); | ||
String event3 = InngestFunctionTestHelpers.sendEvent(client, "test/rateLimit").getIds()[0]; | ||
|
||
Thread.sleep(2000); | ||
|
||
// Rate limit should only allow the first 2 events to run | ||
assertEquals("Completed", devServer.runsByEvent(event1).first().getStatus()); | ||
assertEquals("Completed", devServer.runsByEvent(event2).first().getStatus()); | ||
assertEquals(0, devServer.runsByEvent(event3).data.length); | ||
|
||
// new event after the rate limit period will run, but the previously skipped event will stay skipped | ||
String event4 = InngestFunctionTestHelpers.sendEvent(client, "test/rateLimit").getIds()[0]; | ||
Thread.sleep(2000); | ||
|
||
assertEquals(0, devServer.runsByEvent(event3).data.length); | ||
assertEquals("Completed", devServer.runsByEvent(event4).first().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