Ticket #6577: twisted_docs.py

File twisted_docs.py, 993 bytes (added by Chris Wolfe, 8 years ago)

An example test containing a fake transport

Line 
1from zope.interface import implements
2
3from twisted.internet.interfaces import IUDPTransport
4from twisted.trial import unittest
5
6from ..client import client
7
8class FakeUdpTransport(object):
9    """ Instead of connecting through the network, this transport
10    writes the broadcast messages to a variable that can be
11    checked. """
12
13    implements(IUDPTransport)
14
15    def __init__(self):
16        self.msgs = []
17
18    def write(self, packet, addr=None):
19        self.msgs.append(repr(packet))
20
21    def connect(host, port):
22        pass
23
24    def getHost():
25        pass
26
27    def stopListening():
28        pass
29
30class BroadcastServerTests(unittest.TestCase):
31    def setUp(self):
32        self.protocol = client.MulticastPingClient()
33        self.tr = FakeUdpTransport()
34        self.protocol.transport = self.tr
35       
36    def test_broadcast(self):
37        self.protocol.startProtocol()
38        self.assertTrue(len(self.tr.msgs) > 0)
39        self.assertTrue(self.tr.msgs[0] == "'Client: Ping")