Exemplo n.º 1
0
 def test_cancel_concurrent(self):
     q = queue.Queue()
     fun = q.put
     timer = Timer()
     scheduler = sched.scheduler(timer.time, timer.sleep)
     now = timer.time()
     event1 = scheduler.enterabs(now + 1, 1, fun, (1,))
     event2 = scheduler.enterabs(now + 2, 1, fun, (2,))
     event4 = scheduler.enterabs(now + 4, 1, fun, (4,))
     event5 = scheduler.enterabs(now + 5, 1, fun, (5,))
     event3 = scheduler.enterabs(now + 3, 1, fun, (3,))
     t = threading.Thread(target=scheduler.run)
     t.start()
     timer.advance(1)
     self.assertEqual(q.get(timeout=TIMEOUT), 1)
     self.assertTrue(q.empty())
     scheduler.cancel(event2)
     scheduler.cancel(event5)
     timer.advance(1)
     self.assertTrue(q.empty())
     timer.advance(1)
     self.assertEqual(q.get(timeout=TIMEOUT), 3)
     self.assertTrue(q.empty())
     timer.advance(1)
     self.assertEqual(q.get(timeout=TIMEOUT), 4)
     self.assertTrue(q.empty())
     timer.advance(1000)
     support.join_thread(t, timeout=TIMEOUT)
     self.assertTrue(q.empty())
     self.assertEqual(timer.time(), 4)
Exemplo n.º 2
0
 def test_cancel_concurrent(self):
     q = queue.Queue()
     fun = q.put
     timer = Timer()
     scheduler = sched.scheduler(timer.time, timer.sleep)
     now = timer.time()
     event1 = scheduler.enterabs(now + 1, 1, fun, (1,))
     event2 = scheduler.enterabs(now + 2, 1, fun, (2,))
     event4 = scheduler.enterabs(now + 4, 1, fun, (4,))
     event5 = scheduler.enterabs(now + 5, 1, fun, (5,))
     event3 = scheduler.enterabs(now + 3, 1, fun, (3,))
     t = threading.Thread(target=scheduler.run)
     t.start()
     timer.advance(1)
     self.assertEqual(q.get(timeout=TIMEOUT), 1)
     self.assertTrue(q.empty())
     scheduler.cancel(event2)
     scheduler.cancel(event5)
     timer.advance(1)
     self.assertTrue(q.empty())
     timer.advance(1)
     self.assertEqual(q.get(timeout=TIMEOUT), 3)
     self.assertTrue(q.empty())
     timer.advance(1)
     self.assertEqual(q.get(timeout=TIMEOUT), 4)
     self.assertTrue(q.empty())
     timer.advance(1000)
     support.join_thread(t, timeout=TIMEOUT)
     self.assertTrue(q.empty())
     self.assertEqual(timer.time(), 4)
Exemplo n.º 3
0
    def do_exceptional_simultaneously_blocking_test(self,
                                                    block_func,
                                                    block_args,
                                                    trigger_func,
                                                    trigger_args,
                                                    expected_exception_class,
                                                    instances_count=5):
        raising_events = [threading.Event() for _ in range(instances_count)]
        blocking_threads = [
            threading.Thread(target=_set_event_on_exception,
                             args=(block_func, block_args,
                                   expected_exception_class, event))
            for event in raising_events
        ]
        for t in blocking_threads:
            t.start()

        blocking_threads[0].join(0.1)  # This is expected to time out.
        if not all(t.is_alive() for t in blocking_threads):
            self.fail("Blocking function returned before trigger.")

        trigger_func(*trigger_args)
        for t in blocking_threads:
            support.join_thread(t, 10)
        if not all(e.is_set() for e in raising_events):
            self.fail("Blocking function never raised expected %s." %
                      expected_exception_class.__name__)
Exemplo n.º 4
0
 def test_enter_concurrent(self):
     q = queue.Queue()
     fun = q.put
     timer = Timer()
     scheduler = sched.scheduler(timer.time, timer.sleep)
     scheduler.enter(1, 1, fun, (1,))
     scheduler.enter(3, 1, fun, (3,))
     t = threading.Thread(target=scheduler.run)
     t.start()
     timer.advance(1)
     self.assertEqual(q.get(timeout=TIMEOUT), 1)
     self.assertTrue(q.empty())
     for x in [4, 5, 2]:
         z = scheduler.enter(x - 1, 1, fun, (x,))
     timer.advance(2)
     self.assertEqual(q.get(timeout=TIMEOUT), 2)
     self.assertEqual(q.get(timeout=TIMEOUT), 3)
     self.assertTrue(q.empty())
     timer.advance(1)
     self.assertEqual(q.get(timeout=TIMEOUT), 4)
     self.assertTrue(q.empty())
     timer.advance(1)
     self.assertEqual(q.get(timeout=TIMEOUT), 5)
     self.assertTrue(q.empty())
     timer.advance(1000)
     support.join_thread(t, timeout=TIMEOUT)
     self.assertTrue(q.empty())
     self.assertEqual(timer.time(), 5)
Exemplo n.º 5
0
 def test_enter_concurrent(self):
     q = queue.Queue()
     fun = q.put
     timer = Timer()
     scheduler = sched.scheduler(timer.time, timer.sleep)
     scheduler.enter(1, 1, fun, (1,))
     scheduler.enter(3, 1, fun, (3,))
     t = threading.Thread(target=scheduler.run)
     t.start()
     timer.advance(1)
     self.assertEqual(q.get(timeout=TIMEOUT), 1)
     self.assertTrue(q.empty())
     for x in [4, 5, 2]:
         z = scheduler.enter(x - 1, 1, fun, (x,))
     timer.advance(2)
     self.assertEqual(q.get(timeout=TIMEOUT), 2)
     self.assertEqual(q.get(timeout=TIMEOUT), 3)
     self.assertTrue(q.empty())
     timer.advance(1)
     self.assertEqual(q.get(timeout=TIMEOUT), 4)
     self.assertTrue(q.empty())
     timer.advance(1)
     self.assertEqual(q.get(timeout=TIMEOUT), 5)
     self.assertTrue(q.empty())
     timer.advance(1000)
     support.join_thread(t, timeout=TIMEOUT)
     self.assertTrue(q.empty())
     self.assertEqual(timer.time(), 5)
    def test_string_producer(self):
        s, event = start_echo_server()
        c = echo_client(b'\n', s.port)
        data = b"hello world\nI'm not dead yet!\n"
        c.push_with_producer(data + SERVER_QUIT)
        asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
        support.join_thread(s)

        self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
Exemplo n.º 7
0
    def test_string_producer(self):
        s, event = start_echo_server()
        c = echo_client(b'\n', s.port)
        data = b"hello world\nI'm not dead yet!\n"
        c.push_with_producer(data+SERVER_QUIT)
        asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
        support.join_thread(s, timeout=TIMEOUT)

        self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
Exemplo n.º 8
0
    def test_simple_producer(self):
        s, event = start_echo_server()
        c = echo_client(b'\n', s.port)
        data = b"hello world\nI'm not dead yet!\n"
        p = asynchat.simple_producer(data + SERVER_QUIT, buffer_size=8)
        c.push_with_producer(p)
        asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
        support.join_thread(s, timeout=TIMEOUT)

        self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
    def numeric_terminator_check(self, termlen):
        # Try reading a fixed number of bytes
        s, event = start_echo_server()
        c = echo_client(termlen, s.port)
        data = b"hello world, I'm not dead yet!\n"
        c.push(data)
        c.push(SERVER_QUIT)
        asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
        support.join_thread(s)

        self.assertEqual(c.contents, [data[:termlen]])
Exemplo n.º 10
0
    def test_empty_line(self):
        # checks that empty lines are handled correctly
        s, event = start_echo_server()
        c = echo_client(b'\n', s.port)
        c.push(b"hello world\n\nI'm not dead yet!\n")
        c.push(SERVER_QUIT)
        asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
        support.join_thread(s, timeout=TIMEOUT)

        self.assertEqual(c.contents,
                         [b"hello world", b"", b"I'm not dead yet!"])
Exemplo n.º 11
0
    def numeric_terminator_check(self, termlen):
        # Try reading a fixed number of bytes
        s, event = start_echo_server()
        c = echo_client(termlen, s.port)
        data = b"hello world, I'm not dead yet!\n"
        c.push(data)
        c.push(SERVER_QUIT)
        asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
        support.join_thread(s, timeout=TIMEOUT)

        self.assertEqual(c.contents, [data[:termlen]])
    def test_empty_line(self):
        # checks that empty lines are handled correctly
        s, event = start_echo_server()
        c = echo_client(b'\n', s.port)
        c.push(b"hello world\n\nI'm not dead yet!\n")
        c.push(SERVER_QUIT)
        asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
        support.join_thread(s)

        self.assertEqual(c.contents,
                         [b"hello world", b"", b"I'm not dead yet!"])
Exemplo n.º 13
0
    def test_none_terminator(self):
        # Try reading a fixed number of bytes
        s, event = start_echo_server()
        c = echo_client(None, s.port)
        data = b"hello world, I'm not dead yet!\n"
        c.push(data)
        c.push(SERVER_QUIT)
        asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
        support.join_thread(s, timeout=TIMEOUT)

        self.assertEqual(c.contents, [])
        self.assertEqual(c.buffer, data)
Exemplo n.º 14
0
 def do_blocking_test(self, block_func, block_args, trigger_func, trigger_args):
     thread = _TriggerThread(trigger_func, trigger_args)
     thread.start()
     try:
         self.result = block_func(*block_args)
         # If block_func returned before our thread made the call, we failed!
         if not thread.startedEvent.is_set():
             self.fail("blocking function %r appeared not to block" %
                       block_func)
         return self.result
     finally:
         support.join_thread(thread, 10) # make sure the thread terminates
Exemplo n.º 15
0
 def do_blocking_test(self, block_func, block_args, trigger_func, trigger_args):
     thread = _TriggerThread(trigger_func, trigger_args)
     thread.start()
     try:
         self.result = block_func(*block_args)
         # If block_func returned before our thread made the call, we failed!
         if not thread.startedEvent.is_set():
             self.fail("blocking function '%r' appeared not to block" %
                       block_func)
         return self.result
     finally:
         support.join_thread(thread, 10) # make sure the thread terminates
    def test_none_terminator(self):
        # Try reading a fixed number of bytes
        s, event = start_echo_server()
        c = echo_client(None, s.port)
        data = b"hello world, I'm not dead yet!\n"
        c.push(data)
        c.push(SERVER_QUIT)
        asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
        support.join_thread(s)

        self.assertEqual(c.contents, [])
        self.assertEqual(c.buffer, data)
Exemplo n.º 17
0
 def _cleanup(self):
     """
     Cleans up the test server. This method should not be called manually,
     it is added to the cleanup queue in the _setup method already.
     """
     # if logout was called already we'd raise an exception trying to
     # shutdown the client once again
     if self.client is not None and self.client.state != 'LOGOUT':
         self.client.shutdown()
     # cleanup the server
     self.server.shutdown()
     self.server.server_close()
     support.join_thread(self.thread, 3.0)
     # Explicitly clear the attribute to prevent dangling thread
     self.thread = None
Exemplo n.º 18
0
 def test_push(self):
     # Issue #12523: push() should raise a TypeError if it doesn't get
     # a bytes string
     s, event = start_echo_server()
     c = echo_client(b'\n', s.port)
     data = b'bytes\n'
     c.push(data)
     c.push(bytearray(data))
     c.push(memoryview(data))
     self.assertRaises(TypeError, c.push, 10)
     self.assertRaises(TypeError, c.push, 'unicode')
     c.push(SERVER_QUIT)
     asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
     support.join_thread(s, timeout=TIMEOUT)
     self.assertEqual(c.contents, [b'bytes', b'bytes', b'bytes'])
Exemplo n.º 19
0
 def _cleanup(self):
     """
     Cleans up the test server. This method should not be called manually,
     it is added to the cleanup queue in the _setup method already.
     """
     # if logout was called already we'd raise an exception trying to
     # shutdown the client once again
     if self.client is not None and self.client.state != 'LOGOUT':
         self.client.shutdown()
     # cleanup the server
     self.server.shutdown()
     self.server.server_close()
     support.join_thread(self.thread, 3.0)
     # Explicitly clear the attribute to prevent dangling thread
     self.thread = None
 def test_push(self):
     # Issue #12523: push() should raise a TypeError if it doesn't get
     # a bytes string
     s, event = start_echo_server()
     c = echo_client(b'\n', s.port)
     data = b'bytes\n'
     c.push(data)
     c.push(bytearray(data))
     c.push(memoryview(data))
     self.assertRaises(TypeError, c.push, 10)
     self.assertRaises(TypeError, c.push, 'unicode')
     c.push(SERVER_QUIT)
     asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
     support.join_thread(s)
     self.assertEqual(c.contents, [b'bytes', b'bytes', b'bytes'])
Exemplo n.º 21
0
 def do_exceptional_blocking_test(self,block_func, block_args, trigger_func,
                                trigger_args, expected_exception_class):
     thread = _TriggerThread(trigger_func, trigger_args)
     thread.start()
     try:
         try:
             block_func(*block_args)
         except expected_exception_class:
             raise
         else:
             self.fail("expected exception of kind %r" %
                              expected_exception_class)
     finally:
         support.join_thread(thread, 10) # make sure the thread terminates
         if not thread.startedEvent.is_set():
             self.fail("trigger thread ended but event never set")
Exemplo n.º 22
0
 def do_exceptional_blocking_test(self,block_func, block_args, trigger_func,
                                trigger_args, expected_exception_class):
     thread = _TriggerThread(trigger_func, trigger_args)
     thread.start()
     try:
         try:
             block_func(*block_args)
         except expected_exception_class:
             raise
         else:
             self.fail("expected exception of kind %r" %
                              expected_exception_class)
     finally:
         support.join_thread(thread, 10) # make sure the thread terminates
         if not thread.startedEvent.is_set():
             self.fail("trigger thread ended but event never set")
    def line_terminator_check(self, term, server_chunk):
        event = threading.Event()
        s = echo_server(event)
        s.chunk_size = server_chunk
        s.start()
        event.wait()
        event.clear()
        time.sleep(0.01)  # Give server time to start accepting.
        c = echo_client(term, s.port)
        c.push(b"hello ")
        c.push(b"world" + term)
        c.push(b"I'm not dead yet!" + term)
        c.push(SERVER_QUIT)
        asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
        support.join_thread(s)

        self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
Exemplo n.º 24
0
    def line_terminator_check(self, term, server_chunk):
        event = threading.Event()
        s = echo_server(event)
        s.chunk_size = server_chunk
        s.start()
        event.wait()
        event.clear()
        time.sleep(0.01)   # Give server time to start accepting.
        c = echo_client(term, s.port)
        c.push(b"hello ")
        c.push(b"world" + term)
        c.push(b"I'm not dead yet!" + term)
        c.push(SERVER_QUIT)
        asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
        support.join_thread(s, timeout=TIMEOUT)

        self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
Exemplo n.º 25
0
    def test_close_when_done(self):
        s, event = start_echo_server()
        s.start_resend_event = threading.Event()
        c = echo_client(b'\n', s.port)
        c.push(b"hello world\nI'm not dead yet!\n")
        c.push(SERVER_QUIT)
        c.close_when_done()
        asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)

        # Only allow the server to start echoing data back to the client after
        # the client has closed its connection.  This prevents a race condition
        # where the server echoes all of its data before we can check that it
        # got any down below.
        s.start_resend_event.set()
        support.join_thread(s, timeout=TIMEOUT)

        self.assertEqual(c.contents, [])
        # the server might have been able to send a byte or two back, but this
        # at least checks that it received something and didn't just fail
        # (which could still result in the client not having received anything)
        self.assertGreater(len(s.buffer), 0)
    def test_close_when_done(self):
        s, event = start_echo_server()
        s.start_resend_event = threading.Event()
        c = echo_client(b'\n', s.port)
        c.push(b"hello world\nI'm not dead yet!\n")
        c.push(SERVER_QUIT)
        c.close_when_done()
        asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)

        # Only allow the server to start echoing data back to the client after
        # the client has closed its connection.  This prevents a race condition
        # where the server echoes all of its data before we can check that it
        # got any down below.
        s.start_resend_event.set()
        support.join_thread(s)

        self.assertEqual(c.contents, [])
        # the server might have been able to send a byte or two back, but this
        # at least checks that it received something and didn't just fail
        # (which could still result in the client not having received anything)
        self.assertGreater(len(s.buffer), 0)
Exemplo n.º 27
0
    def test_quick_connect(self):
        # see: http://bugs.python.org/issue10340
        if self.family not in (socket.AF_INET, getattr(socket, "AF_INET6", object())):
            self.skipTest("test specific to AF_INET and AF_INET6")

        server = BaseServer(self.family, self.addr)
        # run the thread 500 ms: the socket should be connected in 200 ms
        t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1,
                                                          count=5))
        t.start()
        try:
            with socket.socket(self.family, socket.SOCK_STREAM) as s:
                s.settimeout(.2)
                s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
                             struct.pack('ii', 1, 0))

                try:
                    s.connect(server.address)
                except OSError:
                    pass
        finally:
            support.join_thread(t, timeout=TIMEOUT)
Exemplo n.º 28
0
    def test_quick_connect(self):
        # see: http://bugs.python.org/issue10340
        if self.family not in (socket.AF_INET, getattr(socket, "AF_INET6", object())):
            self.skipTest("test specific to AF_INET and AF_INET6")

        server = BaseServer(self.family, self.addr)
        # run the thread 500 ms: the socket should be connected in 200 ms
        t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1,
                                                          count=5))
        t.start()
        try:
            with socket.socket(self.family, socket.SOCK_STREAM) as s:
                s.settimeout(.2)
                s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
                             struct.pack('ii', 1, 0))

                try:
                    s.connect(server.address)
                except OSError:
                    pass
        finally:
            support.join_thread(t, timeout=TIMEOUT)
Exemplo n.º 29
0
    def test_send(self):
        evt = threading.Event()
        sock = socket.socket()
        sock.settimeout(3)
        port = support.bind_port(sock)

        cap = BytesIO()
        args = (evt, cap, sock)
        t = threading.Thread(target=capture_server, args=args)
        t.start()
        try:
            # wait a little longer for the server to initialize (it sometimes
            # refuses connections on slow machines without this wait)
            time.sleep(0.2)

            data = b"Suppose there isn't a 16-ton weight?"
            d = dispatcherwithsend_noread()
            d.create_socket()
            d.connect((support.HOST, port))

            # give time for socket to connect
            time.sleep(0.1)

            d.send(data)
            d.send(data)
            d.send(b'\n')

            n = 1000
            while d.out_buffer and n > 0:
                asyncore.poll()
                n -= 1

            evt.wait()

            self.assertEqual(cap.getvalue(), data*2)
        finally:
            support.join_thread(t, timeout=TIMEOUT)
Exemplo n.º 30
0
    def test_send(self):
        evt = threading.Event()
        sock = socket.socket()
        sock.settimeout(3)
        port = support.bind_port(sock)

        cap = BytesIO()
        args = (evt, cap, sock)
        t = threading.Thread(target=capture_server, args=args)
        t.start()
        try:
            # wait a little longer for the server to initialize (it sometimes
            # refuses connections on slow machines without this wait)
            time.sleep(0.2)

            data = b"Suppose there isn't a 16-ton weight?"
            d = dispatcherwithsend_noread()
            d.create_socket()
            d.connect((support.HOST, port))

            # give time for socket to connect
            time.sleep(0.1)

            d.send(data)
            d.send(data)
            d.send(b'\n')

            n = 1000
            while d.out_buffer and n > 0:
                asyncore.poll()
                n -= 1

            evt.wait()

            self.assertEqual(cap.getvalue(), data * 2)
        finally:
            support.join_thread(t, timeout=TIMEOUT)