Ejemplo n.º 1
0
    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()
Ejemplo n.º 2
0
 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_mpoll(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_mpoll(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_mpoll(mp)
     s1.send(b'x')
     called.wait(0.1)
     self.assertEqual(cbargs, [(fd, WRITABLE)])
     del cbargs[:]; called.clear()
     mp.update_callback(h1, 0)
     check_mpoll(mp)
     s1.send(b'x')
     called.wait(0.01)
     self.assertEqual(cbargs, [])
     mp.close()
     check_mpoll(mp)
     s1.close(); s2.close()
Ejemplo n.º 3
0
    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()
Ejemplo n.º 4
0
 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()
Ejemplo n.º 5
0
    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()
Ejemplo n.º 6
0
 def test_debug(self):
     if not __debug__:
         return
     s1, s2 = socketpair()
     fd = s2.fileno()
     mp = MultiPoll(get_hub().loop, fd)
     def callback(fd, events):
         pass
     mp.add_callback(0, callback)
     mp.add_callback(READABLE, callback)
     mp.add_callback(WRITABLE, callback)
     mp.add_callback(READABLE|WRITABLE, callback)
     check(mp)
     with capture_stdio() as (out, err):
         dump(mp)
     lines = out.readlines()
     self.assertGreater(len(lines), 0)
     self.assertEqual(err.read(), '')
     mp.close()
     s1.close(); s2.close()
Ejemplo n.º 7
0
 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()
Ejemplo n.º 8
0
 def test_basic(self):
     s1, s2 = socketpair()
     fd = s2.fileno()
     mp = MultiPoll(get_hub().loop, fd)
     check(mp)
     cbargs = []
     called = Event()
     def callback(fd, events):
         cbargs.append((fd, events))
         called.set()
     mp.add_callback(READABLE, callback)
     check(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(mp)
     s1.close(); s2.close()
Ejemplo n.º 9
0
 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()
Ejemplo n.º 10
0
    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()