def test_leak(self): """Test map leak.""" # There was a bug where we were leaking events in the event map. self.fd = os.open('test_lio_file', Test.FLAG) event_size = len(coro.event_map) filesize = 512 * 1024 orig_data = os.urandom(filesize) coro.aio_write(self.fd, orig_data, 0) for x in xrange(100): size = random.randint(1, filesize) offset = random.randint(0, filesize - size) data = coro.aio_read(self.fd, size, offset) self.assertEqual(data, orig_data[offset:offset + size]) self.assertEqual(event_size, len(coro.event_map)) # Try error path. os.close(self.fd) for x in xrange(100): size = random.randint(1, filesize) offset = random.randint(0, filesize - size) self.assertRaises(OSError, coro.aio_read, self.fd, size, offset) self.assertEqual(event_size, len(coro.event_map))
def test_read_write(self): """Test read/write.""" self.fd = os.open('test_lio_file', Test.FLAG) # Simple 1-byte test. self._read_write('a') # Try something really large. data = os.urandom(5 * 1024 * 1024) self._read_write(data) # Test offset read/write. orig_data = os.urandom(512 * 1024) filesize = len(orig_data) coro.aio_write(self.fd, orig_data, 0) for x in xrange(100): size = random.randint(1, filesize) offset = random.randint(0, filesize - size) data = coro.aio_read(self.fd, size, offset) self.assertEqual(data, orig_data[offset:offset + size]) os.close(self.fd)
def _writer(self, writer_num): selfish_acts = 1 self._num_live_writers += 1 self.writer_status[writer_num] = 'Starting.' try: while 1: if coro.get_now() > self._start_time + self.options.duration*coro.ticks_per_sec: self.writer_status[writer_num] = 'Finished.' return if not selfish_acts % self.options.greediness: self.writer_status[writer_num] = 'Greediness sleep.' coro.sleep_relative(0) self.writer_status[writer_num] = 'Getting area to write.' pos, size, area = self._get_to_write() self.log(3, '%i: write(%i, %i)', writer_num, size, pos) data = t_aio.make_data(pos, size) try: if random.random() < self.options.cancel_percent/100.0: try: self.writer_status[writer_num] = 'Writing with cancel.' num_written = coro.with_timeout(0, coro.aio_write, self._fd, data, long(pos)) except coro.TimeoutError: self._write_cancel_success += 1 else: self._written.append((pos, size)) self._write_cancel_fail += 1 else: self.writer_status[writer_num] = 'Writing.' num_written = coro.aio_write(self._fd, data, long(pos)) if num_written != size: self.log(0, 'ERROR: Failed to write %i bytes (%i written).' % (size, num_written)) self._assertion_errors += 1 else: self._written.append((pos, size)) finally: self._write_locks.delete(area) selfish_acts += 1 #print len(self._written) self._writes_finished += 1 self._bytes_written += size finally: self._worker_semaphore.release() self._num_live_writers -= 1
def test_error(self): """Test error return.""" fd = os.open('test_aio_file', os.O_RDWR | os.O_CREAT | os.O_TRUNC) data = os.urandom(1024 * 1024) r = coro.aio_write(fd, data, 0) self.assertEqual(r, len(data)) self.assertEqual(coro.aio_read(fd, len(data), 0), data) # Rip away the file descriptor. os.close(fd) # Verify it fails. self.assertRaises(oserrors.EBADF, coro.aio_read, fd, len(data), 0) # Try a test that will fail from aio_return. # (NOTE: On FreeBSD before 7, this would actually show up as an # error immediately from aio_error, but in FreeBSD 7 it now appears to # go through the kqueue code path.) soft, hard = resource.getrlimit(resource.RLIMIT_FSIZE) fd = os.open('test_aio_file', os.O_RDWR | os.O_CREAT | os.O_TRUNC) self.assertRaises(oserrors.EFBIG, coro.aio_write, fd, data, soft) os.close(fd)
def test_error(self): """Test error return.""" fd = os.open('test_aio_file', os.O_RDWR|os.O_CREAT|os.O_TRUNC) data = os.urandom(1024 * 1024) r = coro.aio_write(fd, data, 0) self.assertEqual(r, len(data)) self.assertEqual(coro.aio_read(fd, len(data), 0), data) # Rip away the file descriptor. os.close(fd) # Verify it fails. self.assertRaises(oserrors.EBADF, coro.aio_read, fd, len(data), 0) # Try a test that will fail from aio_return. # (NOTE: On FreeBSD before 7, this would actually show up as an # error immediately from aio_error, but in FreeBSD 7 it now appears to # go through the kqueue code path.) soft, hard = resource.getrlimit(resource.RLIMIT_FSIZE) fd = os.open('test_aio_file', os.O_RDWR|os.O_CREAT|os.O_TRUNC) self.assertRaises(oserrors.EFBIG, coro.aio_write, fd, data, soft) os.close(fd)
def _read_write(self, data): n = coro.aio_write(self.fd, data, 0) self.assertEqual(n, len(data)) a = coro.aio_read(self.fd, len(data), 0) self.assertEqual(a, data) os.ftruncate(self.fd, 0)
def write (self, data): result = aio_write (self.fd, data, self.pos) self.pos += result return result