Ejemplo n.º 1
0
 def test_ctx_manager(self):
     c1 = MockSocket([b'foo', b'quit'])
     c2 = MockSocket([b'foo', b'bar', b'baz', b'fee'])
     s = ListenSocket((c1, c2))
     sel = MockSelector([s, c1, s, c2, c2, (c1, c2), c1, c2, c2])
     with sel:
         sel.register(s, EVENT_READ)
         s.bind(('localhost', 8888))
         s.listen(5)
         while len(sel.get_map()) > 0:
             for k, ev in sel.select():
                 sock = k.fileobj
                 if sock == s:
                     c, _ = sock.accept()
                     sel.register(c, EVENT_READ)
                 else:
                     data = sock.recv(1024)
                     if len(data) == 0:
                         sock.close()
                         sel.unregister(sock)
                     else:
                         sock.send(data)
     s.close()
     sel.close()
     self.assertEqual([((b'foo', ), ), ((b'quit', ), )],
                      c1.send.call_args_list)
     self.assertEqual([((b'foo', ), ), ((b'bar', ), ), ((b'baz', ), ),
                       ((b'fee', ), )], c2.send.call_args_list)
Ejemplo n.º 2
0
 def test_close(self):
     sock = ListenSocket()
     sock.bind(('localhost', 80))
     sock.listen(5)
     sock.close()
     with self.assertRaises(OSError):
         sock.accept()
Ejemplo n.º 3
0
 def test_callable(self):
     c1 = MockSocket()
     c2 = MockSocket()
     sock = ListenSocket([c1, lambda: c2])
     sock.bind(('localhost', 80))
     sock.listen(5)
     c, _ = sock.accept()
     self.assertTrue(c is c1)
     c, _ = sock.accept()
     self.assertTrue(c is c2)
Ejemplo n.º 4
0
 def test_child_list(self):
     c1 = MockSocket()
     c2 = MockSocket()
     self.assertFalse(c1 is c2)
     sock = ListenSocket([c1, c2])
     sock.bind(('localhost', 80))
     sock.listen(5)
     c, _ = sock.accept()
     self.assertTrue(c is c1)
     c, _ = sock.accept()
     self.assertTrue(c is c2)
Ejemplo n.º 5
0
 def test_listen_ok(self):
     sock = ListenSocket()
     sock.bind(('localhost', 80))
     sock.listen(5)
     c, _ = sock.accept()
     self.assertIsInstance(c, MockSocket)
Ejemplo n.º 6
0
 def test_no_bind(self):
     s = ListenSocket()
     with self.assertRaises(OSError):
         s.listen(2)