forked from boskworks/bosk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
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
82 changes: 82 additions & 0 deletions
82
bosk-testing/src/main/java/io/vena/bosk/drivers/AsyncDriver.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,82 @@ | ||
package io.vena.bosk.drivers; | ||
|
||
import io.vena.bosk.Bosk; | ||
import io.vena.bosk.BoskDriver; | ||
import io.vena.bosk.DriverFactory; | ||
import io.vena.bosk.Identifier; | ||
import io.vena.bosk.Reference; | ||
import io.vena.bosk.StateTreeNode; | ||
import io.vena.bosk.exceptions.InvalidTypeException; | ||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Semaphore; | ||
import lombok.RequiredArgsConstructor; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static lombok.AccessLevel.PRIVATE; | ||
|
||
@RequiredArgsConstructor(access = PRIVATE) | ||
public class AsyncDriver<R extends StateTreeNode> implements BoskDriver<R> { | ||
private final Bosk<R> bosk; | ||
private final BoskDriver<R> downstream; | ||
private final ExecutorService executor = Executors.newSingleThreadExecutor(); | ||
|
||
public static <RR extends StateTreeNode> DriverFactory<RR> factory() { | ||
return AsyncDriver::new; | ||
} | ||
|
||
@Override | ||
public R initialRoot(Type rootType) throws InvalidTypeException, IOException, InterruptedException { | ||
return downstream.initialRoot(rootType); | ||
} | ||
|
||
@Override | ||
public <T> void submitReplacement(Reference<T> target, T newValue) { | ||
submitAsyncTask("submitReplacement", () -> downstream.submitReplacement(target, newValue)); | ||
} | ||
|
||
@Override | ||
public <T> void submitConditionalReplacement(Reference<T> target, T newValue, Reference<Identifier> precondition, Identifier requiredValue) { | ||
submitAsyncTask("submitConditionalReplacement", () -> downstream.submitConditionalReplacement(target, newValue, precondition, requiredValue)); | ||
} | ||
|
||
@Override | ||
public <T> void submitInitialization(Reference<T> target, T newValue) { | ||
submitAsyncTask("submitInitialization", () -> downstream.submitInitialization(target, newValue)); | ||
} | ||
|
||
@Override | ||
public <T> void submitDeletion(Reference<T> target) { | ||
submitAsyncTask("submitDeletion", () -> downstream.submitDeletion(target)); | ||
} | ||
|
||
@Override | ||
public <T> void submitConditionalDeletion(Reference<T> target, Reference<Identifier> precondition, Identifier requiredValue) { | ||
submitAsyncTask("submitConditionalDeletion", () -> downstream.submitConditionalDeletion(target, precondition, requiredValue)); | ||
} | ||
|
||
@Override | ||
public void flush() throws IOException, InterruptedException { | ||
Semaphore semaphore = new Semaphore(0); | ||
submitAsyncTask("flush", semaphore::release); | ||
semaphore.acquire(); | ||
downstream.flush(); | ||
} | ||
|
||
private void submitAsyncTask(String description, Runnable task) { | ||
LOGGER.debug("Submit {}", description); | ||
var diagnosticAttributes = bosk.diagnosticContext().getAttributes(); | ||
executor.submit(()->{ | ||
LOGGER.debug("Run {}", description); | ||
try (var __ = bosk.diagnosticContext().withOnly(diagnosticAttributes)) { | ||
task.run(); | ||
} | ||
LOGGER.trace("Done {}", description); | ||
}); | ||
} | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(AsyncDriver.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
12 changes: 12 additions & 0 deletions
12
bosk-testing/src/test/java/io/vena/bosk/drivers/AsyncDriverConformanceTest.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,12 @@ | ||
package io.vena.bosk.drivers; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
|
||
public class AsyncDriverConformanceTest extends DriverConformanceTest { | ||
|
||
@BeforeEach | ||
void setupDriverFactory() { | ||
driverFactory = AsyncDriver.factory(); | ||
} | ||
|
||
} |