def test_derived(self):
        # Issue 3088: if there is a threads switch inside the __init__
        # of a threading.local derived class, the per-thread dictionary
        # is created but not correctly set on the object.
        # The first member set may be bogus.
        from eventlib.green import threading
        from eventlib.green import time

        class Local(threading.local):
            def __init__(self):
                time.sleep(0.01)

        local = Local()

        def f(i):
            local.x = i
            # Simply check that the variable is correctly set
            self.assertEqual(local.x, i)

        threads = []
        for i in range(10):
            t = threading.Thread(target=f, args=(i, ))
            t.start()
            threads.append(t)

        for t in threads:
            t.join()
Exemple #2
0
 def process_request(self, request, client_address):
     """Start a new thread to process the request."""
     from eventlib.green import threading
     t = threading.Thread(target=self.process_request_thread,
                          args=(request, client_address))
     if self.daemon_threads:
         t.setDaemon(1)
     t.start()
 def test_enumerate_after_join(self):
     # Try hard to trigger #1703448: a thread is still returned in
     # threading.enumerate() after it has been join()ed.
     enum = threading.enumerate
     old_interval = sys.getcheckinterval()
     sys.setcheckinterval(1)
     try:
         for i in xrange(1, 1000):
             t = threading.Thread(target=lambda: None)
             t.start()
             t.join()
             l = enum()
             self.assertFalse(
                 t in l, "#1703448 triggered after %d trials: %s" % (i, l))
     finally:
         sys.setcheckinterval(old_interval)
Exemple #4
0
def test_rude_shutdown__write():
    if test_support.verbose:
        print("test_rude_shutdown__variant ...")

    from eventlib.green import threading

    # Some random port to connect to.
    PORT = [9934]

    listener_ready = threading.Event()
    listener_gone = threading.Event()

    # `listener` runs in a thread.  It opens a socket listening on PORT, and
    # sits in an accept() until the main thread connects.  Then it rudely
    # closes the socket, and sets Event `listener_gone` to let the main thread
    # know the socket is gone.
    def listener():
        s = socket.socket()
        PORT[0] = test_support.bind_port(s, '', PORT[0])
        s.listen(5)
        listener_ready.set()
        s.accept()
        s = None  # reclaim the socket object, which also closes it
        listener_gone.set()

    def connector():
        listener_ready.wait()
        s = socket.socket()
        s.connect(('localhost', PORT[0]))
        listener_gone.wait()
        try:
            ssl_sock = socket.ssl(s)
            ssl_sock.write("hello")
        except socket.sslerror:
            pass
        else:
            raise test_support.TestFailed(
                'connecting to closed SSL socket should have failed')

    t = threading.Thread(target=listener)
    t.start()
    connector()
    t.join()