Description
There is a TraceableBus for seeing which messages are passed to a bus, but nothing currently to inspect which handlers were called.
Motivation
Some folk might want to write automated tests to ensure that the correct handler was called for a message.
Possible implementation
Something along the lines of the following, where filename, function, lineno, and timestamp function similarly to the MessageInfo in TraceableBus and reference, handler, name, and request relate to the called handler.
@dataclasses.dataclass(frozen=True)classHandlerInfo[T]:
#: handler reference instancereference: HandlerReference[T]
#: handler objecthandler: Handler[T]
#: module path and class or function name of the handlername: str#: request objectrequest: T#: filename where call originatedfilename: str#: function where call originatedfunction: str#: line in file where call originatedlineno: int#: timestamp of calltimestamp: datetime.datetimeclassTraceableHandlerFactory(banshee.HandlerFactory):
def__init__(self, factory: banshee.HandlerFactory) ->None:
self.factory=factoryself._handlers= []
@propertydefhandlers(self):
returntuple(self._handlers)
def__call__(self, reference: banshee.HandlerReference[T], /) ->banshee.Handler[T]:
handler=self.factory(reference)
@functools.wrap(handler)defwrapper(request: T) ->typing.Any:
self._handlers.append(HandlerInfo(...))
returnhandler(request)
returnwrapper
then in a test something like the following can be used:
factory=banshee.SimpleHandlerFactory()
traceable_factory=banshee.TraceableHandlerFactory(factory)
bus= (
banshee.Builder()
.with_factory(traceable_factory)
request=FooCommand()
bus.handle(request)
assertany(
(info.name=="myapp.handlers.handle_foo"andrequest==request)
forinfointraceable_bus.handlers
)
Description
There is a
TraceableBusfor seeing which messages are passed to a bus, but nothing currently to inspect which handlers were called.Motivation
Some folk might want to write automated tests to ensure that the correct handler was called for a message.
Possible implementation
Something along the lines of the following, where
filename,function,lineno, andtimestampfunction similarly to theMessageInfoinTraceableBusandreference,handler,name, andrequestrelate to the called handler.then in a test something like the following can be used: