def test_conversion_fdsync_2_fsync(self): with open('test.txt', 'a') as fp, AIOContext(1) as ctx: block = FsyncBlock(fp) try: submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) get_event_ret = ctx.get_events(1, 1) self.assertEqual(1, len(get_event_ret)) self.assertEqual(block, get_event_ret[0].aio_block) except OSError as err: if _linux_ver >= (4, 18): raise else: self.assertEqual(errno.EINVAL, err.errno) block = block.change_cmd(IOCBCMD.FSYNC) self.assertEqual(fp.fileno(), block.fileno) self.assertEqual(fp, block.file) try: submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) get_event_ret = ctx.get_events(1, 1) self.assertEqual(1, len(get_event_ret)) self.assertEqual(block, get_event_ret[0].aio_block) except OSError as err: if _linux_ver >= (4, 18): raise else: self.assertEqual(errno.EINVAL, err.errno)
def test_poll_modify_masks(self): with AIOContext(2) as ctx, \ socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: host_ip = socket.gethostbyname('www.google.com') sock.connect((host_ip, 80)) sock.sendall('GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n'.encode()) block = PollBlock(sock, select.EPOLLIN) block.event_masks |= select.EPOLLOUT self.assertEqual(select.EPOLLOUT | select.EPOLLIN, block.event_masks) try: submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events_ret = ctx.get_events(1, 1) self.assertEqual(1, len(events_ret)) self.assertIsNone(events_ret[0].buffer) self.assertIsNone(events_ret[0].stripped_buffer()) # TODO # self.assertTupleEqual(tuple(), events_ret) except OSError as err: if _linux_ver >= (4, 18): raise else: self.assertEqual(errno.EINVAL, err.errno) self.assertTrue(ctx.closed)
def test_02_read_w_io_priority(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64), priority_class=IOCBPriorityClass.RT, priority_value=1) self.assertEqual(IOCBPriorityClass.RT, block.priority_class) self.assertEqual(1, block.priority_value) block.set_priority(IOCBPriorityClass.IDLE, 2) self.assertEqual(IOCBPriorityClass.IDLE, block.priority_class) self.assertEqual(2, block.priority_value) block.priority_value = 3 block.priority_class = IOCBPriorityClass.BE self.assertEqual(IOCBPriorityClass.BE, block.priority_class) self.assertEqual(3, block.priority_value) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2)
def test_write_n_read(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w+') as fp: write_block = WriteBlock(fp, self._content) submit_ret = ctx.submit(write_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(write_block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) read_block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(read_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(read_block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2)
def test_2write_n_read(self): _SECOND_CONTENT = 'another\ninput' with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w+') as fp: write_block1 = WriteBlock(fp, self._content) write_block2 = WriteBlock(fp, _SECOND_CONTENT) submit_ret = ctx.submit(write_block1, write_block2) self.assertEqual(2, submit_ret) events = ctx.get_events(2, 2) self.assertEqual(2, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(write_block1, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) read_block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(read_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(_SECOND_CONTENT.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(_SECOND_CONTENT.encode(), event.stripped_buffer()) self.assertEqual(read_block, event.aio_block) self.assertEqual(len(_SECOND_CONTENT), event.response) self.assertEqual(0, event.response2)
def test_conversion_fdsync_2_write(self): with open('test.txt', 'a+') as fp, AIOContext(1) as ctx: block = FDsyncBlock(fp) try: submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) get_event_ret = ctx.get_events(1, 1) self.assertEqual(1, len(get_event_ret)) self.assertEqual(block, get_event_ret[0].aio_block) except OSError as err: if _linux_ver >= (4, 18): raise else: self.assertEqual(errno.EINVAL, err.errno) block = block.change_cmd(IOCBCMD.PWRITE) self.assertEqual(fp.fileno(), block.fileno) self.assertEqual(fp, block.file) content = 'content2'.encode() block.buffer = content submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) get_event_ret = ctx.get_events(1, 1) self.assertEqual(1, len(get_event_ret)) self.assertEqual(block, get_event_ret[0].aio_block) self.assertEqual(fp.read(len(content)), get_event_ret[0].stripped_buffer().decode())
def test_exceed_max_nr(self): with open('/proc/sys/fs/aio-max-nr') as fp: max_nr = int(fp.read()) with self.assertRaises(OSError) as assertion: AIOContext(max_nr + 1) self.assertEqual(errno.EAGAIN, assertion.exception.errno)
def test_empty_submit_n_getevents(self): with AIOContext(2) as ctx: submit_ret = ctx.submit() self.assertEqual(0, submit_ret) events_ret = ctx.get_events(0, 100) self.assertTupleEqual(tuple(), events_ret) self.assertTrue(ctx.closed)
def test_01_write_bytes(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w') as fp: block = WriteBlock(fp, self._content.encode()) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2)
def test_conversion_err_read_2_write(self): with self.assertRaises(ValueError): with open('test.txt', 'a+') as fp, AIOContext(1) as ctx: block = ReadBlock(fp, bytes()) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) get_event_ret = ctx.get_events(1, 1) self.assertEqual(1, len(get_event_ret)) self.assertEqual(block, get_event_ret[0].aio_block) block.change_cmd(IOCBCMD.PWRITE) ctx.submit(block)
def test_02_read_w_rw_flags(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.rw_flag |= IOCBRWFlag.HIPRI submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2)
def test_writev(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w+') as fp: buffers = ('cont'.encode(), 'ent\n'.encode()) block = WriteVBlock(fp, buffers) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] fp.seek(0) self.assertEqual(self._CONTENTS, fp.read(sum(map(lambda x: len(x), buffers)))) self.assertEqual(block, event.aio_block) self.assertEqual(sum(map(lambda x: len(x), buffers)), event.response) self.assertEqual(0, event.response2)
def test_02_read_w_non_zero_offset(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.offset = 1 self.assertEqual(1, block.offset) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode()[1:], event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode()[1:], event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content) - 1, event.response) self.assertEqual(0, event.response2)
def test_02_read_2_str(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: buffer = 'buffer__' block = ReadBlock(fp, buffer) block.length = len(buffer) self.assertEqual(len(buffer), block.length) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2)
def test_readv(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: buffers = (bytearray(4), bytearray(4)) block = ReadVBlock(fp, buffers) new_buffers = (bytearray(4), bytearray(4)) block.buffer = new_buffers self.assertTupleEqual(new_buffers, block.buffer) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._CONTENTS.encode(), functools.reduce(lambda a, b: a + b, event.buffer).rstrip(b'\0')) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._CONTENTS), event.response) self.assertEqual(0, event.response2)
def test_convert_to_same_type(self): with open('test.txt', 'a') as fp, AIOContext(1) as ctx: block = WriteBlock(fp, bytes()) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) get_event_ret = ctx.get_events(1, 1) self.assertEqual(1, len(get_event_ret)) self.assertEqual(block, get_event_ret[0].aio_block) block.change_cmd(IOCBCMD.PWRITE) self.assertEqual(fp.fileno(), block.fileno) self.assertEqual(fp, block.file) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) get_event_ret = ctx.get_events(1, 1) self.assertEqual(1, len(get_event_ret)) self.assertEqual(block, get_event_ret[0].aio_block)
def test_ctx_setup_with_ctxmgr(self): with AIOContext(2) as ctx: pass self.assertTrue(ctx.closed)
def test_empty_getevents(self): with AIOContext(2) as ctx: ret = ctx.get_events(0, 100) self.assertTupleEqual(tuple(), ret) self.assertTrue(ctx.closed)
def test_empty_submit(self): with AIOContext(2) as ctx: ret = ctx.submit() self.assertEqual(0, ret) self.assertTrue(ctx.closed)
def test_double_close(self): ctx = AIOContext(2) ctx.close() ctx.close() self.assertTrue(ctx.closed)
def test_ctx_setup(self): ctx = AIOContext(2) ctx.close() self.assertTrue(ctx.closed)
def test_double_close_with_ctxmgr(self): with AIOContext(2) as ctx: ctx.close() self.assertTrue(ctx.closed)