0

Am on a project using txrdq, am testing (using trial) for a case where a queued job may fail, trial marks the testcase as failed whenever it hits a failure in a errback ..

The errback is a normal behaviour, since a queued job may fail to launch, how to test this case using trial without failing the test ?

here's an example of the test case:

from twisted.trial.unittest import TestCase
from txrdq.rdq import ResizableDispatchQueue
from twisted.python.failure import Failure

class myTestCase(TestCase):
    def aFailingJob(self, a):
        return Failure("This is a failure")

    def setUp(self):
        self.queue = ResizableDispatchQueue(self.aFailingJob, 1)

    def tearDown(self):
        pass

    def test_txrdq(self):
        self.queue.put("Some argument", 1)

3 Answers 3

1

It seems likely that the exception is being logged, since the error handler just raises it. I'm not exactly sure what the error handling code in txrdq looks like, so this is just a guess, but I think it's a pretty good one based on your observations.

Trial fails any unit test that logs an exception, unless the test cleans that exception up after it's logged. Use TestCase.flushLoggedErrors(exceptionType) to deal with this:

def test_txrdq(self):
    self.queue.put("Some argument", 1)
    self.assertEqual(1, len(self.flushLoggedErrors(SomeException)))

Also notice that you should never do Failure("string"). This is analogous to raise "string". String exceptions are deprecated in Python since a looooong time ago. Always construct a Failure with an exception instance:

class JobError(Exception):
    pass

def aFailingJob(self, a):
    return Failure(JobError("This is a failure"))

This makes JobError the exception type you'd pass to flushLoggedErrors.

Make sure that you understand whether queue processing is synchronous or asynchronous. If it is synchronous, your test (with the flushLoggedErrors call added) is fine. If it is asynchronous, your error handler may not have run by the time your test method returns. In that case, you're not going to be testing anything useful, and the errors might be logged after the call to flush them (making the flush useless).

Finally, if you're not writing unit tests '''for''' txrdq, then you might not want to write tests like this. You can probably unit test txrdq-using code without using an actual txrdq. A normal Queue object (or perhaps another more specialized test double) will let you more precisely target the units in your application, making your tests faster, more reliable, and easier to debug.

1
  • Hi Jean Peal, by returning a Failure(JobError("This is a failure")) and then self.assertEqual(1, len(self.flushLoggedErrors(JobError))) in the test case, i still get the same error and the assert fails since 0 is returned by the flushLoggedErrors call, it may be the async behaviour where i may flush the error before it happens so i added an errback to the put method like this: self.queue.put(..., 1).addErrback(self.err) and in err() i flush errors, now i can see the failure happens before the errback is called, but i still get same behaviour and 0 is still returned by the flush call
    – zfou
    Mar 21, 2012 at 12:00
1

This issue has now (finally!) been solved, by L. Daniel Burr. There's a new version (0.2.14) of txRDQ on PyPI.

By the way, in your test you should add from txrdq.job import Job, and then do something like this:

d = self.queue.put("Some argument", 1)
return self.assertFailure(d, Job)

Trial will make sure that d fails with a Job instance. There are a couple of new tests at the bottom of txrdq/test/test_rdq.py that illustrate this kind of assertion.

I'm sorry this problem caused so much head scratching for you - it was entirely my fault.

0

Sorry to see you're still having a problem. I don't know what's going on here, but I have been playing with it for over an hour trying to...

The queue.put method returns a Deferred. You can attach an errback to it to do the flush as @exarkun describes, and then return the Deferred from the test. I expected that to fix things (having read @exarkun's reply and gotten a comment from @idnar in #twisted). But it doesn't help.

Here's a bit of the recent IRC conversation, mentioning what I think could be happening: https://gist.github.com/2177560

As far as I can see, txRDQ is doing the right thing. The job fails and the deferred that is returned by queue.put is errbacked.

If you look in _trial_temp/test.log after you run the test, what do you see? I see an error that says Unhandled error in Deferred and the error is a Failure with a Job in it. So it seems likely to me that the error is somewhere in txRDQ. That there is a deferred that's failing and it's passing on the failure just fine to whoever needs it, but also returning the failure - causing trial to complain. But I don't know where that is. I put a print into the init of the Deferred class just out of curiousity to see how many deferreds were made during the running of the test. The answer: 12!

Sorry not to have better news. If you want to press on, go look at every deferred made by the txRDQ code. Is one of them failing with an errback that returns the failure? I don't see it, and I've put print statements in all over the place to check that things are right. I guess I must be missing something.

Thanks, and thanks too @exarkun.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.