def testFsync(self): with tempfile.TemporaryFile() as temp, libaio.AIOContext(1) as io_context: completion_event_list = [] onCompletion = lambda block, res, res2: ( completion_event_list.append((block, res, res2)) ) fsync_block = libaio.AIOBlock( mode=libaio.AIOBLOCK_MODE_FSYNC, target_file=temp, onCompletion=onCompletion, ) try: io_context.submit([fsync_block]) except OSError as exc: if exc.errno != errno.EINVAL: raise raise unittest.SkipTest('FSYNC kernel support missing') fsync_event_list_reference = [(fsync_block, 0, 0)] self.assertEqual( fsync_event_list_reference, io_context.getEvents(min_nr=None), ) self.assertEqual( fsync_event_list_reference, completion_event_list, )
def testReadWrite(self): with tempfile.TemporaryFile() as temp, libaio.AIOContext(1) as io_context: def readall(): temp.seek(0) return temp.read() temp.write(b'blah') temp.flush() completion_event_list = [] onCompletion = lambda block, res, res2: ( completion_event_list.append((block, res, res2)) ) read_buf_0 = bytearray(2) read_buf_1 = mmap(-1, 2) read_block = libaio.AIOBlock( mode=libaio.AIOBLOCK_MODE_READ, target_file=temp, buffer_list=[ read_buf_0, read_buf_1, ], offset=0, onCompletion=onCompletion, ) io_context.submit([read_block]) read_event_list_reference = [(read_block, 4, 0)] self.assertEqual( read_event_list_reference, io_context.getEvents(min_nr=None), ) self.assertEqual( read_event_list_reference, completion_event_list, ) self.assertEqual(read_buf_0, bytearray(b'bl')) self.assertEqual(list(read_buf_1), [b'a', b'h']) del completion_event_list[:] write_block = libaio.AIOBlock( mode=libaio.AIOBLOCK_MODE_WRITE, target_file=temp, buffer_list=[ bytearray(b'u'), bytearray(b'ez'), ], offset=2, onCompletion=onCompletion, ) io_context.submit([write_block]) write_event_list_reference = [(write_block, 3, 0)] self.assertEqual( write_event_list_reference, io_context.getEvents(min_nr=None), ) self.assertEqual( write_event_list_reference, completion_event_list, ) self.assertEqual(readall(), b'bluez')
def __init__(self, path, writer, onCanSend, onCannotSend): self._aio_context = libaio.AIOContext( PENDING_READ_COUNT + MAX_PENDING_WRITE_COUNT, ) self.eventfd = eventfd = libaio.EventFD() self._writer = writer fs_list, hs_list, ss_list = functionfs.getInterfaceInAllSpeeds( interface={ 'bInterfaceClass': functionfs.ch9.USB_CLASS_VENDOR_SPEC, 'iInterface': 1, }, endpoint_list=[ { 'endpoint': { 'bEndpointAddress': functionfs.ch9.USB_DIR_IN, 'bmAttributes': functionfs.ch9.USB_ENDPOINT_XFER_BULK, }, }, { 'endpoint': { 'bEndpointAddress': functionfs.ch9.USB_DIR_OUT, 'bmAttributes': functionfs.ch9.USB_ENDPOINT_XFER_BULK, }, }, ], ) super(USBCat, self).__init__( path, fs_list=fs_list, hs_list=hs_list, ss_list=ss_list, lang_dict={ 0x0409: [ u"USBCat", ], } ) to_host = self.getEndpoint(2) self._aio_recv_block_list = [ libaio.AIOBlock( mode=libaio.AIOBLOCK_MODE_READ, target_file=to_host, buffer_list=[bytearray(BUF_SIZE)], offset=0, eventfd=eventfd, onCompletion=self._onReceived, ) for _ in xrange(PENDING_READ_COUNT) ] self._aio_send_block_list = [] self._real_onCanSend = onCanSend self._real_onCannotSend = onCannotSend self._need_resume = False
def testPoll(self): with libaio.AIOContext(1) as io_context: completion_event_list = [] onCompletion = lambda block, res, res2: ( completion_event_list.append((block, res, res2)) ) read_end, write_end = os.pipe() try: poll_block = libaio.AIOBlock( mode=libaio.AIOBLOCK_MODE_POLL, target_file=read_end, onCompletion=onCompletion, event_mask=select.EPOLLIN, ) try: io_context.submit([poll_block]) except OSError as exc: if exc.errno != errno.EINVAL: raise raise unittest.SkipTest('POLL kernel support missing') self.assertEqual([], io_context.getEvents(min_nr=0)) self.assertEqual([], completion_event_list) os.write(write_end, b'foo') poll_event_list_reference = [( poll_block, select.EPOLLIN | select.EPOLLRDNORM, 0, )] self.assertEqual( poll_event_list_reference, io_context.getEvents(min_nr=None), ) self.assertEqual( poll_event_list_reference, completion_event_list, ) finally: os.close(write_end) os.close(read_end)
def testBasicFunctionality(self): with tempfile.TemporaryFile() as temp, libaio.AIOContext( 1) as io_context: def readall(): temp.seek(0) return temp.read() temp.write(b'blah') temp.flush() completion_event_list = [] onCompletion = lambda block, res, res2: completion_event_list.append( (block, res, res2)) read_buf_0 = bytearray(2) read_buf_1 = bytearray(2) read_block = libaio.AIOBlock( mode=libaio.AIOBLOCK_MODE_READ, target_file=temp, buffer_list=[ read_buf_0, read_buf_1, ], offset=0, onCompletion=onCompletion, ) io_context.submit([read_block]) read_event_list_reference = [(read_block, 4, 0)] self.assertEqual( read_event_list_reference, io_context.getEvents(min_nr=None), ) self.assertEqual( read_event_list_reference, completion_event_list, ) self.assertEqual(read_buf_0, bytearray(b'bl')) self.assertEqual(read_buf_1, bytearray(b'ah')) del completion_event_list[:] write_block = libaio.AIOBlock( mode=libaio.AIOBLOCK_MODE_WRITE, target_file=temp, buffer_list=[ bytearray(b'u'), bytearray(b'ez'), ], offset=2, onCompletion=onCompletion, ) io_context.submit([write_block]) write_event_list_reference = [(write_block, 3, 0)] self.assertEqual( write_event_list_reference, io_context.getEvents(min_nr=None), ) self.assertEqual( write_event_list_reference, completion_event_list, ) self.assertEqual(readall(), b'bluez') del completion_event_list[:] fsync_block = libaio.AIOBlock( mode=libaio.AIOBLOCK_MODE_FSYNC, target_file=temp, onCompletion=onCompletion, ) io_context.submit([fsync_block]) fsync_event_list_reference = [(fsync_block, 0, 0)] self.assertEqual( fsync_event_list_reference, io_context.getEvents(min_nr=None), ) self.assertEqual( fsync_event_list_reference, completion_event_list, ) del completion_event_list[:] fdsync_block = libaio.AIOBlock( mode=libaio.AIOBLOCK_MODE_FDSYNC, target_file=temp, onCompletion=onCompletion, ) io_context.submit([fdsync_block]) fdsync_event_list_reference = [(fdsync_block, 0, 0)] self.assertEqual( fdsync_event_list_reference, io_context.getEvents(min_nr=None), ) self.assertEqual( fdsync_event_list_reference, completion_event_list, ) del completion_event_list[:] poll_block = libaio.AIOBlock( mode=libaio.AIOBLOCK_MODE_POLL, target_file=temp, onCompletion=onCompletion, event_mask=select.EPOLLIN, ) io_context.submit([poll_block]) poll_event_list_reference = [(poll_block, 0, 0)] self.assertEqual( poll_event_list_reference, io_context.getEvents(min_nr=None), ) self.assertEqual( poll_event_list_reference, completion_event_list, ) del completion_event_list[:]