开发者

Twisted daemon unittest

I've started a client/server project at work using Twisted (I'm a newcomer, so not much experience). I probably did setup things the wrong way/order, because now I'm a little stuck with a Daemon server (using twistd --python to launch it).

I'm wondering if I've to re-implement the server as a standard process to use it in my unittest module?

Here's part of the code to launch the server as a Daemon in the server module (you'll probably recognize part of krondo's articles in this):

class TwistedHawkService(service.Service):
    def startService(self):
        ''''''
        service.Service.startService(self)
        log.msg('TwistedHawkService running ...')


# Configuration
port = 10000
iface = 'localhost'

topService = service.MultiService()

thService = TwistedHawkService()
thService.setServiceParent(topService)

factory = ReceiverFactory(thService)

tcpService = internet.TCPServer(port, factory, interface=iface)
tcpService.setServiceParent(topService)

application = service.Application("TwistedHawkService")

topService.setServiceParent(application)

I tried copy/pasting the configuration part in the setUp method:

from mfxTwistedHawk.client import mfxTHClient
from mfxTwistedHawk.server import mfxTHServer


class RequestTestCase(TestCase):
    def setUp(self):
        # Configuration
        port = 10000
        iface = 'localhost'

        self.topService = service.MultiService()

        thService = mfxTHServer.TwistedHawkService()
        thService.setServiceParent(self.topService)

        factory = mfxTHServer.ReceiverFactory(thService)

        tcpService = internet.TCPServer(port, factory, interface=iface)
        tcpService.setServiceParent(self.topService)

        application = service.Application("TwistedHawkService")

        self.topService.setServiceParent(application)

    def test_connection(self):
        mfxTHClient.requestMain('someRequest')

... but of course using trial unittest.py doesn't start it a daemon, so my client can't reach it.

Any advice of how to setup things wo开发者_如何学运维uld be appreciated.

Thanks!

Edit: Managed to make everything works with this and this, but still feel unsure about the whole thing:

def setUp(self):
    # Configuration
    port = 10000
    iface = 'localhost'

    service = mfxTHServer.TwistedHawkService()
    factory = mfxTHServer.ReceiverFactory(service)
    self.server = reactor.listenTCP(port, factory, interface=iface)

Is it ok to have a daemon implementation for production and standard process for unittest?


Is it ok to have a daemon implementation for production and standard process for unittest?

Your unit test isn't for Twisted's daemonization functionality. It's for the custom application/protocol/server/whatever functionality that you implemented. In general, in a unit test, you want to involve as little code as possible. So in general, it's quite okay, and even preferable, to have your unit tests not daemonize. In fact, you probably want to write some unit tests that don't even listen on a real TCP port, but just call methods on your service, factory, and protocol classes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜