Ticket #6064: 6064.patch

File 6064.patch, 5.3 KB (added by kkpattern, 4 years ago)

Add tests for twisted.internet.protocol.FileWrapper.

  • twisted/internet/test/test_protocol.py

     
    1212
    1313from twisted.python.failure import Failure
    1414from twisted.internet.interfaces import (
    15     IProtocol, ILoggingContext, IProtocolFactory, IConsumer)
     15    IProtocol, ILoggingContext, IProtocolFactory, IConsumer, ITransport)
    1616from twisted.internet.defer import CancelledError
    1717from twisted.internet.protocol import (
    1818    Protocol, ClientCreator, Factory, ProtocolToConsumerAdapter,
    19     ConsumerToProtocolAdapter)
     19    ConsumerToProtocolAdapter, FileWrapper)
    2020from twisted.trial.unittest import TestCase
    2121from twisted.test.proto_helpers import MemoryReactorClock, StringTransport
    2222
     
    428428        protocol.dataReceived(b"hello")
    429429        self.assertEqual(result, [b"hello"])
    430430        self.assertIsInstance(protocol, ConsumerToProtocolAdapter)
     431
     432
     433
     434class FakeFile(object):
     435    """
     436    A fake file-like object that acts enough like a file for L{FileWrapper}.
     437    """
     438    def __init__(self):
     439        self.outchunks = []
     440        self.closed = False
     441
     442
     443    def write(self, chunk):
     444        """
     445        Append the given item to the 'outchunks' list.
     446        """
     447        if self.closed:
     448            raise IOError("the file was closed")
     449        self.outchunks.append(chunk)
     450
     451
     452    def close(self):
     453        """
     454        Set the 'closed' flag to True, explicitly marking that it has been
     455        closed.
     456        """
     457        self.closed = True
     458
     459
     460
     461class FileWrapperTests(TestCase):
     462    """
     463    Tests for L{twisted.internet.protocol.FileWrapper}.
     464    """
     465    def setUp(self):
     466        self.fakeFile = FakeFile()
     467        self.fileWrapper = FileWrapper(self.fakeFile)
     468
     469
     470    def test_interfaces(self):
     471        """
     472        L{FileWrapper} instances provide L{ITransport}.
     473        """
     474        self.assertTrue(verifyObject(ITransport, self.fileWrapper))
     475
     476
     477    def test_write(self):
     478        """
     479        L{FileWrapper.write} writes data into the file-like object.
     480        """
     481        text = "test write"
     482        self.fileWrapper.write(text)
     483        self.assertEqual(self.fakeFile.outchunks[-1], text)
     484
     485
     486    def test_registerProducer(self):
     487        """
     488        L{FileWrapper.registerProducer} records the arguments supplied to it
     489        as instance attributes.
     490        """
     491        producer = object()
     492        streaming = object()
     493        self.fileWrapper.registerProducer(producer, streaming)
     494        self.assertIdentical(self.fileWrapper.producer, producer)
     495        self.assertIdentical(self.fileWrapper.streamingProducer, streaming)
     496
     497
     498    def test_registerProducerWithNoStreaming(self):
     499        """
     500        L{FileWrapper.registerProducer} records the producer as instance
     501        attribute and calls C{producer.resumeProducing} when supplied producer
     502        without streaming.
     503        """
     504        class FakeProducer(object):
     505            def __init__(self):
     506                self.resumeProducingCalled = False
     507
     508
     509            def resumeProducing(self):
     510                self.resumeProducingCalled = True
     511
     512        producer = FakeProducer()
     513        self.fileWrapper.registerProducer(producer, None)
     514        self.assertTrue(producer.resumeProducingCalled)
     515
     516
     517    def _assertProducerUnregistered(self):
     518        """
     519        Assert L{FileWrapper.unregisterProducer} has been called successfully.
     520        """
     521        self.assertEqual(self.fileWrapper.producer, None)
     522
     523
     524    def test_unregisterProducer(self):
     525        """
     526        L{FileWrapper.unregisterProducer} causes the transport to forget about
     527        the registered producer.
     528        """
     529        producer = object()
     530        streaming = object()
     531        self.fileWrapper.registerProducer(producer, streaming)
     532        self.fileWrapper.unregisterProducer()
     533        self._assertProducerUnregistered()
     534
     535
     536    def _assertConnectionLost(self):
     537        """
     538        Assert L{FileWrapper.loseConnection} has been called successfully.
     539        """
     540        self.assertEqual(self.fileWrapper.closed, 1)
     541        self.assertTrue(self.fakeFile.closed)
     542
     543
     544    def test_stopConsuming(self):
     545        """
     546        L{FileWrapper.stopConsuming} causes the transport to unregister
     547        producer and lose connection.
     548        """
     549        self.fileWrapper.stopConsuming()
     550        self._assertProducerUnregistered()
     551        self._assertConnectionLost()
     552
     553
     554    def test_writeSequence(self):
     555        """
     556        L{FileWrapper.writeSequence} concatenates the content in C{iovec} and
     557        writes it into the file-like object.
     558        """
     559        iovec = ["test", "write", "sequence"]
     560        self.fileWrapper.writeSequence(iovec)
     561        self.assertEqual(self.fakeFile.outchunks[-1], "".join(iovec))
     562
     563
     564    def test_loseConnection(self):
     565        """
     566        L{FileWrapper.loseConnection} changes the C{'closed'} of the transport
     567        to C{'1'} and closes the C{'file'}
     568        """
     569        self.fileWrapper.loseConnection()
     570        self._assertConnectionLost()
     571
     572
     573    def test_stopProducing(self):
     574        """
     575        L{FileWrapper.stopProducing} causes the transport to lose connection.
     576        """
     577        self.fileWrapper.stopProducing()
     578        self._assertConnectionLost()