def test_sendFile(self):
        fileToSend = StringIO(fileData)
        clock = Clock()
        cooperator = Cooperator(scheduler=lambda f: clock.callLater(0.1, f))
        d1 = self.client.sendFile(fileToSend, cooperator=cooperator)
        self.pump.pump()
        self.pump.pump()
        self.assertNoResult(d1)
        d2 = deliverContent(self.server.producer)

        clock.advance(1)
        while self.pump.pump():
            clock.advance(1)
        self.successResultOf(d1)
        self.assertEqual(self.successResultOf(d2), fileData)
    def test_deliverContent(self):
        fileToSend = StringIO(fileData)
        clock = Clock()
        cooperator = Cooperator(scheduler=lambda f: clock.callLater(0.1, f))
        @ProducerStarter
        def registerWithConsumer(consumer):
            producer = FileBodyProducer(fileToSend, cooperator=cooperator)
            d = producer.startProducing(consumer)
            d.addCallback(lambda ign: consumer.unregisterProducer())
            d.addErrback(log.err, 'error producing file body')
            consumer.registerProducer(producer, True)
        self.client.callRemote(SendProducer, producer=registerWithConsumer)
        self.pump.pump()
        d = deliverContent(self.server.producer)

        while self.pump.pump():
            clock.advance(1)
        self.assertEqual(self.successResultOf(d), fileData)
 def test_sendFileDisconnection(self):
     fileToSend = StringIO(fileData)
     clock = Clock()
     cooperator = Cooperator(scheduler=lambda f: clock.callLater(0.1, f))
     d1 = self.client.sendFile(fileToSend, cooperator=cooperator)
     self.assertFailure(d1, FakeDisconnectedError)
     self.pump.pump()
     self.pump.pump()
     self.assertNoResult(d1)
     d2 = deliverContent(self.server.producer)
     self.assertFailure(d2, FakeDisconnectedError)
     self.pump.pump()
     clock.advance(1)
     f = failure.Failure(FakeDisconnectedError())
     self.client.connectionLost(f)
     self.server.connectionLost(f)
     self.assertEqual(len(self.flushLoggedErrors(FakeDisconnectedError)), 2)
     self.successResultOf(d1)
     self.successResultOf(d2)
 def receiveProducer(self, producer, name):
     d = deliverContent(producer)
     d.addCallback(self._gotData, name)
     return {}