示例#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 support.verbose:
            print()
            print("*** Testing multiple thread creation "\
            "(will take approx. %s to %s sec.) ***" % (DELAY, thread_count))
        for count in range(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 support.verbose:
            print('done')
        self.assertTrue(testing_queue.qsize() == thread_count,
                        "Not all %s threads executed properly after %s sec." %
                        (thread_count, DELAY))
示例#2
0
    def test_multi_thread_creation(self):
        def queue_mark(queue, delay):
            time.sleep(delay)
            queue.put(_thread.get_ident())

        thread_count = 5
        testing_queue = queue.Queue(thread_count)

        if support.verbose:
            print()
            print("*** Testing multiple thread creation "
                  "(will take approx. %s to %s sec.) ***" % (
                    DELAY, thread_count))

        for count in range(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 support.verbose:
            print('done')
        self.assertEqual(testing_queue.qsize(), thread_count,
                         "Not all %s threads executed properly "
                         "after %s sec." % (thread_count, DELAY))
    def test_multi_thread_creation(self):
        def queue_mark(queue, delay):
            time.sleep(delay)
            queue.put(_thread.get_ident())

        thread_count = 5
        testing_queue = queue.Queue(thread_count)

        if support.verbose:
            print()
            print("*** Testing multiple thread creation "
                  "(will take approx. %s to %s sec.) ***" %
                  (DELAY, thread_count))

        for count in range(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 support.verbose:
            print('done')
        self.assertEqual(
            testing_queue.qsize(), thread_count,
            "Not all %s threads executed properly "
            "after %s sec." % (thread_count, DELAY))
    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 support.verbose:
            print()
            print("*** Testing multiple thread creation "\
            "(will take approx. %s to %s sec.) ***" % (DELAY, thread_count))
        for count in range(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 support.verbose:
            print('done')
        self.assertTrue(
            testing_queue.qsize() == thread_count,
            "Not all %s threads executed properly after %s sec." %
            (thread_count, DELAY))
 def test_args_not_tuple(self):
     """
     Test invoking start_new_thread() with a non-tuple value for "args".
     Expect TypeError with a meaningful error message to be raised.
     """
     with self.assertRaises(TypeError) as cm:
         _thread.start_new_thread(mock.Mock(), [])
     self.assertEqual(cm.exception.args[0], "2nd arg must be a tuple")
 def test_kwargs_not_dict(self):
     """
     Test invoking start_new_thread() with a non-dict value for "kwargs".
     Expect TypeError with a meaningful error message to be raised.
     """
     with self.assertRaises(TypeError) as cm:
         _thread.start_new_thread(mock.Mock(), tuple(), kwargs=[])
     self.assertEqual(cm.exception.args[0], "3rd arg must be a dict")
示例#7
0
 def test_kwargs_not_dict(self):
     """
     Test invoking start_new_thread() with a non-dict value for "kwargs".
     Expect TypeError with a meaningful error message to be raised.
     """
     with self.assertRaises(TypeError) as cm:
         _thread.start_new_thread(mock.Mock(), tuple(), kwargs=[])
     self.assertEqual(cm.exception.args[0], "3rd arg must be a dict")
示例#8
0
 def test_args_not_tuple(self):
     """
     Test invoking start_new_thread() with a non-tuple value for "args".
     Expect TypeError with a meaningful error message to be raised.
     """
     with self.assertRaises(TypeError) as cm:
         _thread.start_new_thread(mock.Mock(), [])
     self.assertEqual(cm.exception.args[0], "2nd arg must be a tuple")
示例#9
0
    def test_RaiseException(self, mock_print_exc):
        """
        Test invoking start_new_thread() with a function that raises exception.

        The exception should be discarded and the traceback should be printed
        via traceback.print_exc()
        """
        func = mock.Mock(side_effect=Exception)
        _thread.start_new_thread(func, tuple())
        self.assertTrue(mock_print_exc.called)
    def test_RaiseException(self, mock_print_exc):
        """
        Test invoking start_new_thread() with a function that raises exception.

        The exception should be discarded and the traceback should be printed
        via traceback.print_exc()
        """
        func = mock.Mock(side_effect=Exception)
        _thread.start_new_thread(func, tuple())
        self.assertTrue(mock_print_exc.called)
示例#11
0
 def test_SystemExit(self):
     """
     Test invoking start_new_thread() with a function that raises
     SystemExit.
     The exception should be discarded.
     """
     func = mock.Mock(side_effect=SystemExit())
     try:
         _thread.start_new_thread(func, tuple())
     except SystemExit:
         self.fail("start_new_thread raised SystemExit.")
 def test_SystemExit(self):
     """
     Test invoking start_new_thread() with a function that raises
     SystemExit.
     The exception should be discarded.
     """
     func = mock.Mock(side_effect=SystemExit())
     try:
         _thread.start_new_thread(func, tuple())
     except SystemExit:
         self.fail("start_new_thread raised SystemExit.")
示例#13
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 support.verbose:
            print()
            print("*** Waiting for thread to release the lock " "(approx. %s sec.) ***" % DELAY)
        self.lock.acquire()
        end_time = int(time.time())
        if support.verbose:
            print("done")
        self.assertTrue((end_time - start_time) >= DELAY, "Blocking by unconditional acquiring failed.")
    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 support.verbose:
            print()
            print("*** Waiting for thread to release the lock "\
            "(approx. %s sec.) ***" % DELAY)
        self.lock.acquire()
        end_time = int(time.time())
        if support.verbose:
            print("done")
        self.assertGreaterEqual(end_time - start_time, DELAY,
                                "Blocking by unconditional acquiring failed.")
示例#15
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")
    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")
示例#17
0
functionStatus = False


def change_function():
    if replyToGroupChat != functionStatus:
        if replyToGroupChat:

            @itchat.msg_register(TEXT, isGroupChat=True)
            def group_text_reply(msg):
                if u'关闭' in msg['Text']:
                    replyToGroupChat = False
                    return u'已关闭'
                elif u'开启' in msg['Text']:
                    return u'已经在运行'
                return u'输入"关闭"或者"开启"测试功能'
        else:

            @itchat.msg_register(TEXT, isGroupChat=True)
            def group_text_reply(msg):
                if u'开启' in msg['Text']:
                    replyToGroupChat = True
                    return u'重新开启成功'

        functionStatus = replyToGroupChat


_dummy_thread.start_new_thread(itchat.run, ())

while 1:
    change_function()
    time.sleep(.1)
示例#18
0
文件: nodes.py 项目: xxoolm/Ryven
 def update_event(self, inp=-1):
     self.set_output_val(
         0,
         _dummy_thread.start_new_thread(self.input(0), self.input(1),
                                        self.input(2)))
示例#19
0
                        en_actions = [(Action.FASTER, None)
                                      for _ in new_world.enemy_ships]
                        my_actions = [(Action.DROITE, None)
                                      for _ in new_world.my_ships]

                        new_world.prepare()
                        new_world.set_actions(0, en_actions)
                        new_world.set_actions(1, my_actions)

                        new_world.update()

                        worlds.append(new_world)

                        histo_pos = len(worlds) - 1

                if event.key == K_r:
                    worlds.clear()
                    worlds.append(get_random_world())
                    histo_pos = 0

        screen.fill(WHITE)
        draw_grid(screen)
        draw_world(screen, worlds[histo_pos])
        pygame.display.flip()


if __name__ == '__main__':
    main()
else:
    start_new_thread(main, tuple())