'L{Request} objects that have registered producers get blocked and
unblocked according to HTTP/2 flow control.'

def test_producerBlockingUnblocking(self):

    f = FrameFactory()
    b = StringTransport()
    a = H2Connection()
    a.requestFactory = DummyProducerHandlerProxy
    requestBytes = f.clientConnectionPreface()
    requestBytes += f.buildSettingsFrame({h2.settings.SettingCodes.INITIAL_WINDOW_SIZE: 5}).serialize()
    requestBytes += buildRequestBytes(self.getRequestHeaders, [], f)
    a.makeConnection(b)
    for byte in iterbytes(requestBytes):
        a.dataReceived(byte)
    stream = a.streams[1]
    request = stream._request.original
    self.assertTrue(stream._producerProducing)
    request.write('helloworld')
    self.assertFalse(stream._producerProducing)
    self.assertEqual(request.producer.events, ['pause'])
    a.dataReceived(f.buildWindowUpdateFrame(streamID=1, increment=5).serialize())
    self.assertFalse(stream._producerProducing)
    self.assertEqual(request.producer.events, ['pause'])
    a.dataReceived(f.buildWindowUpdateFrame(streamID=0, increment=5).serialize())
    self.assertFalse(stream._producerProducing)
    self.assertEqual(request.producer.events, ['pause'])
    a.dataReceived(f.buildWindowUpdateFrame(streamID=1, increment=5).serialize())
    self.assertTrue(stream._producerProducing)
    self.assertEqual(request.producer.events, ['pause', 'resume'])
    request.write('helloworld')
    self.assertFalse(stream._producerProducing)
    self.assertEqual(request.producer.events, ['pause', 'resume', 'pause'])
    a.dataReceived(f.buildWindowUpdateFrame(streamID=1, increment=50).serialize())
    self.assertTrue(stream._producerProducing)
    self.assertEqual(request.producer.events, ['pause', 'resume', 'pause', 'resume'])
    request.unregisterProducer()
    request.finish()
    def validate(streamID):
        frames = framesFromBytes(b.value())
        self.assertTrue(('END_STREAM' in frames[(-1)].flags))
        dataChunks = [f.data for f in frames if isinstance(f, hyperframe.frame.DataFrame)]
        self.assertEqual(dataChunks, ['helloworld', 'helloworld', ''])
    return a._streamCleanupCallbacks[1].addCallback(validate)
