def test_update(self): poll = Poller(get_hub().loop) cbargs = [] called = Event() def callback(fd, events): cbargs.append((fd, events)) called.set() s1, s2 = socketpair() fd = s2.fileno() handle = poll.add_callback(fd, WRITABLE, callback) self.assertIsNotNone(handle) called.wait(0.1) self.assertEqual(cbargs, [(fd, WRITABLE)]) del cbargs[:] called.clear() poll.update_callback(fd, handle, READABLE | WRITABLE) s1.send(b'x') called.wait(0.1) self.assertEqual(cbargs, [(fd, READABLE | WRITABLE)]) del cbargs[:] called.clear() poll.close() s1.close() s2.close()
def test_basic(self): s1, s2 = socketpair() fd = s2.fileno() mp = MultiPoll(get_hub().loop, fd) check_mpoll(mp) cbargs = [] called = Event() def callback(fd, events): cbargs.append((fd, events)) called.set() mp.add_callback(READABLE, callback) check_mpoll(mp) called.wait(0.01) self.assertEqual(cbargs, []) s1.send(b'x') called.wait(0.1) self.assertEqual(cbargs, [(fd, READABLE)]) self.assertEqual(s2.recv(10), b'x') del cbargs[:] called.clear() called.wait(0.01) self.assertEqual(cbargs, []) mp.close() check_mpoll(mp) s1.close() s2.close()
def test_remove(self): s1, s2 = socketpair() fd = s2.fileno() mp = MultiPoll(get_hub().loop, fd) cbargs = [] called = Event() def callback(arg=0): def _callback(fd, events): cbargs.append((fd, events, arg)) called.set() return _callback h1 = mp.add_callback(READABLE, callback(0)) check_mpoll(mp) h2 = mp.add_callback(READABLE, callback(1)) check_mpoll(mp) s1.send(b'x') called.wait(0.1) self.assertEqual(cbargs, [(fd, READABLE, 0), (fd, READABLE, 1)]) del cbargs[:] called.clear() mp.remove_callback(h1) check_mpoll(mp) called.wait(0.1) self.assertEqual(cbargs, [(fd, READABLE, 1)]) mp.remove_callback(h2) check_mpoll(mp) mp.close() check_mpoll(mp) s1.close() s2.close()
def test_multiple(self): s1, s2 = socketpair() fd = s2.fileno() mp = MultiPoll(get_hub().loop, fd) cbargs = [] called = Event() def callback(arg=0): def _callback(fd, events): cbargs.append((fd, events, arg)) called.set() return _callback mp.add_callback(READABLE, callback(0)) check_mpoll(mp) mp.add_callback(READABLE, callback(1)) check_mpoll(mp) mp.add_callback(WRITABLE, callback(2)) check_mpoll(mp) mp.add_callback(WRITABLE, callback(3)) check_mpoll(mp) called.wait(0.1) self.assertEqual(cbargs, [(fd, WRITABLE, 2), (fd, WRITABLE, 3)]) del cbargs[:] called.clear() s1.send(b'x') called.wait(0.1) self.assertEqual(cbargs, [(fd, READABLE, 0), (fd, READABLE, 1), (fd, WRITABLE, 2), (fd, WRITABLE, 3)]) self.assertEqual(s2.recv(10), b'x') mp.close() check_mpoll(mp) s1.close() s2.close()
def test_close(self): s1, s2 = socketpair() fd = s2.fileno() mp = MultiPoll(get_hub().loop, fd) cbargs = [] called = Event() def callback(fd, events): cbargs.append((fd, events)) called.set() h1 = mp.add_callback(READABLE, callback) h2 = mp.add_callback(READABLE, callback) s1.send(b'x') called.wait(0.1) self.assertEqual(cbargs, [(fd, READABLE), (fd, READABLE)]) del cbargs[:] called.clear() mp.close() called.wait(0.01) self.assertEqual(cbargs, []) self.assertRaises(RuntimeError, mp.add_callback, READABLE, callback) self.assertRaises(RuntimeError, mp.remove_callback, h1) self.assertRaises(RuntimeError, mp.remove_callback, h2) self.assertRaises(RuntimeError, mp.update_callback, h1, WRITABLE) self.assertRaises(RuntimeError, mp.update_callback, h2, WRITABLE) s1.close() s2.close()
def test_remove(self): s1, s2 = socketpair() fd = s2.fileno() mp = MultiPoll(get_hub().loop, fd) cbargs = [] called = Event() def callback(arg=0): def _callback(fd, events): cbargs.append((fd, events, arg)) called.set() return _callback h1 = mp.add_callback(READABLE, callback(0)) check_mpoll(mp) h2 = mp.add_callback(READABLE, callback(1)) check_mpoll(mp) s1.send(b'x') called.wait(0.1) self.assertEqual(cbargs, [(fd, READABLE, 0), (fd, READABLE, 1)]) del cbargs[:]; called.clear() mp.remove_callback(h1) check_mpoll(mp) called.wait(0.1) self.assertEqual(cbargs, [(fd, READABLE, 1)]) mp.remove_callback(h2) check_mpoll(mp) mp.close() check_mpoll(mp) s1.close(); s2.close()
def test_multiple(self): s1, s2 = socketpair() fd = s2.fileno() mp = MultiPoll(get_hub().loop, fd) cbargs = [] called = Event() def callback(arg=0): def _callback(fd, events): cbargs.append((fd, events, arg)) called.set() return _callback mp.add_callback(READABLE, callback(0)) check_mpoll(mp) mp.add_callback(READABLE, callback(1)) check_mpoll(mp) mp.add_callback(WRITABLE, callback(2)) check_mpoll(mp) mp.add_callback(WRITABLE, callback(3)) check_mpoll(mp) called.wait(0.1) self.assertEqual(cbargs, [(fd, WRITABLE, 2), (fd, WRITABLE, 3)]) del cbargs[:]; called.clear() s1.send(b'x') called.wait(0.1) self.assertEqual(cbargs, [(fd, READABLE, 0), (fd, READABLE, 1), (fd, WRITABLE, 2), (fd, WRITABLE, 3)]) self.assertEqual(s2.recv(10), b'x') mp.close() check_mpoll(mp) s1.close(); s2.close()
def test_close(self): poll = Poller(get_hub().loop) cbargs = [] called = Event() def callback(fd, events): cbargs.append((fd, events)) called.set() s1, s2 = socketpair() fd = s2.fileno() handle = poll.add_callback(fd, READABLE, callback) self.assertIsNotNone(handle) s1.send(b'x') poll.close() called.wait(0.01) self.assertEqual(cbargs, []) self.assertRaises(RuntimeError, poll.add_callback, fd, READABLE, callback) self.assertRaises(RuntimeError, poll.remove_callback, fd, handle) self.assertRaises(RuntimeError, poll.update_callback, fd, handle, WRITABLE) s1.close(); s2.close()
def test_multiple_fds(self): poll = Poller(get_hub().loop) cbargs = [] called = Event() def callback(fd, events): cbargs.append((fd, events)) called.set() s11, s12 = socketpair() fd1 = s12.fileno() poll.add_callback(fd1, READABLE, callback) s21, s22 = socketpair() fd2 = s22.fileno() poll.add_callback(fd2, READABLE, callback) s11.send(b'x') s21.send(b'x') called.wait() self.assertEqual(cbargs, [(fd1, READABLE), (fd2, READABLE)]) poll.close() s11.close(); s12.close() s21.close(); s22.close()
def test_multiple_fds(self): poll = Poller(get_hub().loop) cbargs = [] called = Event() def callback(fd, events): cbargs.append((fd, events)) called.set() s11, s12 = socketpair() fd1 = s12.fileno() poll.add_callback(fd1, READABLE, callback) s21, s22 = socketpair() fd2 = s22.fileno() poll.add_callback(fd2, READABLE, callback) s11.send(b'x') s21.send(b'x') called.wait() self.assertEqual(cbargs, [(fd1, READABLE), (fd2, READABLE)]) poll.close() s11.close() s12.close() s21.close() s22.close()
def test_close(self): poll = Poller(get_hub().loop) cbargs = [] called = Event() def callback(fd, events): cbargs.append((fd, events)) called.set() s1, s2 = socketpair() fd = s2.fileno() handle = poll.add_callback(fd, READABLE, callback) self.assertIsNotNone(handle) s1.send(b'x') poll.close() called.wait(0.01) self.assertEqual(cbargs, []) self.assertRaises(RuntimeError, poll.add_callback, fd, READABLE, callback) self.assertRaises(RuntimeError, poll.remove_callback, fd, handle) self.assertRaises(RuntimeError, poll.update_callback, fd, handle, WRITABLE) s1.close() s2.close()
def test_update(self): poll = Poller(get_hub().loop) cbargs = [] called = Event() def callback(fd, events): cbargs.append((fd, events)) called.set() s1, s2 = socketpair() fd = s2.fileno() handle = poll.add_callback(fd, WRITABLE, callback) self.assertIsNotNone(handle) called.wait(0.1) self.assertEqual(cbargs, [(fd, WRITABLE)]) del cbargs[:]; called.clear() poll.update_callback(fd, handle, READABLE|WRITABLE) s1.send(b'x') called.wait(0.1) self.assertEqual(cbargs, [(fd, READABLE|WRITABLE)]) del cbargs[:]; called.clear() poll.close() s1.close(); s2.close()
def test_basic(self): s1, s2 = socketpair() fd = s2.fileno() mp = MultiPoll(get_hub().loop, fd) check_mpoll(mp) cbargs = [] called = Event() def callback(fd, events): cbargs.append((fd, events)) called.set() mp.add_callback(READABLE, callback) check_mpoll(mp) called.wait(0.01) self.assertEqual(cbargs, []) s1.send(b'x') called.wait(0.1) self.assertEqual(cbargs, [(fd, READABLE)]) self.assertEqual(s2.recv(10), b'x') del cbargs[:]; called.clear() called.wait(0.01) self.assertEqual(cbargs, []) mp.close() check_mpoll(mp) s1.close(); s2.close()
def test_object_types(self): def sleeper(): gruvi.sleep(0.1) objects = [] for i in range(2): fib = Fiber(sleeper) fib.start() objects.append(fib) for i in range(2): objects.append(Event()) for i in range(2): objects.append(Future()) for i in range(2): proc = Process() proc.spawn('true') objects.append(proc) result = [] def waiter(): result.append(gruvi.wait(objects)) fib = Fiber(waiter) fib.start() gruvi.sleep(0) self.assertTrue(fib.alive) objects[2].set() objects[3].set() objects[4].set_result(True) objects[5].set_exception(RuntimeError) fib.join() self.assertFalse(fib.alive) self.assertEqual(set(result[0][0]), set(objects)) self.assertFalse(objects[0].alive) self.assertFalse(objects[1].alive) self.assertEqual(objects[6].returncode, 0) self.assertEqual(objects[7].returncode, 0) for obj in objects: self.assertIsNone(obj._callbacks) objects[6].close() objects[7].close()
def test_close(self): s1, s2 = socketpair() fd = s2.fileno() mp = MultiPoll(get_hub().loop, fd) cbargs = [] called = Event() def callback(fd, events): cbargs.append((fd, events)) called.set() h1 = mp.add_callback(READABLE, callback) h2 = mp.add_callback(READABLE, callback) s1.send(b'x') called.wait(0.1) self.assertEqual(cbargs, [(fd, READABLE), (fd, READABLE)]) del cbargs[:]; called.clear() mp.close() called.wait(0.01) self.assertEqual(cbargs, []) self.assertRaises(RuntimeError, mp.add_callback, READABLE, callback) self.assertRaises(RuntimeError, mp.remove_callback, h1) self.assertRaises(RuntimeError, mp.remove_callback, h2) self.assertRaises(RuntimeError, mp.update_callback, h1, WRITABLE) self.assertRaises(RuntimeError, mp.update_callback, h2, WRITABLE) s1.close(); s2.close()
def test_update(self): s1, s2 = socketpair() fd = s2.fileno() mp = MultiPoll(get_hub().loop, fd) cbargs = [] called = Event() def callback(fd, events): cbargs.append((fd, events)) called.set() h1 = mp.add_callback(READABLE, callback) check(mp) s1.send(b'x') called.wait(0.1) self.assertEqual(cbargs, [(fd, READABLE)]) del cbargs[:]; called.clear() mp.update_callback(h1, READABLE|WRITABLE) check(mp) s1.send(b'x') called.wait(0.1) self.assertEqual(cbargs, [(fd, READABLE|WRITABLE)]) del cbargs[:]; called.clear() mp.update_callback(h1, WRITABLE) check(mp) s1.send(b'x') called.wait(0.1) self.assertEqual(cbargs, [(fd, WRITABLE)]) del cbargs[:]; called.clear() mp.update_callback(h1, 0) check(mp) s1.send(b'x') called.wait(0.01) self.assertEqual(cbargs, []) mp.close() check(mp) s1.close(); s2.close()