Repository navigation
perf(core): cache the hardware facades, hoist per-message reflection #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
72d3695
perf(core): cache the hardware facades, hoist per-message reflection
IamCoder18 dc16928
docs(core): fix dropped hardware() Javadoc, correct HardwareView life…
IamCoder18 d5cc6ac
test(core): address review nits in CoreHotPathTest
IamCoder18 b93d419
test(core): make mismatched-message drop observable via log assertions
IamCoder18 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
146 changes: 146 additions & 0 deletions
146
src/test/java/com/aaravlabs/synapse/CoreHotPathTest.java
This file contains hidden or 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,146 @@ | ||
| package com.aaravlabs.synapse; | ||
|
|
||
| import com.aaravlabs.synapse.annotation.SubscribedTo; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| /** | ||
| * Covers the core hot-path cleanups: the shared {@code HardwareActions} facade and the | ||
| * per-message reflection work hoisted out of the annotation binder's dispatch lambda. | ||
| */ | ||
| class CoreHotPathTest { | ||
|
|
||
| private RecordingLogSink logs; | ||
| private Orchestrator orchestrator; | ||
|
|
||
| @BeforeEach void setUp() { | ||
| logs = new RecordingLogSink(); | ||
| orchestrator = Orchestrator.create("core-hot-path", logs); | ||
| } | ||
| @AfterEach void tearDown() { orchestrator.close(); } | ||
|
|
||
| /** | ||
| * Records what the core logged so tests can assert on log output, not just on | ||
| * side effects. Mirrors everything to {@link LogSink#STDERR} so a failing run | ||
| * still shows the orchestrator's trace. | ||
| */ | ||
| private static final class RecordingLogSink implements LogSink { | ||
| private final List<String> errors = new CopyOnWriteArrayList<>(); | ||
|
|
||
| /** @return every {@code error(...)} logged so far, in order. */ | ||
| List<String> errors() { return errors; } | ||
|
|
||
| @Override public void info(String tag, String message) { LogSink.STDERR.info(tag, message); } | ||
| @Override public void warn(String tag, String message) { LogSink.STDERR.warn(tag, message); } | ||
| @Override public void error(String tag, String message) { | ||
| errors.add(message); | ||
| LogSink.STDERR.error(tag, message); | ||
| } | ||
| @Override public void error(String tag, String message, Throwable t) { | ||
| errors.add(message + " (" + t + ")"); | ||
| LogSink.STDERR.error(tag, message, t); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void hardwareActionsFacadeIsSharedAcrossCalls() throws Exception { | ||
| assertSame(orchestrator.hardware(), orchestrator.hardware()); | ||
|
|
||
| // Cached, not frozen: the facade still reaches the hardware thread. | ||
| AtomicInteger hits = new AtomicInteger(); | ||
| CountDownLatch done = new CountDownLatch(1); | ||
| orchestrator.hardware().run(() -> { hits.incrementAndGet(); done.countDown(); }); | ||
| assertTrue(done.await(2, TimeUnit.SECONDS), "hardware().run() should have run"); | ||
| assertEquals(1, hits.get()); | ||
| } | ||
|
|
||
| @Test | ||
| void annotatedSubscriberStillReceivesPrimitiveAndWrapperTypedMessages() throws Exception { | ||
| // Covers both sides of the hoisted normalisation: a primitive parameter and a | ||
| // reference-typed one. | ||
| List<Double> doubles = new CopyOnWriteArrayList<>(); | ||
| List<String> strings = new CopyOnWriteArrayList<>(); | ||
| CountDownLatch done = new CountDownLatch(2); | ||
|
|
||
| class N extends Node { | ||
| N(Orchestrator o) { super(o); } | ||
| @SubscribedTo(topic = "hot/double") | ||
| public void onDouble(double v) { doubles.add(v); done.countDown(); } | ||
| @SubscribedTo(topic = "hot/string") | ||
| public void onString(String v) { strings.add(v); done.countDown(); } | ||
| } | ||
| orchestrator.registerNode("n", new N(orchestrator)); | ||
|
|
||
| orchestrator.publish("hot/double", 0.25); | ||
| orchestrator.publish("hot/string", "go"); | ||
|
|
||
| assertTrue(done.await(2, TimeUnit.SECONDS), "both handlers should run"); | ||
| assertEquals(List.of(0.25), doubles); | ||
| assertEquals(List.of("go"), strings); | ||
| } | ||
|
|
||
| @Test | ||
| void annotatedSubscriberSilentlyDropsMismatchedMessages() throws Exception { | ||
| AtomicInteger calls = new AtomicInteger(); | ||
| CountDownLatch first = new CountDownLatch(1); | ||
|
|
||
| // The topic has to be typed more loosely than the handler parameter for the | ||
| // drop branch to be reachable: publish() rejects a String for a Double topic | ||
| // before any subscriber sees it. Topic types are first-writer-wins, so | ||
| // creating it as Object first is the realistic case — two handlers with | ||
| // different parameter types on one topic. | ||
| orchestrator.getOrCreateTopic("hot/drop", Object.class); | ||
|
|
||
| class N extends Node { | ||
| N(Orchestrator o) { super(o); } | ||
| @SubscribedTo(topic = "hot/drop") | ||
| public void onDouble(double v) { calls.incrementAndGet(); first.countDown(); } | ||
| } | ||
| orchestrator.registerNode("n", new N(orchestrator)); | ||
|
|
||
| orchestrator.publish("hot/drop", "text"); | ||
| assertFalse(first.await(150, TimeUnit.MILLISECONDS), | ||
| "mismatched message must be dropped, not delivered"); | ||
| assertEquals(0, calls.get(), "mismatched message must be dropped, not delivered"); | ||
| // "Not delivered" alone cannot tell a silent drop apart from a delivery | ||
| // that threw and was swallowed by the binder's catch (Throwable): if the | ||
| // isInstance guard were removed, m.invoke(node, "text") on a double | ||
| // parameter raises IllegalArgumentException, the handler never runs, and | ||
| // the two assertions above still hold. A dropped message leaves no trace; | ||
| // a failed delivery is logged as an error, so that is what we assert on. | ||
| assertEquals(List.of(), logs.errors(), | ||
| "mismatched message must be dropped silently, not delivered and failed"); | ||
|
|
||
| // A real Double still gets through. | ||
| orchestrator.publish("hot/drop", 1.5); | ||
| assertTrue(first.await(2, TimeUnit.SECONDS)); | ||
| assertEquals(1, calls.get()); | ||
| assertEquals(List.of(), logs.errors(), "matching message must not log an error"); | ||
| } | ||
|
|
||
| @Test | ||
| void bulkReadCallbackGetsAWorkingView() throws Exception { | ||
| CountDownLatch done = new CountDownLatch(1); | ||
| com.aaravlabs.synapse.ftc.HardwareActions.BulkReadHandle handle = | ||
| orchestrator.hardware().bulkRead(200, view -> { | ||
| view.publish("hot/bulk", 1.0f); | ||
| done.countDown(); | ||
| }); | ||
| try { | ||
| assertTrue(done.await(2, TimeUnit.SECONDS), "bulk read callback should have run"); | ||
| assertEquals(Float.valueOf(1.0f), | ||
| orchestrator.getLatestValue("hot/bulk", Float.class).orElse(null)); | ||
| } finally { | ||
| handle.cancel(); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.