Skip to content

Commit 950bac4

Browse files
committed
add e2e tests for idempotency
1 parent fd03fd9 commit 950bac4

File tree

4 files changed

+191
-3
lines changed

4 files changed

+191
-3
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>software.amazon.lambda</groupId>
6+
<artifactId>E2EIdempotencyFunction</artifactId>
7+
<version>1.12.3</version>
8+
<packaging>jar</packaging>
9+
<name>A sample Hello World using powertools idempotency</name>
10+
11+
<properties>
12+
<log4j.version>2.19.0</log4j.version>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<lambda.powertools.version>1.12.3</lambda.powertools.version>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>software.amazon.lambda</groupId>
20+
<artifactId>powertools-idempotency</artifactId>
21+
<version>${lambda.powertools.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.amazonaws</groupId>
25+
<artifactId>aws-lambda-java-core</artifactId>
26+
<version>1.2.1</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.amazonaws</groupId>
30+
<artifactId>aws-lambda-java-events</artifactId>
31+
<version>3.11.0</version>
32+
</dependency>
33+
34+
</dependencies>
35+
36+
<build>
37+
<plugins>
38+
<plugin>
39+
<groupId>org.codehaus.mojo</groupId>
40+
<artifactId>aspectj-maven-plugin</artifactId>
41+
<version>1.14.0</version>
42+
<configuration>
43+
<source>${maven.compiler.source}</source>
44+
<target>${maven.compiler.target}</target>
45+
<complianceLevel>${maven.compiler.target}</complianceLevel>
46+
<aspectLibraries>
47+
<aspectLibrary>
48+
<groupId>software.amazon.lambda</groupId>
49+
<artifactId>powertools-idempotency</artifactId>
50+
</aspectLibrary>
51+
</aspectLibraries>
52+
</configuration>
53+
<executions>
54+
<execution>
55+
<goals>
56+
<goal>compile</goal>
57+
</goals>
58+
</execution>
59+
</executions>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-shade-plugin</artifactId>
64+
<version>3.2.4</version>
65+
<configuration>
66+
<createDependencyReducedPom>false</createDependencyReducedPom>
67+
<finalName>function</finalName>
68+
</configuration>
69+
<executions>
70+
<execution>
71+
<phase>package</phase>
72+
<goals>
73+
<goal>shade</goal>
74+
</goals>
75+
<configuration>
76+
<transformers>
77+
<transformer
78+
implementation="io.github.edwgiz.log4j.maven.plugins.shade.transformer.Log4j2PluginCacheFileTransformer">
79+
</transformer>
80+
</transformers>
81+
</configuration>
82+
</execution>
83+
</executions>
84+
<dependencies>
85+
<dependency>
86+
<groupId>io.github.edwgiz</groupId>
87+
<artifactId>log4j-maven-shade-plugin-extensions</artifactId>
88+
<version>2.17.2</version>
89+
</dependency>
90+
</dependencies>
91+
</plugin>
92+
<plugin>
93+
<groupId>org.apache.maven.plugins</groupId>
94+
<artifactId>maven-compiler-plugin</artifactId>
95+
<version>3.10.1</version>
96+
<configuration>
97+
<source>${maven.compiler.source}</source>
98+
<target>${maven.compiler.target}</target>
99+
</configuration>
100+
</plugin>
101+
</plugins>
102+
</build>
103+
</project>

powertools-e2e-tests/handlers/idempotency/src/main/java/software/amazon/lambda/powertools/e2e/Function.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
import software.amazon.lambda.powertools.idempotency.Idempotent;
1111
import software.amazon.lambda.powertools.idempotency.persistence.DynamoDBPersistenceStore;
1212

13+
import java.time.Duration;
1314
import java.time.Instant;
1415
import java.time.format.DateTimeFormatter;
16+
import java.time.temporal.ChronoUnit;
17+
import java.util.TimeZone;
1518

1619

17-
public class Function implements RequestHandler<String, String> {
20+
public class Function implements RequestHandler<Input, String> {
1821

1922
public Function() {
2023
this(DynamoDbClient
@@ -27,6 +30,7 @@ public Function() {
2730
public Function(DynamoDbClient client) {
2831
Idempotency.config().withConfig(
2932
IdempotencyConfig.builder()
33+
.withExpiration(Duration.of(10, ChronoUnit.SECONDS))
3034
.build())
3135
.withPersistenceStore(
3236
DynamoDBPersistenceStore.builder()
@@ -37,8 +41,8 @@ public Function(DynamoDbClient client) {
3741
}
3842

3943
@Idempotent
40-
public String handleRequest(String input, Context context) {
41-
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
44+
public String handleRequest(Input input, Context context) {
45+
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME.withZone(TimeZone.getTimeZone("UTC").toZoneId());
4246
return dtf.format(Instant.now());
4347
}
4448
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package software.amazon.lambda.powertools.e2e;
2+
3+
public class Input {
4+
private String message;
5+
6+
public Input(String message) {
7+
this.message = message;
8+
}
9+
10+
public Input() {
11+
}
12+
13+
public String getMessage() {
14+
return message;
15+
}
16+
17+
public void setMessage(String message) {
18+
this.message = message;
19+
}
20+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package software.amazon.lambda.powertools;
2+
3+
import org.assertj.core.api.Assertions;
4+
import org.junit.jupiter.api.AfterAll;
5+
import org.junit.jupiter.api.BeforeAll;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.Timeout;
8+
import software.amazon.lambda.powertools.testutils.Infrastructure;
9+
import software.amazon.lambda.powertools.testutils.InvocationResult;
10+
11+
import java.time.Year;
12+
import java.util.HashMap;
13+
import java.util.concurrent.TimeUnit;
14+
15+
public class IdempotencyE2ETest {
16+
private static Infrastructure infrastructure;
17+
18+
@BeforeAll
19+
@Timeout(value = 5, unit = TimeUnit.MINUTES)
20+
public static void setup() {
21+
infrastructure = Infrastructure.builder()
22+
.testName(IdempotencyE2ETest.class.getSimpleName())
23+
.pathToFunction("idempotency")
24+
.idempotencyTable("idempo")
25+
.environmentVariables(new HashMap<>() {{
26+
put("IDEMPOTENCY_TABLE", "idempo");
27+
}}
28+
)
29+
.build();
30+
infrastructure.deploy();
31+
}
32+
33+
@AfterAll
34+
public static void tearDown() {
35+
if (infrastructure != null)
36+
infrastructure.destroy();
37+
}
38+
39+
@Test
40+
public void test_ttlNotExpired_sameResult_ttlExpired_differentResult() throws InterruptedException {
41+
// GIVEN
42+
String event = "{\"message\":\"TTL 10sec\"}";
43+
44+
// WHEN
45+
// First invocation
46+
InvocationResult result1 = infrastructure.invokeFunction(event);
47+
48+
// Second invocation (should get same result)
49+
InvocationResult result2 = infrastructure.invokeFunction(event);
50+
51+
Thread.sleep(12000);
52+
53+
// Third invocation (should get different result)
54+
InvocationResult result3 = infrastructure.invokeFunction(event);
55+
56+
// THEN
57+
Assertions.assertThat(result1.getResult()).contains(Year.now().toString());
58+
Assertions.assertThat(result2.getResult()).isEqualTo(result1.getResult());
59+
Assertions.assertThat(result3.getResult()).isNotEqualTo(result2.getResult());
60+
}
61+
}

0 commit comments

Comments
 (0)