示例#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)
示例#2
0
 def test_close(self):
     sock = ListenSocket()
     sock.bind(('localhost', 80))
     sock.listen(5)
     sock.close()
     with self.assertRaises(OSError):
         sock.accept()
示例#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)
示例#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)
示例#5
0
 def test_listen_ok(self):
     sock = ListenSocket()
     sock.bind(('localhost', 80))
     sock.listen(5)
     c, _ = sock.accept()
     self.assertIsInstance(c, MockSocket)
示例#6
0
 def test_no_listen(self):
     s = ListenSocket()
     s.bind(('localhost', 80))
     with self.assertRaises(OSError):
         s.accept()