Execute fn synchronously


Hi people,
I need some advices here, I started to add dramatiq to our django project, it works out of the box and it’s awesome, but since we have old tests to modify I am scratching my head since a couple of days. The util django-dramatiq offers,DramatiqTestCase, is fine, but it inherits from TransactionTestCase so tests run under it are very slow.
We do have some set of pure unit tests, but we also sometimes have more complicated tests where views call actors and actors call other actors/do stuff based on some logic. When we have to fully test some behaviors, having some hook to run synchronously fn.send() would be nice; first there is no need to spawn the stubbroker and threads, second with no threads TestCase can still be used speeding up the test suite. I know that calling the actor without send runs it sync, but when you have this things inside other code this is not a feasable solution (also when you use one actor to schedule/dispatch more tasks).
I ended up with the most horrible solution probably, monkey patching Actor.send to run self.fn(*args, **kwargs), it works and thanks to this I don’t have to refactor lot of tests.
I’d like to ask if there is a more elegant solution to this problem and if there is any remote chance to add an option (by default async) that can be modified on need to run the function sync/async, something like:

def send(self, *args, **kwargs):
    if ASYNC_FLAG:
        return  self.send_with_options(args=args, kwargs=kwargs)
        return self.fn(*args, **kwargs)


This is actually one thing I wanted to avoid while building Dramatiq. I want my tasks to be executed asynchronously during integration tests because that better represents how they’re going to be behave in the real world. That said, I’ll consider ways of making this easier in 2.0.


that’s perfectly understandable, in fact this specific issue is very coupled on how the DB is setup/cleaned when running the django test suite. Will wait for 2.0 monkeypatching tests in the meanwhile, thanks for chiming in :v: