コード例 #1
0
    def test_multi_creation(self):
        # Make sure multiple threads can be created.
        def queue_mark(queue, delay):
            """Wait for ``delay`` seconds and then put something into ``queue``"""
            time.sleep(delay)
            queue.put(_thread.get_ident())

        thread_count = 5
        testing_queue = Queue.Queue(thread_count)
        if test_support.verbose:
            print
            print "*** Testing multiple thread creation " "(will take approx. %s to %s sec.) ***" % (
                DELAY,
                thread_count,
            )
        for count in xrange(thread_count):
            if DELAY:
                local_delay = round(random.random(), 1)
            else:
                local_delay = 0
            _thread.start_new_thread(queue_mark, (testing_queue, local_delay))
        time.sleep(DELAY)
        if test_support.verbose:
            print "done"
        self.failUnless(
            testing_queue.qsize() == thread_count,
            "Not all %s threads executed properly after %s sec." % (thread_count, DELAY),
        )
コード例 #2
0
    def test_multi_creation(self):
        #Make sure multiple threads can be created.
        def queue_mark(queue, delay):
            """Wait for ``delay`` seconds and then put something into ``queue``"""
            time.sleep(delay)
            queue.put(_thread.get_ident())

        thread_count = 5
        testing_queue = Queue.Queue(thread_count)
        if test_support.verbose:
            print
            print "*** Testing multiple thread creation "\
            "(will take approx. %s to %s sec.) ***" % (DELAY, thread_count)
        for count in xrange(thread_count):
            if DELAY:
                local_delay = round(random.random(), 1)
            else:
                local_delay = 0
            _thread.start_new_thread(queue_mark, (testing_queue, local_delay))
        time.sleep(DELAY)
        if test_support.verbose:
            print 'done'
        self.assertTrue(
            testing_queue.qsize() == thread_count,
            "Not all %s threads executed properly after %s sec." %
            (thread_count, DELAY))
コード例 #3
0
    def test_uncond_acquire_blocking(self):
        # Make sure that unconditional acquiring of a locked lock blocks.
        def delay_unlock(to_unlock, delay):
            """Hold on to lock for a set amount of time before unlocking."""
            time.sleep(delay)
            to_unlock.release()

        self.lock.acquire()
        start_time = int(time.time())
        _thread.start_new_thread(delay_unlock, (self.lock, DELAY))
        if test_support.verbose:
            print
            print "*** Waiting for thread to release the lock " "(approx. %s sec.) ***" % DELAY
        self.lock.acquire()
        end_time = int(time.time())
        if test_support.verbose:
            print "done"
        self.failUnless((end_time - start_time) >= DELAY, "Blocking by unconditional acquiring failed.")
コード例 #4
0
    def test_uncond_acquire_blocking(self):
        #Make sure that unconditional acquiring of a locked lock blocks.
        def delay_unlock(to_unlock, delay):
            """Hold on to lock for a set amount of time before unlocking."""
            time.sleep(delay)
            to_unlock.release()

        self.lock.acquire()
        start_time = int(time.time())
        _thread.start_new_thread(delay_unlock, (self.lock, DELAY))
        if test_support.verbose:
            print
            print "*** Waiting for thread to release the lock "\
            "(approx. %s sec.) ***" % DELAY
        self.lock.acquire()
        end_time = int(time.time())
        if test_support.verbose:
            print "done"
        self.assertTrue((end_time - start_time) >= DELAY,
                        "Blocking by unconditional acquiring failed.")
コード例 #5
0
    def test_arg_passing(self):
        #Make sure that parameter passing works.
        def arg_tester(queue, arg1=False, arg2=False):
            """Use to test _thread.start_new_thread() passes args properly."""
            queue.put((arg1, arg2))

        testing_queue = Queue.Queue(1)
        _thread.start_new_thread(arg_tester, (testing_queue, True, True))
        result = testing_queue.get()
        self.assertTrue(
            result[0] and result[1],
            "Argument passing for thread creation using tuple failed")
        _thread.start_new_thread(arg_tester, tuple(), {
            'queue': testing_queue,
            'arg1': True,
            'arg2': True
        })
        result = testing_queue.get()
        self.assertTrue(
            result[0] and result[1],
            "Argument passing for thread creation using kwargs failed")
        _thread.start_new_thread(arg_tester, (testing_queue, True),
                                 {'arg2': True})
        result = testing_queue.get()
        self.assertTrue(
            result[0] and result[1],
            "Argument passing for thread creation using both tuple"
            " and kwargs failed")
コード例 #6
0
    def test_arg_passing(self):
        # Make sure that parameter passing works.
        def arg_tester(queue, arg1=False, arg2=False):
            """Use to test _thread.start_new_thread() passes args properly."""
            queue.put((arg1, arg2))

        testing_queue = Queue.Queue(1)
        _thread.start_new_thread(arg_tester, (testing_queue, True, True))
        result = testing_queue.get()
        self.failUnless(result[0] and result[1], "Argument passing for thread creation using tuple failed")
        _thread.start_new_thread(arg_tester, tuple(), {"queue": testing_queue, "arg1": True, "arg2": True})
        result = testing_queue.get()
        self.failUnless(result[0] and result[1], "Argument passing for thread creation using kwargs failed")
        _thread.start_new_thread(arg_tester, (testing_queue, True), {"arg2": True})
        result = testing_queue.get()
        self.failUnless(
            result[0] and result[1], "Argument passing for thread creation using both tuple" " and kwargs failed"
        )