Пример #1
0
def test_Server():
    backend = MemoryBackend()
    doc = random_string()
    server = Server(doc, backend)
    assert server.document == doc
    assert server.backend == backend

    op1 = random_operation(doc)
    doc1 = op1(doc)
    assert server.receive_operation('user1', 0, op1) == op1
    assert server.document == doc1
    assert backend.operations == [op1]

    op2 = random_operation(doc1)
    doc2 = op2(doc1)
    assert server.receive_operation('user1', 1, op2) == op2
    assert backend.operations == [op1, op2]
    assert server.document == doc2

    server.receive_operation('user1', 1, op2) == None
    assert backend.operations == [op1, op2]
    assert server.document == doc2

    op2_b = random_operation(doc1)
    (op2_b_p, op2_p) = TextOperation.transform(op2_b, op2)
    assert server.receive_operation('user2', 1, op2_b) == op2_b_p
    assert backend.operations == [op1, op2, op2_b_p]
    assert server.document == op2_p(op2_b(doc1))
Пример #2
0
def test_client_server_interaction():
    document = random_string()
    server = Server(document, MemoryBackend())

    def server_receive(msg):
        (client_id, revision, operation) = msg
        operation_p = server.receive_operation(client_id, revision, operation)
        msg = (client_id, operation_p)
        client1_receive_channel.write(msg)
        client2_receive_channel.write(msg)

    def client_receive(client):
        def rcv(msg):
            (client_id, operation) = msg
            if client.id == client_id:
                client.server_ack()
            else:
                client.apply_server(operation)

        return rcv

    client1_send_channel = NetworkChannel(server_receive)
    client1 = MyClient(0, 'client1', document, client1_send_channel)
    client1_receive_channel = NetworkChannel(client_receive(client1))

    client2_send_channel = NetworkChannel(server_receive)
    client2 = MyClient(0, 'client2', document, client2_send_channel)
    client2_receive_channel = NetworkChannel(client_receive(client2))

    channels = [
        client1_send_channel, client1_receive_channel, client2_send_channel,
        client2_receive_channel
    ]

    def can_receive():
        for channel in channels:
            if not channel.is_empty():
                return True
        return False

    def receive_random():
        filtered = []
        for channel in channels:
            if not channel.is_empty():
                filtered.append(channel)
        random.choice(filtered).receive()

    n = 64
    while n > 0:
        if not can_receive() or random.random() < 0.75:
            client = random.choice([client1, client2])
            client.perform_operation()
        else:
            receive_random()
        n -= 1

    while can_receive():
        receive_random()

    assert server.document == client1.document == client2.document
def test_client_server_interaction():
    document = random_string()
    server = Server(document, MemoryBackend())

    def server_receive(msg):
        (client_id, revision, operation) = msg
        operation_p = server.receive_operation(client_id, revision, operation)
        msg = (client_id, operation_p)
        client1_receive_channel.write(msg)
        client2_receive_channel.write(msg)

    def client_receive(client):
        def rcv(msg):
            (client_id, operation) = msg
            if client.id == client_id:
                client.server_ack()
            else:
                client.apply_server(operation)
        return rcv

    client1_send_channel = NetworkChannel(server_receive)
    client1 = MyClient(0, 'client1', document, client1_send_channel)
    client1_receive_channel = NetworkChannel(client_receive(client1))

    client2_send_channel = NetworkChannel(server_receive)
    client2 = MyClient(0, 'client2', document, client2_send_channel)
    client2_receive_channel = NetworkChannel(client_receive(client2))

    channels = [
        client1_send_channel, client1_receive_channel,
        client2_send_channel, client2_receive_channel
    ]

    def can_receive():
        for channel in channels:
            if not channel.is_empty():
                return True
        return False

    def receive_random():
        filtered = []
        for channel in channels:
            if not channel.is_empty():
                filtered.append(channel)
        random.choice(filtered).receive()

    n = 64
    while n > 0:
        if not can_receive() or random.random() < 0.75:
            client = random.choice([client1, client2])
            client.perform_operation()
        else:
            receive_random()
        n -= 1

    while can_receive():
        receive_random()

    assert server.document == client1.document == client2.document
Пример #4
0

def server(document):
    server = Server(document, MemoryBackend())

    def server_receive(msg):
        (client_id, revision, operation) = msg
        operation_p = server.receive_operation(client_id, revision, operation)

        msg = (client_id, operation_p)
        # client1_receive_channel.write(msg)
        client_1_q.put(msg)
        # client2_receive_channel.write(msg)
        client_2_q.put(msg)
        print "server:" + server.document

    # Check the queue
    while True:
        msg = server_q.get()
        server_receive(msg)


if __name__ == '__main__':
    # Run client 1
    # Run client 2
    # Run server
    document = random_string()
    print "document:" + document
    thread_3 = Thread(target=server, args=(document, ))
    thread_3.start()