def test_exception_raised_no_dlq(): """If an exception is raised and there's no DLQ, the proc should crash, (without acking).""" proc: missive.Processor[missive.RawMessage] = missive.Processor() @proc.handle_for(always) def crash(message, ctx): raise RuntimeError("bad bytes!") with proc.test_client() as test_client: blank_message = missive.RawMessage(b"") with pytest.raises(RuntimeError): test_client.send(blank_message)
def test_no_matching_handler(): processor: m.Processor[m.RawMessage] = m.Processor() @processor.handle_for(never) def non_matching_handler(message, ctx): assert False with processor.test_client() as test_client: blank_message = m.RawMessage(b"") with pytest.raises(RuntimeError): test_client.send(blank_message) assert blank_message not in test_client.acked
def test_exception_raised_with_a_dlq(): """If an exception is raised and there's a DLQ, it's written to the DLQ, acked and the processor gets on with it's life.""" proc: missive.Processor[missive.RawMessage] = missive.Processor() dlq: missive.DLQ = {} proc.set_dlq(dlq) @proc.handle_for(always) def crash(message, ctx): raise RuntimeError("bad bytes!") with proc.test_client() as test_client: blank_message = missive.RawMessage(b"") test_client.send(blank_message) assert dlq == {blank_message.message_id: (blank_message, "bad bytes!")} assert blank_message in test_client.acked
def test_no_matching_handler(): """Messages for which no handler matches should be written to the DLQ""" dlq: Dict = {} processor: m.Processor[m.RawMessage] = m.Processor() processor.set_dlq(dlq) @processor.handle_for(never) def non_matching_handler(message, ctx): assert False with processor.test_client() as test_client: blank_message = m.RawMessage(b"") test_client.send(blank_message) assert blank_message in test_client.acked assert list(dlq.values()) == [(blank_message, "no matching handlers")]
def test_one_matching_handler(): processor: m.Processor[m.RawMessage] = m.Processor() flag = False @processor.handle_for(always) def flip_bit(message: m.RawMessage, ctx: m.HandlingContext[m.RawMessage]) -> None: nonlocal flag flag = True ctx.ack() with processor.test_client() as test_client: blank_message = m.RawMessage(b"") test_client.send(blank_message) assert flag assert blank_message in test_client.acked
def test_multiple_matching_handlers(): processor: m.Processor[m.RawMessage] = m.Processor() @processor.handle_for(always) def a_matching_handler(message, ctx): message.ack() @processor.handle_for(lambda m: True) def another_matching_handler(message, ctx): message.ack() with processor.test_client() as test_client: blank_message = m.RawMessage(b"") with pytest.raises(RuntimeError): test_client.send(blank_message) assert blank_message not in test_client.acked
def test_multiple_matching_handlers(): dlq: Dict = {} processor: m.Processor[m.RawMessage] = m.Processor() processor.set_dlq(dlq) @processor.handle_for(always) def a_matching_handler(message, ctx): ctx.ack(message) @processor.handle_for(lambda m: True) def another_matching_handler(message, ctx): message.ack() with processor.test_client() as test_client: blank_message = m.RawMessage(b"") test_client.send(blank_message) assert blank_message in test_client.acked assert list(dlq.values()) == [(blank_message, "multiple matching handlers")]
def test_no_dlq_required(): """Test the happy path where the DLQ is never used""" dlq: Dict = {} processor: m.Processor[m.RawMessage] = m.Processor() processor.set_dlq(dlq) flag = False @processor.handle_for(always) def flip_bit(message: m.RawMessage, ctx: m.HandlingContext[m.RawMessage]) -> None: nonlocal flag flag = True ctx.ack() with processor.test_client() as test_client: blank_message = m.RawMessage(b"") test_client.send(blank_message) assert flag assert blank_message in test_client.acked
def test_one_matching_handler_among_multiple(): processor: m.Processor[m.RawMessage] = m.Processor() flag = False @processor.handle_for(always) def a_matching_handler(message, ctx): nonlocal flag flag = True ctx.ack() @processor.handle_for(never) def another_handler(message, ctx): message.ack() with processor.test_client() as test_client: blank_message = m.RawMessage(b"") test_client.send(blank_message) assert flag assert blank_message in test_client.acked