Rework Daemon class to favor composition over inheritance
Most of the code I write tends to be some sort of Daemon
. This is an abstract base class currently that puts a particular workload on a background thread and takes care of orderly shutdown, handling interruptions etc.
It works very well, but its design means that all implementations of it (subclasses like RubySampler
, servers etc.) are notoriously difficult to test. That's because its API interface is based on sub-typing and template methods that these subclasses need to implement, particularly run_thread
which executes inside the daemon thread.
Writing such tests means concurrency must be taken into account via condition variables or sleep
s. This makes tests flaky and hard to write.
We should change the Daemon
or provide an alternative that maintains all its good properties (reliably starting, running, and stopping async work) but make it based on composition instead where the actual business logic is passed to a Daemon
instance as a collaborator it calls into. That way we can completely isolate the technical functions of it from domain functions and a test has merely to be written for the (synchronous) module the daemon dispatches calls to.