Ticket #5781: false-factory.patch

File false-factory.patch, 7.3 KB (added by Julian, 21 months ago)
  • twisted/internet/protocol.py

    diff --git a/twisted/internet/protocol.py b/twisted/internet/protocol.py
    index 4c455bd..0122c05 100644
    a b class Factory: 
    104104        return p 
    105105 
    106106 
     107 
    107108class ClientFactory(Factory): 
    108109    """A Protocol factory for clients. 
    109110 
    class ClientFactory(Factory): 
    136137        """ 
    137138 
    138139 
     140 
     141class FalseFactory(ClientFactory): 
     142    """ 
     143    Factory that always builds the same protocol instance. 
     144 
     145    @ivar instance: The L{Protocol} which will be "built" each time. 
     146    """ 
     147    def __init__(self, instance): 
     148        self.instance = instance 
     149 
     150 
     151    def __repr__(self): 
     152        return "<%s: %r>" % (self.__class__.__name__, self.instance) 
     153 
     154 
     155    def buildProtocol(self, addr): 
     156        """ 
     157        Return the pre-constructed protocol instance. 
     158        """ 
     159        return self.instance 
     160 
     161 
     162 
    139163class _InstanceFactory(ClientFactory): 
    140164    """ 
    141165    Factory used by ClientCreator. 
    class FileWrapper: 
    824848__all__ = ["Factory", "ClientFactory", "ReconnectingClientFactory", "connectionDone", 
    825849           "Protocol", "ProcessProtocol", "FileWrapper", "ServerFactory", 
    826850           "AbstractDatagramProtocol", "DatagramProtocol", "ConnectedDatagramProtocol", 
    827            "ClientCreator"] 
     851           "ClientCreator", "FalseFactory"] 
  • twisted/internet/test/connectionmixins.py

    diff --git a/twisted/internet/test/connectionmixins.py b/twisted/internet/test/connectionmixins.py
    index a239f0d..1ef803d 100644
    a b from twisted.python.runtime import platform 
    2121from twisted.python.log import ILogContext, msg, err 
    2222from twisted.internet.defer import Deferred, gatherResults 
    2323from twisted.internet.interfaces import IConnector, IReactorFDSet 
    24 from twisted.internet.protocol import ClientFactory, Protocol, ServerFactory 
     24from twisted.internet.protocol import ( 
     25    ClientFactory, Protocol, ServerFactory, FalseFactory) 
    2526from twisted.trial.unittest import SkipTest 
    2627from twisted.internet.test.reactormixins import needsRunningReactor 
    2728from twisted.test.test_tcp import ClosingProtocol 
    class EndpointCreator: 
    129130 
    130131 
    131132 
    132 class _SingleProtocolFactory(ClientFactory): 
    133     """ 
    134     Factory to be used by L{runProtocolsWithReactor}. 
    135  
    136     It always returns the same protocol (i.e. is intended for only a single 
    137     connection). 
    138     """ 
    139  
    140     def __init__(self, protocol): 
    141         self._protocol = protocol 
    142  
    143  
    144     def buildProtocol(self, addr): 
    145         return self._protocol 
    146  
    147  
    148  
    149133def runProtocolsWithReactor(reactorBuilder, serverProtocol, clientProtocol, 
    150134                            endpointCreator): 
    151135    """ 
    def runProtocolsWithReactor(reactorBuilder, serverProtocol, clientProtocol, 
    170154    reactor = reactorBuilder.buildReactor() 
    171155    serverProtocol._setAttributes(reactor, Deferred()) 
    172156    clientProtocol._setAttributes(reactor, Deferred()) 
    173     serverFactory = _SingleProtocolFactory(serverProtocol) 
    174     clientFactory = _SingleProtocolFactory(clientProtocol) 
     157    serverFactory = FalseFactory(serverProtocol) 
     158    clientFactory = FalseFactory(clientProtocol) 
    175159 
    176160    # Listen on a port: 
    177161    serverEndpoint = endpointCreator.server(reactor) 
  • twisted/internet/test/test_protocol.py

    diff --git a/twisted/internet/test/test_protocol.py b/twisted/internet/test/test_protocol.py
    index 00d1b8f..94e9a7a 100644
    a b from twisted.internet.interfaces import (IProtocol, ILoggingContext, 
    1616from twisted.internet.defer import CancelledError 
    1717from twisted.internet.protocol import ( 
    1818    Protocol, ClientCreator, Factory, ProtocolToConsumerAdapter, 
    19     ConsumerToProtocolAdapter) 
     19    ConsumerToProtocolAdapter, FalseFactory) 
    2020from twisted.trial.unittest import TestCase 
    2121from twisted.test.proto_helpers import MemoryReactorClock, StringTransport 
    2222 
    class AdapterTests(TestCase): 
    412412        protocol.dataReceived(b"hello") 
    413413        self.assertEqual(result, [b"hello"]) 
    414414        self.assertIsInstance(protocol, ConsumerToProtocolAdapter) 
     415 
     416 
     417 
     418class FalseFactoryTests(TestCase): 
     419    """ 
     420    Tests for L{FalseFactory} 
     421    """ 
     422    def setUp(self): 
     423        self.protocol = Protocol() 
     424        self.factory = FalseFactory(self.protocol) 
     425 
     426 
     427    def test_alwaysBuildsTheSameProtocol(self): 
     428        """ 
     429        L{FalseFactory} takes a protocol instance and returns it every time 
     430        L{FalseFactory.buildProtocol} is called. 
     431        """ 
     432        self.assertIs(self.factory.buildProtocol(None), self.protocol) 
     433        self.assertIs(self.factory.buildProtocol(None), self.protocol) 
     434 
     435 
     436    def test_repr(self): 
     437        self.assertEqual( 
     438            repr(self.factory), "<FalseFactory: %r>" % self.protocol) 
  • twisted/protocols/loopback.py

    diff --git a/twisted/protocols/loopback.py b/twisted/protocols/loopback.py
    index 0e0ba3b..8ac5bc1 100644
    a b from zope.interface import implementer 
    1616# Twisted Imports 
    1717from twisted.protocols import policies 
    1818from twisted.internet import interfaces, protocol, main, defer 
     19from twisted.internet.protocol import FalseFactory 
    1920from twisted.internet.task import deferLater 
    2021from twisted.python import failure 
    2122from twisted.internet.interfaces import IAddress 
    class LoopbackRelay: 
    317318 
    318319 
    319320 
    320 class LoopbackClientFactory(protocol.ClientFactory): 
     321class LoopbackClientFactory(FalseFactory): 
    321322 
    322323    def __init__(self, protocol): 
     324        FalseFactory.__init__(self, protocol) 
    323325        self.disconnected = 0 
    324326        self.deferred = defer.Deferred() 
    325         self.protocol = protocol 
    326  
    327     def buildProtocol(self, addr): 
    328         return self.protocol 
    329327 
    330328    def clientConnectionLost(self, connector, reason): 
    331329        self.disconnected = 1 
  • twisted/test/test_policies.py

    diff --git a/twisted/test/test_policies.py b/twisted/test/test_policies.py
    index 956cf19..c149bf9 100644
    a b class SimpleProtocol(protocol.Protocol): 
    4040 
    4141 
    4242 
    43 class SillyFactory(protocol.ClientFactory): 
    44  
    45     def __init__(self, p): 
    46         self.p = p 
    47  
    48     def buildProtocol(self, addr): 
    49         return self.p 
    50  
    51  
    5243class EchoProtocol(protocol.Protocol): 
    5344    paused = False 
    5445 
    class ThrottlingTestCase(unittest.TestCase): 
    364355        n = p.getHost().port 
    365356 
    366357        def _connect123(results): 
    367             reactor.connectTCP("127.0.0.1", n, SillyFactory(c1)) 
     358            reactor.connectTCP("127.0.0.1", n, protocol.FalseFactory(c1)) 
    368359            c1.dConnected.addCallback( 
    369                 lambda r: reactor.connectTCP("127.0.0.1", n, SillyFactory(c2))) 
     360                lambda r: reactor.connectTCP( 
     361                    "127.0.0.1", n, protocol.FalseFactory(c2))) 
    370362            c2.dConnected.addCallback( 
    371                 lambda r: reactor.connectTCP("127.0.0.1", n, SillyFactory(c3))) 
     363                lambda r: reactor.connectTCP( 
     364                    "127.0.0.1", n, protocol.FalseFactory(c3))) 
    372365            return c3.dDisconnected 
    373366 
    374367        def _check123(results): 
    class ThrottlingTestCase(unittest.TestCase): 
    383376            return c1.dDisconnected 
    384377 
    385378        def _connect4(results): 
    386             reactor.connectTCP("127.0.0.1", n, SillyFactory(c4)) 
     379            reactor.connectTCP("127.0.0.1", n, protocol.FalseFactory(c4)) 
    387380            return c4.dConnected 
    388381 
    389382        def _check4(results):