Skip to content

Commit 76ec4ff

Browse files
committed
make initialState optional in config
1 parent f05a7ac commit 76ec4ff

3 files changed

Lines changed: 28 additions & 5 deletions

File tree

sdk/src/main/java/software/amazon/lambda/durable/config/WaitForConditionConfig.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
public class WaitForConditionConfig<T> {
1717
private final WaitForConditionWaitStrategy<T> waitStrategy;
1818
private final SerDes serDes;
19+
private final T initialState;
1920

2021
private WaitForConditionConfig(Builder<T> builder) {
2122
this.waitStrategy = builder.waitStrategy;
2223
this.serDes = builder.serDes;
24+
this.initialState = builder.initialState;
2325
}
2426

2527
/**
@@ -35,6 +37,11 @@ public SerDes serDes() {
3537
return serDes;
3638
}
3739

40+
/** Returns the initial state object, or null if not specified. */
41+
public T initialState() {
42+
return initialState;
43+
}
44+
3845
/**
3946
* Returns a new builder initialized with the values from this config. Useful internally for injecting default
4047
* SerDes.
@@ -45,6 +52,7 @@ public Builder<T> toBuilder() {
4552
var b = new Builder<T>();
4653
b.waitStrategy = this.waitStrategy;
4754
b.serDes = this.serDes;
55+
b.initialState = this.initialState;
4856
return b;
4957
}
5058

@@ -61,6 +69,7 @@ public static <T> Builder<T> builder() {
6169
public static class Builder<T> {
6270
private WaitForConditionWaitStrategy<T> waitStrategy;
6371
private SerDes serDes;
72+
private T initialState;
6473

6574
private Builder() {}
6675

@@ -91,6 +100,17 @@ public Builder<T> serDes(SerDes serDes) {
91100
return this;
92101
}
93102

103+
/**
104+
* Sets the initial state for the waitForCondition operation. The initial state will be null if it's not set.
105+
*
106+
* @param initialState the initial state object to pass to the condition function
107+
* @return this builder for method chaining
108+
*/
109+
public Builder<T> initialState(T initialState) {
110+
this.initialState = initialState;
111+
return this;
112+
}
113+
94114
public WaitForConditionConfig<T> build() {
95115
return new WaitForConditionConfig<>(this);
96116
}

sdk/src/main/java/software/amazon/lambda/durable/operation/WaitForConditionOperation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public WaitForConditionOperation(
6161

6262
@Override
6363
protected void start() {
64-
executeCheckLogic(null, 0);
64+
executeCheckLogic(config.initialState(), 0);
6565
}
6666

6767
@Override
@@ -105,7 +105,7 @@ private void resumeCheckLoop(Operation existing) {
105105
if (checkpointData != null) {
106106
currentState = deserializeResult(checkpointData);
107107
} else {
108-
currentState = null;
108+
currentState = config.initialState();
109109
}
110110
executeCheckLogic(currentState, attempt);
111111
}

sdk/src/test/java/software/amazon/lambda/durable/operation/WaitForConditionOperationTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,11 @@ void replayStartedWithNullCheckpointDataUsesInitialState() throws Exception {
343343
.build();
344344
when(executionManager.getOperationAndUpdateReplayState(OPERATION_ID)).thenReturn(op);
345345

346-
var receivedState = new java.util.concurrent.atomic.AtomicReference<Integer>(-1);
347-
var config = WaitForConditionConfig.<Integer>builder().serDes(SERDES).build();
346+
var receivedState = new java.util.concurrent.atomic.AtomicInteger(-1);
347+
var config = WaitForConditionConfig.<Integer>builder()
348+
.serDes(SERDES)
349+
.initialState(42)
350+
.build();
348351
var operation = createOperation(
349352
(state, ctx) -> {
350353
receivedState.set(state);
@@ -355,7 +358,7 @@ void replayStartedWithNullCheckpointDataUsesInitialState() throws Exception {
355358
operation.execute();
356359

357360
Thread.sleep(200);
358-
assertNull(receivedState.get(), "Should use initialState when checkpoint data is null");
361+
assertEquals(42, receivedState.get(), "Should use initialState when checkpoint data is null");
359362
}
360363

361364
// ===== resumeCheckLoop checkpoint deserialize exception =====

0 commit comments

Comments
 (0)