示例#1
0
    async def test_empty_file(self):
        queue = []
        async_queue = FakeAsyncQueue(queue)
        file = File(ByteArrayFile(bytearray(b"")), queue=async_queue)

        data = file.data()

        with self.assertRaises(StopAsyncIteration):
            await data.__anext__()
示例#2
0
    async def test_file_size_lt_read_buffer_size_lt_queue_item_size(self):
        queue = []
        async_queue = FakeAsyncQueue(queue)
        file = File(ByteArrayFile(bytearray(b"\x01\x02\x03\x04\x05")),
                    queue=async_queue,
                    read_buffer_size=6,
                    queue_item_size=7)
        items = []
        async for item in file.data():
            items.append(item)

        self.assertTrue(items == [bytearray(b"\x01\x02\x03\x04\x05")])
示例#3
0
    async def test_queue_item_size_eq_read_buffer_size_lt_file_size(self):
        queue = []
        async_queue = FakeAsyncQueue(queue)
        file = File(ByteArrayFile(bytearray(b"\x01\x02\x03\x04\x05")),
                    queue=async_queue,
                    read_buffer_size=1,
                    queue_item_size=1)
        result = [bytearray([i + 1]) for i in range(5)]
        items = []
        async for item in file.data():
            items.append(item)

        self.assertTrue(items == result)
示例#4
0
    async def test_read_without_consuming_queue(self):
        queue = []
        async_queue = FakeAsyncQueue(queue)
        file = File(ByteArrayFile(bytearray(b"\x01\x02\x03\x04\x05")),
                    queue=async_queue,
                    read_buffer_size=1,
                    queue_item_size=1)

        result = [bytearray([i + 1]) for i in range(5)]
        result.extend([None, None])

        await file.read()
        self.assertEqual(async_queue.how_many_tasks_done(), 0)

        async for item in file.data():
            pass
        self.assertEqual(async_queue.how_many_tasks_done(), 6)

        self.assertEqual(queue, result)
示例#5
0
async def asyncfileserver(file_name: str) -> int:
    async with aiofiles.open(file_name, "rb") as async_file:
        streams = await create_standard_streams(sys.stdin.buffer,
                                                sys.stdout.buffer,
                                                sys.stderr.buffer)
        reader, writer, _ = streams
        input = AsyncConsoleInput(reader)
        output = AsyncConsoleOutput(writer)
        arbiter = Arbiter(input, output, ViewDataFactory(),
                          ConfirmCommandFactory())
        queue = ConfirmPutQueue(arbiter, asyncio.Queue())
        file = File(file=async_file, queue=queue)

        client = Client(queue, output)

        read_file = asyncio.create_task(file.read())
        write_console = asyncio.create_task(client.write())

        await asyncio.gather(read_file, write_console)

        return ExitStatus.success