Testing
InMemoryFlexiQ and InMemoryQueueBackend — a pure-Java backend for fast unit tests.
InMemoryFlexiQ and InMemoryQueueBackend — a pure-Java backend for fast unit tests.
import org.byteveda.flexiq.test.InMemoryFlexiQ;Ships in the org.byteveda:flexiq-test artifact (the :test-support
subproject). The testing model is: swap in an in-memory backend, run a real
in-process worker against it, and synchronize on results the same way you
would against a native backend. For a walkthrough with assertions, see the
Testing guide.
testImplementation("org.byteveda:flexiq-test:1.0.0")InMemoryFlexiQstatic FlexiQ open()
static FlexiQ open(Serializer serializer)Opens a FlexiQ over a fresh
InMemoryQueueBackend. open() uses the default JSON serializer; open(Serializer)
swaps it. To share one backend instance across a custom builder, construct it
explicitly:
FlexiQ flexiq = FlexiQ.builder().open(new InMemoryQueueBackend());InMemoryQueueBackendA pure-Java QueueBackend implementation — no JNI, no disk. It provides the
full producer, inspection, admin, locks, and log surface, plus a small
in-process polling worker: dispatch → complete/fail/cancel → retry or
dead-letter, honoring per-worker queue filters and priority/FIFO ordering
among pending jobs.
try (FlexiQ flexiq = InMemoryFlexiQ.open()) {
String id = flexiq.enqueue("add", List.of(2, 3));
try (Worker worker = flexiq.worker()
.handle("add", List.class, numbers -> (int) numbers.get(0) + (int) numbers.get(1))
.start()) {
flexiq.awaitJob(id, Duration.ofSeconds(5));
}
assertEquals(5, flexiq.getResult(id, Integer.class).orElseThrow());
}| Capability | Behavior |
|---|---|
| Workflows | Unsupported. Every workflow QueueBackend method (submitWorkflow, expandFanOut, getWorkflowStatusJson, …) throws UnsupportedOperationException. Use a native backend (e.g. a throwaway SQLite file) for workflow tests. |
| Periodic tasks | registerPeriodic and friends are recorded in a catalog — listPeriodic, deletePeriodic, pausePeriodic all work against it — but registered periodics never fire. |
There is no dedicated mock-resource type — register a stub factory the same way you'd register a real one:
flexiq.resource("db", context -> fakeDb);The worker injects whatever is registered, so no real connection is opened during the test.
awaitJob(id, timeout) blocks until the job reaches a terminal state and is
the simplest synchronization point. An event listener with a CountDownLatch
works too:
CountDownLatch done = new CountDownLatch(1);
Worker worker = flexiq.worker()
.handle("echo", String.class, payload -> payload.length())
.on(EventName.SUCCESS, event -> done.countDown())
.start();