Ejemplo n.º 1
0
def test_read_write_delete(tmpdir):
    filename = tmpdir.join('repository.json')
    repository = Repository(filename)

    assert repository.read_state() is None

    repository.write_state(State(capacity=10, visitors=[1, 2, 3]))
    assert repository.read_state() == State(capacity=10, visitors=[1, 2, 3])

    repository.delete_state()
    assert repository.read_state() is None
Ejemplo n.º 2
0
def test_enter():
    state = State(capacity=2, visitors=[100])

    with pytest.raises(AssertionError):
        state.enter(100)

    state.enter(200)
    assert state.visitors == [100, 200]

    with pytest.raises(AssertionError):
        state.enter(300)
Ejemplo n.º 3
0
def test_leave():
    state = State(capacity=2, visitors=[100])

    with pytest.raises(AssertionError):
        state.leave(200)

    state.leave(100)
    assert state.visitors == []
def test_leave_request(visitor_repository):
    visitor_repository.write_state(State(capacity=1, visitors=[nodes[1].id]))

    node = GateNode(nodes[0], [], visitor_repository)
    node.leader = nodes[5]

    assert list(node.process_message(NetworkMessage('leave_request', nodes[1], nodes[0]))) == \
        [NetworkMessage('mutex_requested', nodes[0], nodes[5])]
    assert node.leave_queue == [nodes[1]]

    assert list(node.process_message(NetworkMessage('leave_request', nodes[2], nodes[0]))) == []
    assert node.leave_queue == [nodes[1], nodes[2]]

    assert list(node.process_message(NetworkMessage('mutex_granted', nodes[5], nodes[0]))) == \
        [NetworkMessage('leave_response', nodes[0], nodes[1], allowed=True),
            NetworkMessage('leave_response', nodes[0], nodes[2], allowed=False),
            NetworkMessage('mutex_released', nodes[0], nodes[5])]
    assert node.leave_queue == []

    assert list(node.process_message(NetworkMessage('leave_request', nodes[3], nodes[0]))) == \
        [NetworkMessage('mutex_requested', nodes[0], nodes[5])]
    assert node.leave_queue == [nodes[3]]
Ejemplo n.º 5
0
import logging
import random
from amusementpark.messages import LocalMessage
from amusementpark.visitor_repository import Repository, State
from helpers import create_node_infos, create_gate_nodes

logging.basicConfig(level=logging.INFO)

network_map = {
    'a': set('bj'),
    'b': set('agc'),
    'c': set('bde'),
    'd': set('cef'),
    'e': set('cdfg'),
    'f': set('dei'),
    'g': set('behj'),
    'h': set('gi'),
    'i': set('fh'),
    'j': set('ag'),
}

repository = Repository('repository.json')
repository.write_state(State(capacity=3, visitors=[]))

gate_node_infos = create_node_infos(network_map.keys())
gate_nodes = create_gate_nodes(gate_node_infos, network_map, repository)

for _, broker, _ in gate_nodes.values():
    message = LocalMessage('say_hello')
    broker.add_incoming_message(message)