def leave_park(visitor):
    _, _, broker, _ = visitor
    gate_name = random_gate()
    gate_info = gate_node_infos[gate_name]

    message = LocalMessage('leave_park', gate=gate_info)
    broker.add_incoming_message(message)
def test_start_election(visitor_repository):
    node = GateNode(nodes[0], [nodes[1], nodes[2]], visitor_repository)

    assert list(node.process_message(LocalMessage('start_election'))) == \
        [NetworkMessage('election_started', nodes[0], nodes[1]), NetworkMessage('election_started', nodes[0], nodes[2])]
    
    assert node.state == GateNode.STATE_INITIATED
def test_terminate():
    node = GateNode(nodes[0], [nodes[1], nodes[2]], visitor_repository)

    assert list(node.process_message(LocalMessage('terminate'))) == \
        [NetworkMessage('terminated', nodes[0], nodes[1]),
            NetworkMessage('terminated', nodes[0], nodes[2]),
            None]
def test_hello(visitor_repository):
    node = GateNode(nodes[0], [nodes[1], nodes[2]], visitor_repository)

    assert list(node.process_message(LocalMessage('say_hello'))) == \
        [NetworkMessage('hello', nodes[0], nodes[1]), NetworkMessage('hello', nodes[0], nodes[2])]
    
    assert list(node.process_message(NetworkMessage('hello', nodes[1], nodes[0]))) == \
        [NetworkMessage('hey', nodes[0], nodes[1])]
def test_remove_leader():
    node = GateNode(nodes[0], [nodes[1], nodes[2]], visitor_repository)
    node.leader = nodes[0]

    assert list(node.process_message(LocalMessage('remove_leader'))) == \
        [NetworkMessage('leader_removed', nodes[0], nodes[1]),
            NetworkMessage('leader_removed', nodes[0], nodes[2])]
    assert node.leader is None
示例#6
0
def test_enter_refused():
    node = VisitorNode(nodes[0])

    assert list(node.process_message(LocalMessage('enter_park', gate=nodes[1]))) == \
        [NetworkMessage('enter_request', nodes[0], nodes[1])]
    assert node.state == VisitorNode.STATE_ENTERING

    assert not list(
        node.process_message(
            NetworkMessage('enter_response', nodes[1], nodes[0],
                           allowed=False)))
    assert node.state == VisitorNode.STATE_IDLE
示例#7
0
def test_leave_rejected():
    node = VisitorNode(nodes[0])
    node.state = VisitorNode.STATE_ENTERED

    assert list(node.process_message(LocalMessage('leave_park', gate=nodes[1]))) == \
        [NetworkMessage('leave_request', nodes[0], nodes[1])]
    assert node.state == VisitorNode.STATE_LEAVING

    assert not list(
        node.process_message(
            NetworkMessage('leave_response', nodes[1], nodes[0],
                           allowed=False)))
    assert node.state == VisitorNode.STATE_ENTERED
示例#8
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)
示例#9
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)

_, broker, _ = random.choice(list(gate_nodes.values()))

message = LocalMessage('start_election')
broker.add_incoming_message(message)
示例#10
0
def remove_leader(gate_name):
    _, broker, _ = gate_nodes[gate_name]

    message = LocalMessage('remove_leader')
    broker.add_incoming_message(message)
示例#11
0
def terminate_gate(gate_name):
    _, broker, _ = gate_nodes[gate_name]

    message = LocalMessage('terminate')
    broker.add_incoming_message(message)
示例#12
0
def start_election(gate_name):
    _, broker, _ = gate_nodes[gate_name]

    message = LocalMessage('start_election')
    broker.add_incoming_message(message)