コード例 #1
0
def test_example_repeating_test_can_be_stopped_and_run_again(make_thread):
    r = make_thread(ExampleRepeatingThread, "R")

    q = Queue()

    # Set loop timer to be faster than usual
    r.loop_timer = timedelta(milliseconds=200)

    send_subscription_message(r, q)

    r.start()
    time.sleep(0.3)

    # r runs at time t = 0, 0.2

    r.stop()
    min_sleep()
    assert q.qsize() == 2

    r.start()
    time.sleep(0.5)

    # r runs at approx t = 0.3, 0.5, 0.7

    r.end()
    time.sleep(0.1)

    assert threading.active_count() == 2
    assert q.qsize() == 5
    q_results = [q.get().package for i in range(5)]
    assert q_results == [1, 2, 3, 4, 5]
コード例 #2
0
def test_single_run_thread_ends_straight_away():
    assert threading.active_count() == 2

    e = ExampleSingleRunThread("E")
    assert threading.active_count() == 3

    e.end()
    min_sleep()

    assert threading.active_count() == 2
コード例 #3
0
def test_exception_can_be_caught(make_thread):
    mock_supervisor = Mock()

    e = make_thread(ExampleExceptionThread, "E", supervisor=mock_supervisor)

    e.start()
    min_sleep()

    mock_supervisor.raise_exception.assert_called_once()
    # Check that a reference to the thread is returned as the last argument
    assert e is mock_supervisor.raise_exception.call_args[0][0][-1]
コード例 #4
0
def test_single_run_thread_finishes_straight_away():
    assert threading.active_count() == 2

    e = ExampleSingleRunThread("E")
    e.start()

    assert threading.active_count() == 3

    min_sleep()

    assert threading.active_count() == 2
コード例 #5
0
def test_single_run_thread_puts_message_in_queue(make_thread):
    e = make_thread(ExampleSingleRunThread, "E")

    q = Queue()

    send_subscription_message(e, q)
    e.start()

    min_sleep()

    assert threading.active_count() == 2
    assert q.qsize() == 1
    assert q.get() == Message("E", Commands.THREAD_HANDLE, None)
コード例 #6
0
def make_thread():

    created_threads = []

    def _make_thread(thread_class, *args, **kwargs):
        thread = thread_class(*args, **kwargs)
        created_threads.append(thread)
        return thread

    yield _make_thread

    for t in created_threads:
        while t.is_alive():
            t.end()
            min_sleep()
コード例 #7
0
def test_multi_run_thread_runs_then_ends():
    assert threading.active_count() == 2

    m = ExampleMultiRunThread("M")
    assert threading.active_count() == 3

    m.start()
    m.start()
    m.start()
    m.start()
    min_sleep()

    assert m.times_ran == 4

    m.end()
    min_sleep()

    assert threading.active_count() == 2
コード例 #8
0
def test_multi_run_thread_puts_message_in_queue(make_thread):
    m = make_thread(ExampleMultiRunThread, "M")

    q = Queue()

    send_subscription_message(m, q)
    m.start()
    m.start()
    m.start()
    m.start()
    m.end()

    min_sleep()

    assert threading.active_count() == 2
    assert q.qsize() == 4
    assert q.get() == Message("M", Commands.THREAD_HANDLE, 1)
    assert q.get() == Message("M", Commands.THREAD_HANDLE, 2)
    assert q.get() == Message("M", Commands.THREAD_HANDLE, 3)
    assert q.get() == Message("M", Commands.THREAD_HANDLE, 4)
コード例 #9
0
def test_example_repeating_test_runs_a_known_number_of_times(make_thread):
    r = make_thread(ExampleRepeatingThread, "R")

    q = Queue()

    # Set loop timer to be faster than usual
    r.loop_timer = timedelta(milliseconds=200)

    send_subscription_message(r, q)

    r.start()
    time.sleep(0.7)

    r.end()
    min_sleep()

    assert threading.active_count() == 2
    assert q.qsize() == 4
    q_results = [q.get().package for i in range(4)]
    assert q_results == [1, 2, 3, 4]
コード例 #10
0
def test_alternating_endpoints_working_as_expected(make_thread):
    a = make_thread(ExampleAlternatingThread, "A")

    q_a = Queue()
    q_b = Queue()

    send_subscription_message(a, q_a, a.Endpoints.RESULT)
    send_subscription_message(a, q_b, a.Endpoints.NOT_RESULT)

    a.start()
    a.start()
    a.end()

    min_sleep()

    assert threading.active_count() == 2
    assert q_a.qsize() == 2
    assert q_b.qsize() == 2
    assert q_a.get().package is True
    assert q_a.get().package is False
    assert q_b.get().package is False
    assert q_b.get().package is True
コード例 #11
0
def test_multi_run_thread_puts_message_in_all_queues(make_thread):
    m = make_thread(ExampleMultiRunThread, "M")

    q_a = Queue()
    q_b = Queue()

    send_subscription_message(m, q_a)
    send_subscription_message(m, q_b)
    m.start()
    m.start()
    m.start()
    m.start()
    m.end()

    min_sleep()

    assert threading.active_count() == 2
    assert q_a.qsize() == 4
    assert q_b.qsize() == 4
    q_a_results = [q_a.get().package for i in range(4)]
    q_b_results = [q_b.get().package for i in range(4)]
    assert q_a_results == [1, 2, 3, 4]
    assert q_b_results == [1, 2, 3, 4]