Exemplo 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)
Exemplo n.º 2
0
    def ensure_media_is_in_local_cache(self, file_info):
        """Ensures that the given file is in the local cache. Attempts to
        download it from storage providers if it isn't.

        Args:
            file_info (FileInfo)

        Returns:
            Deferred[str]: Full path to local file
        """
        path = self._file_info_to_path(file_info)
        local_path = os.path.join(self.local_media_directory, path)
        if os.path.exists(local_path):
            defer.returnValue(local_path)

        dirname = os.path.dirname(local_path)
        if not os.path.exists(dirname):
            os.makedirs(dirname)

        for provider in self.storage_providers:
            res = yield provider.fetch(path, file_info)
            if res:
                with res:
                    consumer = BackgroundFileConsumer(open(local_path, "w"))
                    yield res.write_to_consumer(consumer)
                    yield consumer.wait()
                defer.returnValue(local_path)

        raise Exception("file could not be found")
Exemplo n.º 3
0
    def ensure_media_is_in_local_cache(self, file_info):
        """Ensures that the given file is in the local cache. Attempts to
        download it from storage providers if it isn't.

        Args:
            file_info (FileInfo)

        Returns:
            Deferred[str]: Full path to local file
        """
        path = self._file_info_to_path(file_info)
        local_path = os.path.join(self.local_media_directory, path)
        if os.path.exists(local_path):
            defer.returnValue(local_path)

        dirname = os.path.dirname(local_path)
        if not os.path.exists(dirname):
            os.makedirs(dirname)

        for provider in self.storage_providers:
            res = yield provider.fetch(path, file_info)
            if res:
                with res:
                    consumer = BackgroundFileConsumer(
                        open(local_path, "wb"), self.hs.get_reactor()
                    )
                    yield res.write_to_consumer(consumer)
                    yield consumer.wait()
                defer.returnValue(local_path)

        raise Exception("file could not be found")
Exemplo n.º 4
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)
Exemplo n.º 5
0
    def test_pull_consumer(self):
        string_file = StringIO()
        consumer = BackgroundFileConsumer(string_file, reactor=reactor)

        try:
            producer = DummyPullProducer()

            yield producer.register_with_consumer(consumer)

            yield producer.write_and_wait("Foo")

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

            yield producer.write_and_wait("Bar")

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

        yield consumer.wait()

        self.assertTrue(string_file.closed)
Exemplo n.º 6
0
    def test_pull_consumer(self):
        string_file = StringIO()
        consumer = BackgroundFileConsumer(string_file)

        try:
            producer = DummyPullProducer()

            yield producer.register_with_consumer(consumer)

            yield producer.write_and_wait("Foo")

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

            yield producer.write_and_wait("Bar")

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

        yield consumer.wait()

        self.assertTrue(string_file.closed)
Exemplo n.º 7
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)
Exemplo n.º 8
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)