예제 #1
0
def test_script_execution_service_worker_verify_list_method_called(caplog):
    """
    SES.summarise should be called when 'request.procedure.list' message is received
    """
    helper = PubSubHelper()

    work_q = MPQueue()
    msg = EventMessage(
        "TEST_SUMMARY",
        "PUBSUB",
        dict(topic=topics.request.procedure.list, kwargs={"request_id": "123"}),
    )
    work_q.put(msg)
    event = mp.Event()

    with mock.patch(
        "oet.procedure.application.main.ScriptExecutionService.summarise"
    ) as mock_cls:
        mock_cls.side_effect = partial(set_event, event)
        _proc_worker_wrapper_helper(
            caplog,
            ScriptExecutionServiceWorker,
            args=(work_q,),
            expect_shutdown_evt=True,
        )

    assert event.is_set() is True
    mock_cls.assert_called_once()

    assert helper.topic_list == [
        topics.request.procedure.list,  # list requested
        topics.procedure.pool.list,  # response published
    ]
예제 #2
0
def test_script_execution_service_worker_handles_request_to_list_invalid_id(caplog):
    """
    The ValueError raised when SES.summarise is given an invalid PID should be handled.
    """
    helper = PubSubHelper()

    work_q = MPQueue()
    msg = EventMessage(
        "TEST_SUMMARY",
        "PUBSUB",
        dict(topic=topics.request.procedure.list, kwargs={"request_id": "123"}),
    )
    work_q.put(msg)

    with mock.patch(
        "oet.procedure.application.main.ScriptExecutionService.summarise"
    ) as mock_cls:
        mock_cls.side_effect = ValueError
        _proc_worker_wrapper_helper(
            caplog,
            ScriptExecutionServiceWorker,
            args=(work_q,),
            expect_shutdown_evt=True,
        )

    mock_cls.assert_called_once()

    assert helper.topic_list == [
        topics.request.procedure.list,  # list requested
        topics.procedure.pool.list,  # response published
    ]
    assert helper.messages[1][1] == dict(msg_src="TEST", request_id="123", result=[])
예제 #3
0
def test_event_bus_worker_does_not_publish_messages_from_self(caplog):
    """
    Verify that message event is not published if the event originates from
    an internal source.
    """
    helper = PubSubHelper()

    work_q = MPQueue()
    # TEST is the default component name assigned in
    # _proc_worker_wrapper_helper. This message should be ignored.
    msg = EventMessage(
        "TEST",
        "PUBSUB",
        dict(topic=topics.request.procedure.list, kwargs={"request_id": "123"}),
    )

    work_q.put(msg)
    # But coming from NONTEST, this message should be republished.
    msg = EventMessage(
        "NONTEST",
        "PUBSUB",
        dict(topic=topics.request.procedure.list, kwargs={"request_id": "456"}),
    )
    work_q.put(msg)

    _proc_worker_wrapper_helper(
        caplog, EventBusWorker, args=(work_q,), expect_shutdown_evt=True
    )

    assert len(helper.messages) == 1
    assert helper.messages[0][1] == dict(msg_src="NONTEST", request_id="456")
예제 #4
0
def test_drain_queue():
    Q = MPQueue()

    items = list(Q.drain())
    assert items == []

    expected = [f"ITEM{idx}" for idx in range(10)]
    for item in expected:
        Q.put(item)

    items = list(Q.drain())
    assert items == expected

    num_left = Q.safe_close()
    assert num_left == 0
예제 #5
0
def test_main_loop_ends_on_fatal_message():
    """
    Main loop should terminate when fatal messsage is received.
    """
    mock_ctx = mock.MagicMock()

    event_q = MPQueue()
    event_q.put(EventMessage("TEST", "FATAL", msg="foo"))
    event_q.put(EventMessage("TEST", "END", msg="foo"))
    mock_ctx.event_queue = event_q

    mock_ctx.shutdown_event.is_set.return_value = False

    main_loop(mock_ctx, [])

    assert event_q.safe_close() == 1
예제 #6
0
def test_mpqueue_get():
    Q = MPQueue()

    item = Q.safe_get(None)
    assert item is None

    Q.put("ITEM1")
    Q.put("ITEM2")

    assert Q.safe_get(0.02) == "ITEM1"
    assert Q.safe_get(0.02) == "ITEM2"
    assert Q.safe_get(0.02) is None
    assert Q.safe_get(None) is None

    num_left = Q.safe_close()
    assert num_left == 0
예제 #7
0
def test_main_loop_ignores_and_logs_events_of_unknown_types():
    """
    Loop should log events it doesn't know how to handle.
    """
    mock_ctx = mock.MagicMock()

    event_q = MPQueue()
    event_q.put(EventMessage("TEST", "FOO", msg="1"))
    mock_ctx.event_queue = event_q

    # one processing loop before shutdown in set, at which point the loop
    # should exit with three messages still in the event queue
    mock_ctx.shutdown_event.is_set.side_effect = [False, True]

    main_loop(mock_ctx, [])

    event_q.safe_close()
    mock_ctx.log.assert_called_once()
    assert "Unknown Event" in mock_ctx.log.call_args[0][1]
예제 #8
0
def test_event_bus_worker_verify_message_publishes_when_message_in_work_queue(caplog):
    """
    Verify that message event is published if the event originates from an
    external source.
    """
    helper = PubSubHelper()

    work_q = MPQueue()
    msg = EventMessage(
        "EXTERNAL COMPONENT",
        "PUBSUB",
        dict(topic=topics.request.procedure.list, kwargs={"request_id": "123"}),
    )
    work_q.put(msg)
    _proc_worker_wrapper_helper(
        caplog, EventBusWorker, args=(work_q,), expect_shutdown_evt=True
    )

    assert topics.request.procedure.list in helper.topic_list
예제 #9
0
def test_main_loop_adds_pubsub_messages_to_event_queues():
    """
    PUBSUB messages should be added to event queues.
    """
    mock_ctx = mock.MagicMock()

    event_q = MPQueue()
    event_q.put(EventMessage("TEST", "PUBSUB", msg="1"))
    event_q.put(EventMessage("TEST", "PUBSUB", msg="2"))
    event_q.put(EventMessage("TEST", "PUBSUB", msg="3"))
    event_q.put(EventMessage("TEST", "END", msg="foo"))
    mock_ctx.event_queue = event_q

    # one processing loop before shutdown in set, at which point the loop
    # should exit with three messages still in the event queue
    mock_ctx.shutdown_event.is_set.return_value = False

    q1 = MPQueue()
    q2 = MPQueue()

    main_loop(mock_ctx, [q1, q2])

    assert q1.safe_close() == 3
    assert q2.safe_close() == 3

    event_q.safe_close()
예제 #10
0
def test_queue_proc_worker(caplog):
    work_q = MPQueue()
    work_q.put(1)
    work_q.put(2)
    work_q.put(3)
    work_q.put(4)
    work_q.put("END")
    work_q.put(5)

    items = _proc_worker_wrapper_helper(caplog,
                                        QueueProcWorkerTest,
                                        args=(work_q, ),
                                        expect_shutdown_evt=False)
    assert len(items) == 4
    assert items == [f"DONE {idx + 1}" for idx in range(4)]
예제 #11
0
def assert_command_request_and_response(
    caplog, mock_method, request_topic, response_topic, cmd
):
    helper = PubSubHelper()

    work_q = MPQueue()
    msg = EventMessage(
        "UNITTEST",
        "PUBSUB",
        dict(topic=request_topic, kwargs={"request_id": "1234", "cmd": cmd}),
    )
    work_q.put(msg)
    event = mp.Event()

    mock_method.side_effect = partial(set_event, event)
    _proc_worker_wrapper_helper(
        caplog, ScriptExecutionServiceWorker, args=(work_q,), expect_shutdown_evt=True
    )

    assert event.is_set()
    mock_method.assert_called_once()
    assert mock_method.call_args[0][0] == cmd

    assert helper.topic_list == [request_topic, response_topic]
예제 #12
0
def test_main_loop_ends_when_shutdown_event_is_set():
    """
    Main loop should terminate when shutdown event is set.
    """
    mock_ctx = mock.MagicMock()

    event_q = MPQueue()
    event_q.put(EventMessage("TEST", "PUBSUB", msg="foo"))
    event_q.put(EventMessage("TEST", "PUBSUB", msg="foo"))
    event_q.put(EventMessage("TEST", "PUBSUB", msg="foo"))
    event_q.put(EventMessage("TEST", "END", msg="foo"))
    mock_ctx.event_queue = event_q

    # one processing loop before shutdown in set, at which point the loop
    # should exit with two messages still in the event queue
    mock_ctx.shutdown_event.is_set.side_effect = [False, False, True]

    main_loop(mock_ctx, [])

    assert event_q.safe_close() == 2