Esempio n. 1
0
def test_group_are_canceled_on_actor_failure(stub_broker, stub_worker, cancel_backend):
    # Given a cancel backend
    # And a broker with the cancel middleware
    stub_broker.add_middleware(Cancel(backend=cancel_backend))

    has_been_called = []

    # And an actor
    @remoulade.actor()
    def do_work():
        has_been_called.append(1)
        raise ValueError()

    # And this actor is declared
    stub_broker.declare_actor(do_work)

    g = group((do_work.message() for _ in range(4)), cancel_on_error=True)

    # When I group a few jobs together and run it
    g.run()

    stub_broker.join(do_work.queue_name)
    stub_worker.join()

    # All actors should have been canceled
    assert all(cancel_backend.is_canceled(child.message_id, g.group_id) for child in g.children)
Esempio n. 2
0
def test_cancel_pipeline_or_groups(stub_broker, stub_worker, cancel_backend, with_pipeline):
    # Given a cancel backend
    # And a broker with the cancel middleware
    stub_broker.add_middleware(Cancel(backend=cancel_backend))

    has_been_called = []

    # And an actor
    @remoulade.actor()
    def do_work():
        has_been_called.append(1)
        raise ValueError()

    # And this actor is declared
    stub_broker.declare_actor(do_work)

    if with_pipeline:
        g = pipeline((do_work.message() for _ in range(4)))
    else:
        g = group((do_work.message() for _ in range(4)))

    g.cancel()
    g.run()

    stub_broker.join(do_work.queue_name)
    stub_worker.join()

    # All actors should have been canceled
    assert all(cancel_backend.is_canceled(child.message_id, None) for child in g.children)
    assert len(has_been_called) == 0
Esempio n. 3
0
def test_compositions_are_canceled_on_actor_failure(stub_broker, stub_worker, cancel_backend):
    # Given a cancel backend
    # And a broker with the cancel middleware
    stub_broker.add_middleware(Cancel(backend=cancel_backend))

    # And an actor who doesn't fail
    @remoulade.actor
    def do_work(arg=None):
        return 1

    # And an actor who fails
    @remoulade.actor
    def do_fail(arg):
        raise Exception

    # And those actors are declared
    remoulade.declare_actors([do_work, do_fail])

    g = group([do_work.message(), do_fail.message() | do_work.message()], cancel_on_error=True)

    # When I group a few jobs together and run it
    g.run()

    stub_broker.join(do_fail.queue_name)
    stub_worker.join()

    # All actors should have been canceled
    assert cancel_backend.is_canceled("", g.group_id)
Esempio n. 4
0
def test_actors_can_be_canceled(stub_broker, stub_worker, cancel_backend, cancel):
    # Given a cancel backend
    # And a broker with the cancel middleware
    stub_broker.add_middleware(Cancel(backend=cancel_backend))

    has_been_called = []

    # And an actor
    @remoulade.actor()
    def do_work():
        has_been_called.append(1)

    # And this actor is declared
    stub_broker.declare_actor(do_work)

    # When I send that actor a message
    message = do_work.message()

    # If I cancel the message
    if cancel:
        message.cancel()

    stub_broker.enqueue(message)

    stub_broker.join(do_work.queue_name)
    stub_worker.join()

    # It should not be called
    assert bool(has_been_called) != cancel
Esempio n. 5
0
def test_composition_can_be_canceled(stub_broker, stub_worker, cancel_backend):
    # Given a cancel backend
    # And a broker with the cancel middleware
    stub_broker.add_middleware(Cancel(backend=cancel_backend))

    calls_count = 0

    # And an actor
    @remoulade.actor()
    def do_work():
        nonlocal calls_count
        calls_count += 1
        raise ValueError()

    # And this actor is declared
    stub_broker.declare_actor(do_work)

    # And a composition
    g = group([do_work.message() | do_work.message() for _ in range(2)])

    # If the composition is canceled
    cancel_backend.cancel([g.group_id])

    g.run()

    stub_broker.join(do_work.queue_name)
    stub_worker.join()

    # It messages should not have runMessageSchema
    assert calls_count == 0
Esempio n. 6
0
def test_raise_error_if_unknown_id(stub_broker, cancel_backend, api_client):
    # Given a cancel middleware
    stub_broker.add_middleware(Cancel(backend=cancel_backend))

    # If I try to cancel a id that is not a id of a message or composition
    res = api_client.post("messages/cancel/invalid_id")

    assert res.status_code == 400
Esempio n. 7
0
 def test_cancel_state_message(self, stub_broker, stub_worker, cancel_backend, state_middleware, do_work):
     stub_broker.add_middleware(Cancel(backend=cancel_backend))
     #  Pause the worker to be able to cancel the  message, and
     #   this does not been processed  when it enters the queue
     stub_worker.pause()
     msg = do_work.send()
     msg.cancel()
     stub_worker.resume()
     stub_broker.join(do_work.queue_name)
     stub_worker.join()
     state = state_middleware.backend.get_state(msg.message_id)
     assert state.status == StateStatusesEnum.Canceled
     # should not finish, since is cancelled
     assert state.end_datetime is None
Esempio n. 8
0
def test_local_broker_cancel(local_broker, stub_cancel_backend):

    local_broker.add_middleware(Cancel(backend=stub_cancel_backend))

    @remoulade.actor()
    def do_work():
        return 1

    # And this actor is declared
    local_broker.declare_actor(do_work)

    # When I send that actor a message
    message = do_work.send()

    # I can call cancel
    message.cancel()
Esempio n. 9
0
 def test_cancel_state_message(
     self,
     stub_broker,
     stub_worker,
     cancel_backend,
     state_middleware,
     do_work,
 ):
     stub_broker.add_middleware(Cancel(backend=cancel_backend))
     #  Pause the worker to be able to cancel the  message, and
     #   this does not been processed  when it enters the queue
     stub_worker.pause()
     msg = do_work.send()
     msg.cancel()
     stub_worker.resume()
     stub_broker.join(do_work.queue_name)
     stub_worker.join()
     assert state_middleware.backend.get_state(
         msg.message_id).name == StateNamesEnum.Canceled
Esempio n. 10
0
def test_compositions_are_canceled_on_message_cancel(stub_broker, cancel_backend, state_middleware, api_client):
    # Given a cancel backend
    # And a broker with the cancel middleware
    stub_broker.add_middleware(Cancel(backend=cancel_backend))

    # And an actor
    @remoulade.actor
    def do_work(arg=None):
        return 1

    # And those actors are declared
    stub_broker.declare_actor(do_work)

    message_to_cancel = do_work.message()

    # And a group that I enqueue
    g = group([message_to_cancel | do_work.message(), do_work.message()], cancel_on_error=True)
    g.run()

    # When I cancel a message of this group
    api_client.post("messages/cancel/" + message_to_cancel.message_id)

    # The whole composition should be canceled
    assert cancel_backend.is_canceled("", g.group_id)
Esempio n. 11
0
 def test_request_cancel(self, stub_broker, api_client, cancel_backend):
     message_id = "3141516"
     stub_broker.add_middleware(Cancel(backend=cancel_backend))
     api_client.post("/messages/cancel/{}".format(message_id))
     assert cancel_backend.is_canceled(message_id, None)
Esempio n. 12
0
 def test_request_cancel(self, stub_broker, cancel_backend):
     with app.test_client() as client:
         message_id = "1"
         stub_broker.add_middleware(Cancel(backend=cancel_backend))
         client.post(f"/messages/cancel/{message_id}")
         assert cancel_backend.is_canceled(message_id, None)