Example #1
0
 def test_UDP(self):
     """
     Test L{internet.UDPServer} with a random port: starting the service
     should give it valid port, and stopService should free it so that we
     can start a server on the same port again.
     """
     if not interfaces.IReactorUDP(reactor, None):
         raise unittest.SkipTest("This reactor does not support UDP sockets")
     p = protocol.DatagramProtocol()
     t = internet.UDPServer(0, p)
     t.startService()
     num = t._port.getHost().port
     self.assertNotEquals(num, 0)
     def onStop(ignored):
         t = internet.UDPServer(num, p)
         t.startService()
         return t.stopService()
     return defer.maybeDeferred(t.stopService).addCallback(onStop)
Example #2
0
                maybeDeferred(secondPort.stopListening)
            ])
            result.addCallback(lambda ign: passthrough)
            return result

        joined.addBoth(cleanup)
        return joined

    if runtime.platform.isWindows():
        test_multiListen.skip = (
            "on non-linux platforms it appears multiple "
            "processes can listen, but not multiple sockets "
            "in same process?")


if not interfaces.IReactorUDP(reactor, None):
    UDPTestCase.skip = "This reactor does not support UDP"
    ReactorShutdownInteraction.skip = "This reactor does not support UDP"
if not interfaces.IReactorMulticast(reactor, None):
    MulticastTestCase.skip = "This reactor does not support multicast"


def checkForLinux22():
    import os
    if os.path.exists("/proc/version"):
        s = open("/proc/version").read()
        if s.startswith("Linux version"):
            s = s.split()[2]
            if s.split(".")[:2] == ["2", "2"]:
                f = MulticastTestCase.testInterface.im_func
                f.todo = "figure out why this fails in linux 2.2"
    def setDeferred(self, d):
        """
        Set the Deferred which will be called back when datagramReceived is
        called.
        """
        self.d = d

    def datagramReceived(self, bytes, addr):
        if self.d is not None:
            d, self.d = self.d, None
            d.callback(bytes)
        raise BadClientError("Application code is very buggy!")


@skipIf(not interfaces.IReactorUDP(reactor, None),
        "This reactor does not support UDP")
class UDPTests(TestCase):
    def test_oldAddress(self):
        """
        The C{type} of the host address of a listening L{DatagramProtocol}'s
        transport is C{"UDP"}.
        """
        server = Server()
        d = server.startedDeferred = defer.Deferred()
        p = reactor.listenUDP(0, server, interface="127.0.0.1")

        def cbStarted(ignored):
            addr = p.getHost()
            self.assertEqual(addr.type, 'UDP')
            return p.stopListening()
    def setDeferred(self, d):
        """
        Set the Deferred which will be called back when datagramReceived is
        called.
        """
        self.d = d

    def datagramReceived(self, bytes, addr):
        if self.d is not None:
            d, self.d = self.d, None
            d.callback(bytes)
        raise BadClientError("Application code is very buggy!")


@skipIf(not interfaces.IReactorUDP(reactor, None), "This reactor does not support UDP")
class UDPTests(TestCase):
    def test_oldAddress(self):
        """
        The C{type} of the host address of a listening L{DatagramProtocol}'s
        transport is C{"UDP"}.
        """
        server = Server()
        d = server.startedDeferred = defer.Deferred()
        p = reactor.listenUDP(0, server, interface="127.0.0.1")

        def cbStarted(ignored):
            addr = p.getHost()
            self.assertEqual(addr.type, "UDP")
            return p.stopListening()