[Twisted-Python] Help with trial test failure

2013-08-04 Thread Benjamin BERTRAND
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?

Both version of the code can be found here: 
https://gist.github.com/beenje/6150400
(revision 1 with the problem and revision 2 with the inlineCallbacks)

Below is the original version with the problem:

Thanks

Benjamin

example.py
--
import json
import time
from twisted.internet import defer
from twisted.internet.protocol import ServerFactory
from twisted.protocols.basic import LineReceiver
from twisted.python import log


class BasicProtocol(LineReceiver):

def lineReceived(self, line):
self.messageReceived(line)

def messageReceived(self, message):
try:
self.factory.messageReceived(message)
except AttributeError:
pass


class BasicGatewayFactory(ServerFactory):

protocol = BasicProtocol

def __init__(self, service, channel):
self.service = service
self.channel = channel

def messageReceived(self, message):
self.service.publish(self.channel, message)


class RedisPublishService(object):

def __init__(self, factory):
"""
@param factory: redis client factory
"""
self.factory = factory

@defer.inlineCallbacks
def publish(self, channel, message):
log.msg("Publish message {} on {}".format(message, channel))
yield self.factory.client.publish(channel, message)
timestamp = int(time.time() * 1000)
# Include the timestamp in the value to allow
# duplicate message
value = json.dumps({"timestamp": timestamp, "message": message})
log.msg("Store message in {} sorted set with score {}".format(
channel, timestamp))
# Set the timestamp as score to easily fetch the values within a
# time period using zrangebyscore
yield self.factory.client.zadd(channel, timestamp, value)


if __name__ == '__main__':
import sys
from twisted.internet import reactor
from txredis.client import RedisClientFactory
log.startLogging(sys.stdout)
redis_factory = RedisClientFactory()
reactor.connectTCP('localhost', 6379, redis_factory)
redis_pub_service = RedisPublishService(redis_factory)
gw_factory = BasicGatewayFactory(redis_pub_service, "test")
reactor.listenTCP(8000, gw_factory)
reactor.run()
--

test_example.py
--
from twisted.internet import reactor, defer, protocol
from twisted.python import log
from twisted.test import proto_helpers
from twisted.trial.unittest import TestCase
from txredis.client import RedisSubscriber, RedisClientFactory
from txredis.testing import REDIS_HOST, REDIS_PORT
from gateway.example import BasicGatewayFactory, RedisPublishService


class GatewayServiceTestCase(TestCase):

@defer.inlineCallbacks
def setUp(self):
self.redis_factory = RedisClientFactory()
reactor.connectTCP(REDIS_HOST, REDIS_PORT, self.redis_factory)
yield self.redis_factory.deferred
self.redis_pub_service = RedisPublishService(self.redis_factory)
self.factory = BasicGatewayFactory(self.redis_pub_service, "test")
self.server = self.factory.buildProtocol(None)
self.transport = proto_helpers.StringTransportWithDisconnection()
self.transport.protocol = self.server
self.server.makeConnection(self.transport)

class MySubscriber(RedisSubscriber):
def __init__(self, *args, **kwargs):
RedisSubscriber.__init__(self, *args, **kwargs)
self.msg_channel = None
self.msg_message = None
self.msg_received = defer.Deferred()

def messageReceived(self, channel, message):
log.msg("Message received!")
self.msg_channel = channel
 

[Twisted-Python] [#XSZ-691-39878]: Help with trial test failure

2013-08-04 Thread TeamSpeak Piracy
Twisted general discussion,

Thank you for contacting us. This is an automated response confirming the receipt of your ticket. One of our agents will get back to you as soon as possible. For your records, the details of the ticket are listed below. When replying, please make sure that the ticket ID is kept in the subject line to ensure that your replies are tracked appropriately.

   Ticket ID: XSZ-691-39878
   Subject: [Twisted-Python] Help with trial test failure
   Department: Piracy [English]
   Type: Issue
   Status: Open

You can check the status of or reply to this ticket online at: https://support.teamspeakusa.com/index.php?/Tickets/Ticket/View/XSZ-691-39878

Kind regards,

TeamSpeak USA, Inc.
TeamSpeak Piracy
e-Mail: pir...@teamspeakusa.com
Visit: http://www.TeamSpeak.com
Knowledgebase: http://support.TeamSpeakUSA.com

Hours of operation for this department are Monday - Friday, 9AM to 5PM Pacific Time (UTC-8). We are committed to responding to your inquiry within 48 hours, and typically will reply within 24 hours, excluding weekends and holidays.




___
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python


Re: [Twisted-Python] Help with trial test failure

2013-08-04 Thread Itamar Turner-Trauring

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


___
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python


Re: [Twisted-Python] Help with trial test failure

2013-08-04 Thread Benjamin BERTRAND

Le 4 août 2013 à 21:55, Itamar Turner-Trauring  a écrit :

> 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
> 

I tried to add self.flushLoggedErrors() at the end of the test_messageReceived, 
but it doesn't help.
What is strange is that if I remove the 
self.redis_factory.client.transport.loseConnection() in the tearDown method, 
the test is OK, but of course I get an error because the reactor was unclean:

gateway.test.test_example
  GatewayServiceTestCase
test_messageReceived ...   [OK]
   [ERROR]

===
[ERROR]
Traceback (most recent call last):
Failure: twisted.trial.util.DirtyReactorAggregateError: Reactor was unclean.
Selectables:
< to ('localhost', 6381) at 10d06de90>

gateway.test.test_example.GatewayServiceTestCase.test_messageReceived
---
Ran 1 tests in 0.008s

Could the logging come from the tearDown method??


___
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python