[Twisted-Python] Help with trial test failure

Benjamin Bertrand beenje at gmail.com
Mon Aug 5 03:09:56 MDT 2013


On Sun, Aug 4, 2013 at 7:55 PM, Itamar Turner-Trauring
<itamar at itamarst.org>wrote:

> On 08/04/2013 10:25 AM, Benjamin BERTRAND wrote:
>
>> Hi,
>>
>> I'm trying to write a simple gateway to receive messages using a specific
>> protocol and publish/store them using txredis.
>> I wrote a small example that seems to work.
>> But the small test I wrote fails:
>>
>> $ trial gateway/test
>> gateway.test.test_example
>>    GatewayServiceTestCase
>>      test_messageReceived ...
>>  [ERROR]
>>
>> ==============================**==============================**
>> ===================
>> [ERROR]
>> Traceback (most recent call last):
>> Failure: twisted.internet.error.**ConnectionDone: Connection was closed
>> cleanly.
>>
>> gateway.test.test_example.**GatewayServiceTestCase.test_**messageReceived
>> ------------------------------**------------------------------**
>> -------------------
>> Ran 1 tests in 0.007s
>>
>>
>> As I understand, the connection to the redis server is lost during the
>> test.
>> I actually managed to get the test to pass by adding some inlineCallbacks
>> decorator to my messageReceived and lineReceived methods.
>> But I don't really understand why that would be needed.
>> Could someone explain what is happening?
>>
>>  I suspect the inlineCallbacks is irrelevant (and not the way to solve
> this).
>
> At a guess, what is happening is that something is logging the connection
> being lost (perhaps the redis library?). Logging errors in a unit test
> makes trial consider that test to have failed: logging an error suggests
> something has gone wrong. You can tell trial to expect a specific error to
> be logged in your test: https://twistedmatrix.com/**
> documents/current/core/howto/**trial.html#auto11<https://twistedmatrix.com/documents/current/core/howto/trial.html#auto11>
>
>
I think I understand what is happening.
In my publish method, I call the redis publish and zadd methods.
My test checks the message received by the subscriber and closes the
connection just after. I don't wait for the deferred return by the zadd
method to fire (and get an error).

That's why the inlineCallbacks solves the problem. When I yield on
the self.server.dataReceived, I wait for the deferred in the publish
methods to fire and everything is clean when I close the connection.

Is there another way to do that?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://twistedmatrix.com/pipermail/twisted-python/attachments/20130805/cef0e3f9/attachment.html>


More information about the Twisted-Python mailing list