Exemplo n.º 1
0
def test_transfered_ongoing_non_continues_ops():
    ticket = Ticket(testutil.create_ticket(ops=["read"]))

    # Start 2 ongoing operations.
    # ongoing: 0-0, 200-200
    # completed:
    op1 = Operation(0, 100)
    op2 = Operation(200, 100)
    ticket._add_operation(op1)
    ticket._add_operation(op2)
    assert ticket.transferred() == 0
    assert ticket.active()

    # Consume op1 data:
    # ongoing: 0-100, 200-200
    # completed:
    op1.run()
    ticket._remove_operation(op1)
    assert ticket.transferred() == 100

    # Consume op2 data:
    # ongoing: 0-100, 200-300
    # completed:
    op2.run()
    ticket._remove_operation(op2)
    assert ticket.transferred() == 200
Exemplo n.º 2
0
def test_run_operation_benchmark():
    # Run 1000000 operations with 4 concurrent threads.
    ticket = Ticket(testutil.create_ticket(ops=["read"]))
    operations = 10**6
    workers = 4
    chunk = 10**9
    step = chunk * workers // operations

    def worker(offset, size):
        while offset < size:
            ticket.run(Operation(offset, step))
            offset += step

    start = time.monotonic()

    threads = []
    try:
        for i in range(workers):
            t = util.start_thread(worker, args=(i * chunk, chunk))
            threads.append(t)
    finally:
        for t in threads:
            t.join()

    elapsed = time.monotonic() - start

    print("%d operations, %d concurrent threads in %.3f seconds (%d nsec/op)"
          % (operations, workers, elapsed, elapsed * 10**9 // operations))
Exemplo n.º 3
0
def test_remove_context_error(cfg):
    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"]), cfg)
    ctx = FailingContext()
    ticket.add_context(1, ctx)

    # If closing a context fails, fail. The ticket cannot be removed
    # until this context is closed successfully.
    with pytest.raises(RuntimeError):
        ticket.cancel(timeout=0)

    assert not ctx.closed

    # Calling again will close context successfully, and the ticket can
    # be removed.
    ticket.cancel(timeout=0)

    assert ctx.closed
Exemplo n.º 4
0
def test_transfered_ongoing_overlapping_ops():
    ticket = Ticket(testutil.create_ticket(ops=["read"]))

    # Start 2 ongoing operations.
    # ongoing: 0-0, 80-80
    # completed:
    op1 = Operation(0, 120)
    op2 = Operation(80, 120)
    ticket._add_operation(op1)
    ticket._add_operation(op2)
    assert ticket.transferred() == 0
    assert ticket.active()

    # Consume op1 data:
    # ongoing: 0-120, 80-80
    # completed:
    op1.run()
    ticket._remove_operation(op1)
    assert ticket.transferred() == 120
    assert ticket.active()

    # Consume op2 data:
    # ongoing: 0-120, 80-200
    # completed:
    op2.run()
    ticket._remove_operation(op2)
    assert ticket.transferred() == 200
    assert not ticket.active()
Exemplo n.º 5
0
def test_transfered_inactive_empty_ops():
    ticket = Ticket(testutil.create_ticket(ops=["read"]))
    ticket.run(Operation(0, 0))
    assert ticket.transferred() == 0

    ticket.run(Operation(1000, 0))
    assert ticket.transferred() == 0
Exemplo n.º 6
0
def test_cancel_wait():
    ticket = Ticket(testutil.create_ticket(ops=["read"]))

    # Add connections using this ticket.
    connections = []
    for i in range(4):
        ctx = Context()
        ticket.add_context(i, ctx)
        connections.append(ctx)

    def close_connections():
        time.sleep(0.1)
        for i in range(4):
            ticket.remove_context(i)

    info = ticket.info()
    assert not info["canceled"]
    assert info["connections"] == 4

    t = util.start_thread(close_connections)
    try:
        ticket.cancel(timeout=10)

        # After the ticket was canceled, number of connections must be zero.
        info = ticket.info()
        assert info["connections"] == 0

        # And all contexts must be closed.
        assert all(ctx.closed for ctx in connections)
    finally:
        t.join()
Exemplo n.º 7
0
def test_cancel_timeout(cfg):
    ticket = Ticket(testutil.create_ticket(ops=["read"]), cfg)

    # Add conection - having connections does not block cancelation, but we
    # cannot have ongoing operations without a connection.
    ctx = Context()
    ticket.add_context(1, ctx)

    # Ongoing operation blocks cancel.
    ticket._add_operation(Operation(0, 100))

    # Canceling will time out.
    with pytest.raises(errors.TransferCancelTimeout):
        ticket.cancel(timeout=0.001)

    # Ticket is marked as canceled, but the context was not closed.
    assert ticket.canceled
    assert not ctx.closed

    # Caller can poll ticket "active" property and remove the ticket when the
    # ticket is inactive.
    info = ticket.info()
    assert info["canceled"]
    assert info["active"]
    assert info["connections"] == 1
Exemplo n.º 8
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
Exemplo n.º 9
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
Exemplo n.º 10
0
def test_transfered_ongoing_concurrent_ops(cfg):
    ticket = Ticket(testutil.create_ticket(ops=["read"]), cfg)

    # Start 2 ongoing operations:
    # ongoing: 0-0, 100-100
    # completed:
    op1 = Operation(0, 100)
    ticket._add_operation(op1)
    assert ticket.transferred() == 0
    assert ticket.active()

    op2 = Operation(100, 100)
    ticket._add_operation(op2)
    assert ticket.transferred() == 0
    assert ticket.active()

    # Consume op1 data:
    # ongoing: 0-100, 100-100
    # completed:
    op1.run()
    ticket._remove_operation(op1)
    assert ticket.transferred() == 100
    assert ticket.active()

    # Consume op2 data:
    # ongoing: 0-100, 100-200
    # completed:
    op2.run()
    ticket._remove_operation(op2)
    assert ticket.transferred() == 200
    assert not ticket.active()
Exemplo n.º 11
0
def test_canceled_fail_add_context():
    ticket = Ticket(testutil.create_ticket(ops=["read"]))
    ticket.cancel()

    ctx = Context()

    # Adding new context must fail.
    with pytest.raises(errors.AuthorizationError):
        ticket.add_context(2, ctx)
Exemplo n.º 12
0
def test_cancel_unused():
    ticket = Ticket(testutil.create_ticket(ops=["read"]))
    ticket.cancel()

    # Ticket is canceled and can be removed immediately.
    assert ticket.canceled
    info = ticket.info()
    assert info["canceled"]
    assert info["connections"] == 0
Exemplo n.º 13
0
def test_cancel_no_connection(cfg):
    ticket = Ticket(testutil.create_ticket(ops=["read"]), cfg)
    ticket.cancel()

    # Ticket is canceled and can be removed immediately.
    assert ticket.canceled
    info = ticket.info()
    assert info["canceled"]
    assert not info["active"]
    assert info["connections"] == 0
Exemplo n.º 14
0
def test_transfered_inactive_non_continuous_ops():
    ticket = Ticket(testutil.create_ticket(ops=["read"]))
    # Run 2 non-continutes operations
    ticket.run(Operation(0, 100))
    ticket.run(Operation(200, 100))
    assert ticket.transferred() == 200

    # Run last operation filling the hole - with some overlap.
    ticket.run(Operation(80, 120))
    assert ticket.transferred() == 300
Exemplo n.º 15
0
def test_ticket_run():
    ticket = Ticket(testutil.create_ticket(ops=["read"]))
    op = Operation(0, 100)
    assert ticket.transferred() == op.done
    assert op.done == 0

    ticket.run(op)

    assert ticket.transferred() == op.done
    assert op.done == 100
Exemplo n.º 16
0
def test_transfered_inactive_unordered_ops():
    ticket = Ticket(testutil.create_ticket(ops=["read"]))
    ticket.run(Operation(100, 100))
    assert ticket.transferred() == 100

    ticket.run(Operation(0, 100))
    assert ticket.transferred() == 200

    ticket.run(Operation(200, 100))
    assert ticket.transferred() == 300
Exemplo n.º 17
0
def test_transfered_inactive_overlapping_ops():
    ticket = Ticket(testutil.create_ticket(ops=["read"]))
    ticket.run(Operation(0, 120))
    assert ticket.transferred() == 120

    ticket.run(Operation(100, 100))
    assert ticket.transferred() == 200

    ticket.run(Operation(180, 120))
    assert ticket.transferred() == 300
Exemplo n.º 18
0
def test_cancel_async():
    ticket = Ticket(testutil.create_ticket(ops=["read"]))
    ticket.add_context(1, None)
    ticket.cancel(timeout=0)

    # Ticket is canceled, but it cannot be removed.
    assert ticket.canceled
    info = ticket.info()
    assert info["canceled"]
    assert info["connections"] == 1
Exemplo n.º 19
0
def test_repr():
    ticket = Ticket(testutil.create_ticket(
        ops=["read"], filename="tmp_file"))
    ticket_repr = repr(ticket)

    info = ticket.info()
    del info["timeout"]

    for key, value in info.items():
        pair = "%s=%r" % (key, value)
        assert pair in ticket_repr
Exemplo n.º 20
0
def test_canceled_fail_run_before():
    ticket = Ticket(testutil.create_ticket(ops=["read"]))
    ticket.cancel()

    op = Operation()

    # Running operations must fail.
    with pytest.raises(errors.AuthorizationError):
        ticket.run(op)

    # Operation was not run.
    assert op.done == 0
Exemplo n.º 21
0
def test_run_benchmark(cfg, workers, client_class, mode):
    ticket = Ticket(testutil.create_ticket(size=100 * 1024**3, ops=mode), cfg)

    client = client_class(ticket, workers)

    elapsed = client.run()

    if mode != ["read", "write"]:
        assert ticket.transferred() == ticket.size

    ops = ticket.size // client.io_size
    print("%d workers, %d ops, %.3f s, %.2f ops/s" %
          (client.workers, ops, elapsed, ops / elapsed))
Exemplo n.º 22
0
def test_cancel_async(cfg):
    ticket = Ticket(testutil.create_ticket(ops=["read"]), cfg)
    ctx = Context()
    ticket.add_context(1, ctx)
    ticket._add_operation(Operation(0, 100))
    ticket.cancel(timeout=0)

    # Ticket is marked as canceled, but the context was not closed.
    assert ticket.canceled
    assert not ctx.closed

    info = ticket.info()
    assert info["canceled"]
    assert info["active"]
    assert info["connections"] == 1
Exemplo n.º 23
0
def test_cancel_ongoing_operations():
    ticket = Ticket(testutil.create_ticket(ops=["read"]))

    # Few connections are using this ticket. Each running an operation.
    ops = []
    for i in range(4):
        ctx = Context()
        op = Operation()
        ticket.add_context(i, ctx)
        ticket._add_operation(op)
        ops.append(op)

    ticket.cancel(timeout=0)

    # All ongoing operations are canceled.
    assert all(op.canceled for op in ops)
Exemplo n.º 24
0
def test_cancel_timeout():
    ticket = Ticket(testutil.create_ticket(ops=["read"]))
    ticket.add_context(1, None)

    # Canceling will time out.
    with pytest.raises(errors.TicketCancelTimeout):
        ticket.cancel(timeout=0.001)

    # But ticket is marked as canceled.
    assert ticket.canceled

    # Caller can poll ticket status and remove the ticket when number of
    # connections is zero.
    info = ticket.info()
    assert info["canceled"]
    assert info["connections"] == 1
Exemplo n.º 25
0
def test_cancel_wait(cfg):
    ticket = Ticket(testutil.create_ticket(ops=["read"]), cfg)

    # Add connections using this ticket.
    users = []
    for cid in range(4):
        ctx = Context()
        op = Operation(cid * 1024**2, 1024**2)
        ticket.add_context(cid, ctx)
        ticket._add_operation(op)
        users.append((cid, ctx, op))

    # Add idle connection.
    idle_ctx = Context()
    ticket.add_context(4, idle_ctx)

    def finish_operations():
        time.sleep(0.1)
        for cid, ctx, op in users:
            # Removing operation from a canceled ticket raises, send and error
            # and close the connection.
            try:
                ticket._remove_operation(op)
            except errors.AuthorizationError:
                ticket.remove_context(cid)

    info = ticket.info()
    assert not info["canceled"]
    assert info["connections"] == 5
    assert info["active"]

    t = util.start_thread(finish_operations)
    try:
        ticket.cancel(timeout=10)
    finally:
        t.join()

    info = ticket.info()

    # After the ticket was canceled, ticket is inactive, and all ongoging
    # connnections removed from ticket. The idle connection is left, but its
    # context is closed.

    assert not info["active"]
    assert info["connections"] == 1
    assert all(ctx.closed for cid, ctx, op in users)
    assert idle_ctx.closed
Exemplo n.º 26
0
def test_cancel_idle_connection(cfg):
    ticket = Ticket(testutil.create_ticket(ops=["read"]), cfg)
    ctx = Context()
    ticket.add_context(1, ctx)
    ticket.cancel()

    # Ticket is canceled and can be removed immediately.
    assert ticket.canceled
    assert ctx.closed

    info = ticket.info()
    assert info["canceled"]
    assert not info["active"]

    # The conection context was closed. The connection will be closed when it
    # times out or when a user send the next request.
    assert info["connections"] == 1
Exemplo n.º 27
0
def test_canceled_fail_run_after(cfg):
    ticket = Ticket(testutil.create_ticket(ops=["read"]), cfg)

    class Operation:
        def __init__(self):
            self.done = False
            self.canceled = False

        def run(self):
            self.done = True
            ticket.cancel(timeout=0.001)

        def cancel(self):
            self.canceled = True

    op = Operation()

    # If ticket was canceled while ongoing operations are running, ticket run
    # will fail removing the operations.
    with pytest.raises(errors.AuthorizationError):
        ticket.run(op)

    assert op.done
Exemplo n.º 28
0
def test_transferred_benchmark(cfg, workers):
    # Time trransferred call with multiple ongoing and completed operations.
    ticket = Ticket(testutil.create_ticket(ops=["read"]), cfg)

    ops = 10000

    # Add some completed ranges - assume worst case when ranges are not
    # continues.
    for i in range(workers):
        ticket.run(Operation(i * 1000, 100))

    # Add some ongoing operations - assume worst case when ranges are not
    # continues.
    for i in range(workers):
        ticket._add_operation(Operation(i * 1000 + 200, 100))

    # Time transferred call - merging ongoing and completed ranges.
    start = time.monotonic()
    for i in range(ops):
        ticket.transferred()
    elapsed = time.monotonic() - start

    print("%d workers, %d ops, %.3f s, %.2f ops/s" %
          (workers, ops, elapsed, ops / elapsed))
Exemplo n.º 29
0
def test_transferred_benchmark(concurrent):
    # Time trransferred call with multiple ongoing and completed operations.
    ticket = Ticket(testutil.create_ticket(ops=["read"]))

    calls = 10000

    # Add some completed ranges - assume worst case when ranges are not
    # continues.
    for i in range(concurrent):
        ticket.run(Operation(i * 1000, 100))

    # Add some ongoing operations - assume worst case when ranges are not
    # continues.
    for i in range(concurrent):
        ticket._add_operation(Operation(i * 1000 + 200, 100))

    # Time transferred call - merging ongoing and completed ranges.
    start = time.monotonic()
    for i in range(calls):
        ticket.transferred()
    elapsed = time.monotonic() - start

    print("%d concurrent operations, %d calls in %.3f seconds (%d nsec/op)"
          % (concurrent, calls, elapsed, elapsed * 10**9 // calls))
Exemplo n.º 30
0
def test_get_context():
    ticket = Ticket(testutil.create_ticket(ops=["read"]))
    ctx = Context()
    ticket.add_context(1, ctx)
    assert ticket.get_context(1) is ctx