Skip to content

Commit

Permalink
feat: processing patterns can intercept network insertions
Browse files Browse the repository at this point in the history
raoulvdberge committed Jan 5, 2025
1 parent e031143 commit 97149aa
Showing 3 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -26,11 +26,15 @@ class ExternalTaskPattern extends AbstractTaskPattern {

@Override
boolean step(final MutableResourceList internalStorage, final ExternalPatternInputSink externalPatternInputSink) {

if (expectedOutputs.isEmpty()) {
return true;
}
if (iterationsToSendToSink == 0) {
// TODO: coverage
return false;
}
if (!acceptsIterationInputs(internalStorage, externalPatternInputSink)) {
// TODO: coverage
return false;
}
LOGGER.info("Stepped {} with {} iterations remaining", pattern, iterationsToSendToSink);
@@ -46,22 +50,25 @@ long interceptInsertion(final ResourceKey resource, final long amount) {
expectedOutputs.remove(resource, available);
return available;
}
// TODO: coverage
return 0;
}

private boolean acceptsIterationInputs(final MutableResourceList internalStorage,
final ExternalPatternInputSink externalPatternInputSink) {
final ResourceList iterationInputsSimulated = calculateIterationInputs(Action.SIMULATE);
if (!extractAll(iterationInputsSimulated, internalStorage, Action.SIMULATE)) {
// TODO: coverage
return false;
}
if (!externalPatternInputSink.accept(pattern, iterationInputsSimulated.copyState(), Action.SIMULATE)) {
// TODO: coverage
return false;
}
final ResourceList iterationInputs = calculateIterationInputs(Action.EXECUTE);
extractAll(iterationInputs, internalStorage, Action.EXECUTE);
if (!externalPatternInputSink.accept(pattern, iterationInputs.copyState(), Action.EXECUTE)) {
// TODO: return here.
// TODO: coverage
return false;
}
return true;
Original file line number Diff line number Diff line change
@@ -138,6 +138,7 @@ private boolean returnInternalStorage(final RootStorage rootStorage) {

@Override
public long beforeInsert(final ResourceKey resource, final long amount, final Actor actor) {
// TODO: coverage
long totalIntercepted = 0;
for (final AbstractTaskPattern pattern : patterns.values()) {
final long remaining = amount - totalIntercepted;
Original file line number Diff line number Diff line change
@@ -268,6 +268,7 @@ void shouldCompleteTaskWithExternalPattern() {

task.step(storage, sink);
assertThat(task.getState()).isEqualTo(TaskState.RUNNING);
assertThat(storage.getAll()).isEmpty();
assertThat(task.copyInternalStorageState())
.usingRecursiveFieldByFieldElementComparator()
.containsExactlyInAnyOrder(
@@ -288,12 +289,23 @@ void shouldCompleteTaskWithExternalPattern() {
);

task.step(storage, sink);
assertThat(task.getState()).isEqualTo(TaskState.RUNNING);
assertThat(task.getState()).isEqualTo(TaskState.RETURNING_INTERNAL_STORAGE);
assertThat(storage.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactly(
new ResourceAmount(IRON_INGOT, 3)
);
assertThat(task.copyInternalStorageState())
.usingRecursiveFieldByFieldElementComparator()
.containsExactly(
new ResourceAmount(IRON_PICKAXE, 1)
);

task.step(storage, sink);
assertThat(task.getState()).isEqualTo(TaskState.COMPLETED);
assertThat(storage.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrder(
new ResourceAmount(IRON_INGOT, 3),
new ResourceAmount(IRON_PICKAXE, 1)
);
assertThat(task.copyInternalStorageState()).isEmpty();
}

@Test

0 comments on commit 97149aa

Please sign in to comment.