Ticket #6492: producer-paused-attrib.patch

File producer-paused-attrib.patch, 4.2 KB (added by John Popplewell, 9 years ago)
  • twisted/internet/_pollingfile.py

     
    191192    def bufferEmpty(self):
    192193        if self.producer is not None and ((not self.streamingProducer) or
    193194                                          self.producerPaused):
    194             self.producer.producerPaused = 0
     195            self.producerPaused = 0
    195196            self.producer.resumeProducing()
    196197            return True
    197198        return False
  • twisted/internet/test/test_pollingfile.py

     
    44"""
    55Tests for L{twisted.internet._pollingfile}.
    66"""
     7import os
    78
    89from twisted.python.runtime import platform
    910from twisted.trial.unittest import TestCase
     11from twisted.protocols import basic
     12from twisted.internet.test.reactormixins import ReactorBuilder
    1013
     14
     15
    1116if platform.isWindows():
     17    import win32pipe
     18    import win32security
     19
    1220    from twisted.internet import _pollingfile
     21
     22    from twisted.internet._pollingfile import _PollingTimer
     23    _skipNotWindows = None
    1324else:
    14     _pollingfile = None
     25    _skipNotWindows = "Test will run only on Windows."
     26    _PollingTimer = object
     27 
    1528
     29 
     30class PipeRunner(_PollingTimer):
     31    """
     32    Builds, initializes and runs a pair of read/write pipes.
     33    """
     34    def __init__(self, pipeSize, doneReadCB, doneWriteCB, receivedCB, reactor):
     35        _PollingTimer.__init__(self, reactor)
     36        sAttrs = win32security.SECURITY_ATTRIBUTES()
     37        sAttrs.bInheritHandle = 1
     38        hRead, hWrite = win32pipe.CreatePipe(sAttrs, pipeSize)
     39        self.reader = _pollingfile._PollableReadPipe(hRead, receivedCB, doneReadCB)
     40        self.writer = _pollingfile._PollableWritePipe(hWrite, doneWriteCB)
     41        self._addPollableResource(self.reader)
     42        self._addPollableResource(self.writer)
    1643
    1744
     45
     46class TestPollablePipes(ReactorBuilder):
     47    """
     48    Tests for L{_pollingfile._PollableWritePipe} and
     49    L{_pollingfile._PollableReadPipe}.
     50    """
     51    def setUp(self):
     52        self._oldBufferSize = _pollingfile.FULL_BUFFER_SIZE
     53
     54
     55    def tearDown(self):
     56        _pollingfile.FULL_BUFFER_SIZE = self._oldBufferSize
     57
     58
     59    def test_pushProducerPausedAttribute(self):
     60        """
     61        Test write pipe as a consumer using a push producer.
     62
     63        See Ticket #6492: bufferEmpty sets producerPaused attribute on the
     64        producer instead of on itself.
     65        """
     66        pipeSize = 512  # set a small pipe buffer size
     67        _pollingfile.FULL_BUFFER_SIZE = 1024
     68        testMessage = '0' * 2048
     69
     70        class TestProtocol(basic.LineReceiver):
     71            def lineReceived(self, line):
     72                self.testResponse = line
     73                transport.writer.close()
     74
     75        class TestProducer(object):
     76            def resumeProducing(self):
     77                transport.writer.write(testMessage+"\r\n")
     78
     79            def pauseProducing(self):
     80                pass
     81
     82            def stopProducing(self):
     83                pass
     84
     85        reactor = self.buildReactor()
     86        r, w = lambda: reactor.stop(), lambda: None
     87        protocol, producer = TestProtocol(), TestProducer()
     88        pre_producer_attribs = set(dir(producer))
     89        transport = PipeRunner(pipeSize, r, w, protocol.dataReceived, reactor)
     90        transport.writer.registerProducer(producer, True)
     91        producer.resumeProducing()
     92        self.runReactor(reactor)
     93        self.assertEqual(testMessage, protocol.testResponse)
     94        difference = set(dir(producer)).difference(pre_producer_attribs)
     95        self.assertEqual(difference, set())
     96
     97globals().update(TestPollablePipes.makeTestCaseClasses())
     98
     99
    18100class TestPollableWritePipe(TestCase):
    19101    """
    20102    Tests for L{_pollingfile._PollableWritePipe}.
     
    41123
    42124
    43125
     126if _skipNotWindows:
     127    TestPollablePipes.skip = skipMessage
     128    TestPollableWritePipeUnicode.skip = skipMessage
    44129
    45 if _pollingfile is None:
    46     TestPollableWritePipe.skip = "Test will run only on Windows."