Skip to content

Commit

Permalink
Add anchor rollback test
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelCourtney committed Jan 23, 2025
1 parent 35d5278 commit ea5a811
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
/**
* Creates three activities in a chain of anchors, then deletes one.
* If `whichToDelete` is negative, this leaves all three activities.
* If `rollback` is true, this will roll the edit back before finishing.
*/
@SchedulingProcedure
public record ActivityDeletionGoal(int whichToDelete, DeletedAnchorStrategy anchorStrategy) implements Goal {
public record ActivityDeletionGoal(int whichToDelete, DeletedAnchorStrategy anchorStrategy, boolean rollback) implements Goal {
@Override
public void run(@NotNull final EditablePlan plan) {
final var ids = new ActivityDirectiveId[3];
Expand All @@ -42,6 +43,7 @@ public void run(@NotNull final EditablePlan plan) {
plan.delete(ids[whichToDelete], anchorStrategy);
}

plan.commit();
if (rollback) plan.rollback();
else plan.commit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ void createsThreeActivities() throws IOException {
.createObjectBuilder()
.add("whichToDelete", -1)
.add("anchorStrategy", "Error")
.add("rollback", false)
.build();

hasura.updateSchedulingSpecGoalArguments(procedureId.invocationId(), args);
Expand Down Expand Up @@ -82,6 +83,7 @@ void deletesLast() throws IOException {
.createObjectBuilder()
.add("whichToDelete", 2)
.add("anchorStrategy", "Error")
.add("rollback", false)
.build();

hasura.updateSchedulingSpecGoalArguments(procedureId.invocationId(), args);
Expand Down Expand Up @@ -113,6 +115,7 @@ void deletesMiddleCascade() throws IOException {
.createObjectBuilder()
.add("whichToDelete", 1)
.add("anchorStrategy", "Cascade")
.add("rollback", false)
.build();

hasura.updateSchedulingSpecGoalArguments(procedureId.invocationId(), args);
Expand All @@ -134,7 +137,8 @@ void deletesMiddleAnchorToParent() throws IOException {
final var args = Json
.createObjectBuilder()
.add("whichToDelete", 1)
.add("anchorStrategy", "ReAnchor")
.add("anchorStrategy", "PreserveTree")
.add("rollback", false)
.build();

hasura.updateSchedulingSpecGoalArguments(procedureId.invocationId(), args);
Expand Down Expand Up @@ -168,6 +172,7 @@ void deletesFirstCascade() throws IOException {
.createObjectBuilder()
.add("whichToDelete", 0)
.add("anchorStrategy", "Cascade")
.add("rollback", false)
.build();

hasura.updateSchedulingSpecGoalArguments(procedureId.invocationId(), args);
Expand All @@ -185,7 +190,8 @@ void deletesFirstReAnchorToPlan() throws IOException {
final var args = Json
.createObjectBuilder()
.add("whichToDelete", 0)
.add("anchorStrategy", "ReAnchor")
.add("anchorStrategy", "PreserveTree")
.add("rollback", false)
.build();

hasura.updateSchedulingSpecGoalArguments(procedureId.invocationId(), args);
Expand All @@ -212,4 +218,45 @@ void deletesFirstReAnchorToPlan() throws IOException {
it -> Objects.equals(it.type(), "BiteBanana") && Objects.equals(it.anchorId(), id2.get())
));
}

@Test
void anchorResetOnRollback() throws IOException {
final var args = Json
.createObjectBuilder()
.add("whichToDelete", 1)
.add("anchorStrategy", "PreserveTree")
.add("rollback", true)
.build();

hasura.updateSchedulingSpecGoalArguments(procedureId.invocationId(), args);

hasura.awaitScheduling(specId);

final var plan = hasura.getPlan(planId);
final var activities = plan.activityDirectives();

assertEquals(3, activities.size());

final AtomicReference<Integer> id1 = new AtomicReference<>();
final AtomicReference<Integer> id2 = new AtomicReference<>();
assertTrue(activities.stream().anyMatch(
it -> {
final var result = Objects.equals(it.type(), "BiteBanana") && Objects.equals(it.anchorId(), null);
if (result) id1.set(it.id());
return result;
}
));

assertTrue(activities.stream().anyMatch(
it -> {
final var result = Objects.equals(it.type(), "BiteBanana") && Objects.equals(it.anchorId(), id1.get());
if (result) id2.set(it.id());
return result;
}
));

assertTrue(activities.stream().anyMatch(
it -> Objects.equals(it.type(), "BiteBanana") && Objects.equals(it.anchorId(), id2.get())
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,20 @@ public Map<ActivityDirectiveId, ActivityDirectiveId> updatePlanActivityDirective
activity.anchoredToStart()
);
if (!activityDirectiveFromSchedulingDirective.equals(actFromInitialPlan.get())) {
final var newState = activityDirectiveFromSchedulingDirective.serializedActivity();
final var oldState = actFromInitialPlan.get().serializedActivity();
if (!Objects.equals(newState.getTypeName(), oldState.getTypeName())) {
throw new IllegalStateException(
"Modified activities cannot change type. Was " + oldState.getTypeName()
+ ", now " + newState.getTypeName()
);
}
if (!Objects.equals(newState.getArguments(), oldState.getArguments())) {
throw new IllegalStateException(
"Modified activities cannot change arguments. Was " + oldState.getArguments()
+ ", now " + newState.getArguments()
);
}
toModify.add(activity);
}
ids.put(activity.id(), activity.id());
Expand Down

0 comments on commit ea5a811

Please sign in to comment.