-
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.
Copied doc from https://github.com/inngest/inngest/blob/6594ccb692121bfe62b11676fe002416df13300b/docs/SDK_SPEC.md?plain=1#L534-L538 https://www.inngest.com/docs/guides/handling-idempotency#at-the-function-level-the-consumer The integration test validates idempotency by - checking an observable side effect, in this case incrementing a static counter variable - check that the second run of the same idempotency was ignored by the dev server
- Loading branch information
1 parent
937f7af
commit bf5da4f
Showing
6 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/IdempotentFunction.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,25 @@ | ||
package com.inngest.springbootdemo.testfunctions; | ||
|
||
import com.inngest.*; | ||
import lombok.Getter; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class IdempotentFunction extends InngestFunction { | ||
|
||
@NotNull | ||
@Override | ||
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) { | ||
return builder | ||
.id("idempotent-fn") | ||
.name("Idempotent Function") | ||
.triggerEvent("test/idempotent") | ||
.idempotency("event.data.companyId"); | ||
} | ||
|
||
@Getter | ||
private static int counter = 0; | ||
@Override | ||
public Integer execute(FunctionContext ctx, Step step) { | ||
return step.run("increment-count", () -> counter++, 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
51 changes: 51 additions & 0 deletions
51
...boot-demo/src/test/java/com/inngest/springbootdemo/IdempotentFunctionIntegrationTest.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,51 @@ | ||
package com.inngest.springbootdemo; | ||
|
||
import com.inngest.Inngest; | ||
import com.inngest.springbootdemo.testfunctions.IdempotentFunction; | ||
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 java.util.Collections; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@IntegrationTest | ||
@Execution(ExecutionMode.CONCURRENT) | ||
class IdempotentFunctionIntegrationTest { | ||
@Autowired | ||
private DevServerComponent devServer; | ||
|
||
@Autowired | ||
private Inngest client; | ||
|
||
@Test | ||
void testIdempotencyKey() throws Exception { | ||
Map dataPayload = Collections.singletonMap("companyId", 42); | ||
String eventWithIdempotencyKey = InngestFunctionTestHelpers.sendEvent(client, "test/idempotent", dataPayload).getIds()[0]; | ||
String eventWithSameIdempotencyKey = InngestFunctionTestHelpers.sendEvent(client, "test/idempotent", dataPayload).getIds()[0]; | ||
|
||
Thread.sleep(2000); | ||
|
||
// With the same idempotency key, only one of the events should have run | ||
RunEntry<Object> firstRun = devServer.runsByEvent(eventWithIdempotencyKey).first(); | ||
assertEquals(0, devServer.runsByEvent(eventWithSameIdempotencyKey).data.length); | ||
assertEquals("Completed", firstRun.getStatus()); | ||
|
||
// This would be 2 if the function was not idempotent | ||
assertEquals(1, IdempotentFunction.getCounter()); | ||
|
||
|
||
Map differentDataPayload = Collections.singletonMap("companyId", 43); | ||
String eventWithDifferentIdempotencyKey = InngestFunctionTestHelpers.sendEvent(client, "test/idempotent", differentDataPayload).getIds()[0]; | ||
|
||
Thread.sleep(2000); | ||
|
||
// Event with a different idempotency key will run once | ||
RunEntry<Object> otherRun = devServer.runsByEvent(eventWithDifferentIdempotencyKey).first(); | ||
assertEquals("Completed", otherRun.getStatus()); | ||
assertEquals(2, IdempotentFunction.getCounter()); | ||
} | ||
} |
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
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