Ejemplo n.º 1
0
    def test_push_producer_feedback(self):
        string_file = BlockingStringWrite()
        consumer = BackgroundFileConsumer(string_file, reactor=reactor)

        try:
            producer = NonCallableMock(
                spec_set=["pauseProducing", "resumeProducing"])

            resume_deferred = defer.Deferred()
            producer.resumeProducing.side_effect = lambda: resume_deferred.callback(
                None)

            consumer.registerProducer(producer, True)

            number_writes = 0
            with string_file.write_lock:
                for _ in range(consumer._PAUSE_ON_QUEUE_SIZE):
                    consumer.write("Foo")
                    number_writes += 1

                producer.pauseProducing.assert_called_once()

            yield string_file.wait_for_n_writes(number_writes)

            yield resume_deferred
            producer.resumeProducing.assert_called_once()
        finally:
            consumer.unregisterProducer()

        yield consumer.wait()

        self.assertTrue(string_file.closed)
Ejemplo n.º 2
0
    def test_push_producer_feedback(self):
        string_file = BlockingStringWrite()
        consumer = BackgroundFileConsumer(string_file)

        try:
            producer = NonCallableMock(spec_set=["pauseProducing", "resumeProducing"])

            resume_deferred = defer.Deferred()
            producer.resumeProducing.side_effect = lambda: resume_deferred.callback(None)

            consumer.registerProducer(producer, True)

            number_writes = 0
            with string_file.write_lock:
                for _ in range(consumer._PAUSE_ON_QUEUE_SIZE):
                    consumer.write("Foo")
                    number_writes += 1

                producer.pauseProducing.assert_called_once()

            yield string_file.wait_for_n_writes(number_writes)

            yield resume_deferred
            producer.resumeProducing.assert_called_once()
        finally:
            consumer.unregisterProducer()

        yield consumer.wait()

        self.assertTrue(string_file.closed)
Ejemplo n.º 3
0
    def test_push_consumer(self):
        string_file = BlockingStringWrite()
        consumer = BackgroundFileConsumer(string_file, reactor=reactor)

        try:
            producer = NonCallableMock(spec_set=[])

            consumer.registerProducer(producer, True)

            consumer.write("Foo")
            yield string_file.wait_for_n_writes(1)

            self.assertEqual(string_file.buffer, "Foo")

            consumer.write("Bar")
            yield string_file.wait_for_n_writes(2)

            self.assertEqual(string_file.buffer, "FooBar")
        finally:
            consumer.unregisterProducer()

        yield consumer.wait()

        self.assertTrue(string_file.closed)
Ejemplo n.º 4
0
    def test_push_consumer(self):
        string_file = BlockingStringWrite()
        consumer = BackgroundFileConsumer(string_file)

        try:
            producer = NonCallableMock(spec_set=[])

            consumer.registerProducer(producer, True)

            consumer.write("Foo")
            yield string_file.wait_for_n_writes(1)

            self.assertEqual(string_file.buffer, "Foo")

            consumer.write("Bar")
            yield string_file.wait_for_n_writes(2)

            self.assertEqual(string_file.buffer, "FooBar")
        finally:
            consumer.unregisterProducer()

        yield consumer.wait()

        self.assertTrue(string_file.closed)