Testing UDP server with Twisted Trial

215 views Asked by At

I used to use Trial for testing TCP servers written with Twisted. Is there any support from Trial to test UDP servers ?

thanks !

1

There are 1 answers

0
t2d On

There is actually no documentation, but support. Use

twisted.test.proto_helpers.FakeDatagramTransport

This is a test for the Echo Server from the UDPTutorial.

from twisted.trial import unittest
from twisted.test import proto_helpers
import echo


class EchoTest(unittest.TestCase):
    def setUp(self):
        self.protocol = echo.Echo()
        self.transport = proto_helpers.FakeDatagramTransport()
        self.protocol.transport = self.transport

    def test_echo(self):
        self.protocol.startProtocol()
        self.assertTrue(len(self.transport.written) == 0)
        # simulate incoming package
        self.protocol.datagramReceived("test", ("127.0.0.1", 55555))
        # check echo has been written as answer on the transport
        msg, addr = self.transport.written[0]
        self.assertEqual(msg, "test")
        self.assertEqual(addr[1], 55555)