Пример #1
0
def test_remove_context_error():

    class FailingContext:

        def __init__(self):
            self.count = 1
            self.closed = False

        def close(self):
            if self.count > 0:
                self.count -= 1
                raise RuntimeError("Cannot close yet")
            self.closed = True

    ticket = Ticket(testutil.create_ticket(ops=["read"]))
    ticket.add_context(1, FailingContext())

    ticket.cancel(timeout=0)

    # If closing a context fails, keep it. The ticket cannot be removed until
    # this context is removed successfully.
    with pytest.raises(RuntimeError):
        ticket.remove_context(1)

    info = ticket.info()
    assert info["connections"] == 1

    # Calling again will close and remove the context successfully.
    ticket.remove_context(1)

    info = ticket.info()
    assert info["connections"] == 0
Пример #2
0
def test_remove_context_missing():
    ticket = Ticket(testutil.create_ticket(ops=["read"]))
    ticket.add_context(1, Context())
    assert ticket.info()["connections"] == 1

    ticket.remove_context(2)
    assert ticket.info()["connections"] == 1