Ejemplo n.º 1
0
def test_broadcast_move(
        fx_server: WSGIServer,
        fx_session: scoped_session,
        fx_other_server: WSGIServer,
        fx_other_session: Session,
        fx_user: User,
        fx_novice_status: typing.Mapping[str, str],
):
    now = datetime.datetime.utcnow()
    node = Node(url=fx_server.url,
                last_connected_at=now)
    node2 = Node(url=fx_other_server.url,
                 last_connected_at=datetime.datetime.utcnow())
    move = fx_user.create_novice(fx_novice_status)
    fx_session.add_all([node, node2, move])
    fx_session.commit()
    assert not fx_other_session.query(Move).get(move.id)
    serialized = move.serialize(
        use_bencode=False,
        include_signature=True,
        include_id=True,
    )
    multicast(serialized=serialized, broadcast=broadcast_move)
    assert fx_other_session.query(Move).get(move.id)
    assert node.last_connected_at > now
Ejemplo n.º 2
0
def test_broadcast_block(
        fx_server: WSGIServer,
        fx_session: scoped_session,
        fx_other_session: Session,
        fx_other_server: WSGIServer,
        fx_user: User
):
    now = datetime.datetime.utcnow()
    node = Node(url=fx_server.url,
                last_connected_at=now)
    node2 = Node(url=fx_other_server.url,
                 last_connected_at=datetime.datetime.utcnow())
    block = Block.create(fx_user, [])
    fx_session.add_all([node, node2, block])
    fx_session.flush()
    assert fx_session.query(Block).get(block.id)
    assert not fx_other_session.query(Block).get(block.id)
    multicast(
        serialized=block.serialize(
            use_bencode=False,
            include_suffix=True,
            include_moves=True,
            include_hash=True
        ),
        broadcast=broadcast_block,
    )
    assert node.last_connected_at > now
    assert fx_session.query(Block).count() == 1
    assert fx_other_session.query(Block).get(block.id)
Ejemplo n.º 3
0
def test_broadcast_node_same_url(fx_session: scoped_session):
    url = 'http://test.neko'
    now = datetime.datetime.utcnow()
    node = Node(url=url, last_connected_at=now)
    fx_session.add(node)
    fx_session.commit()
    with Mocker() as m:
        multicast(serialized={'url': url}, sent_node=node,
                  broadcast=broadcast_node)
        assert not m.called
    assert node.last_connected_at == now
Ejemplo n.º 4
0
def test_broadcast_my_node(fx_session: scoped_session):
    url = 'http://test.neko'
    now = datetime.datetime.utcnow()
    node = Node(url=url, last_connected_at=now)
    fx_session.add(node)
    fx_session.commit()
    with Mocker() as m:
        m.post('http://test.neko/nodes', json={'result': 'success'})
        multicast({'url': url}, my_node=node, broadcast=broadcast_node)
        assert node.last_connected_at > now
        # check request.json value
        assert m.request_history[0].json() == {
            'url': 'http://test.neko',
            'sent_node': 'http://test.neko'
        }
Ejemplo n.º 5
0
def broadcast_node_failed(fx_session: scoped_session,
                          fx_other_session: Session, error):
    now = datetime.datetime.utcnow()
    node = Node(url='http://test.neko',
                last_connected_at=now)
    node2 = Node(url='http://other.neko',
                 last_connected_at=datetime.datetime.utcnow())
    fx_session.add(node)
    fx_session.commit()
    fx_other_session.add(node2)
    fx_other_session.commit()
    assert not fx_session.query(Node).filter(Node.url == node2.url).first()
    with Mocker() as m:
        m.post('http://test.neko', exc=error)
        multicast(serialized={'url': fx_other_server.url},
                  broadcast=broadcast_node)
    assert not fx_session.query(Node).filter(Node.url == node2.url).first()
    assert node.last_connected_at == now
Ejemplo n.º 6
0
def test_broadcast_block_same_node(fx_session: scoped_session, fx_user: User):
    block = Block.create(fx_user, [])
    url = 'http://test.neko'
    now = datetime.datetime.utcnow()
    node = Node(url=url, last_connected_at=now)
    fx_session.add(node)
    fx_session.flush()
    multicast(
        serialized=block.serialize(
            use_bencode=False,
            include_suffix=True,
            include_moves=True,
            include_hash=True
        ),
        sent_node=node,
        broadcast=broadcast_block,
    )
    assert node.last_connected_at == now
Ejemplo n.º 7
0
def broadcast_move_failed(fx_session: scoped_session,
                          fx_user: User,
                          fx_novice_status: typing.Mapping[str, str],
                          error):
    now = datetime.datetime.utcnow()
    move = fx_user.create_novice(fx_novice_status)
    node = Node(url='http://test.neko',
                last_connected_at=now)
    fx_session.add_all([node, move])
    fx_session.commit()
    with Mocker() as m:
        serialized = move.serialize(
            use_bencode=False,
            include_signature=True,
            include_id=True,
        )
        m.post('http://test.neko', exc=error)
        multicast(serialized=serialized, broadcast=broadcast_move)
    assert node.last_connected_at == now
Ejemplo n.º 8
0
def test_broadcast_move_same_url(fx_session: scoped_session,
                                 fx_user: User,
                                 fx_novice_status: typing.Mapping[str, str]):
    url = 'http://test.neko'
    now = datetime.datetime.utcnow()
    node = Node(url=url, last_connected_at=now)
    move = fx_user.create_novice(fx_novice_status)
    fx_session.add_all([node, move])
    fx_session.commit()
    with Mocker() as m:
        serialized = move.serialize(
            use_bencode=False,
            include_signature=True,
            include_id=True,
        )
        multicast(serialized=serialized, sent_node=node,
                  broadcast=broadcast_move)
        assert not m.called
    assert node.last_connected_at == now
Ejemplo n.º 9
0
def test_broadcast_node(
        fx_server: WSGIServer,
        fx_session: scoped_session,
        fx_other_server: WSGIServer,
        fx_other_session: Session,
):
    now = datetime.datetime.utcnow()
    node = Node(url=fx_server.url,
                last_connected_at=now)
    node2 = Node(url=fx_other_server.url,
                 last_connected_at=datetime.datetime.utcnow())
    fx_session.add(node)
    fx_session.commit()
    fx_other_session.add(node2)
    fx_other_session.commit()
    assert not fx_session.query(Node).filter(Node.url == node2.url).first()
    multicast(serialized={'url': fx_other_server.url},
              broadcast=broadcast_node)
    assert fx_session.query(Node).filter(Node.url == node2.url).first()
    assert node.last_connected_at > now
Ejemplo n.º 10
0
def test_broadcast_block_retry(
        fx_session: scoped_session,
        fx_user: User, limit: int, blocks: int, expected: int
):
    for i in range(blocks):
        block = Block.create(fx_user, [])
    url = 'http://test.neko'
    now = datetime.datetime.utcnow()
    node = Node(url=url, last_connected_at=now)
    fx_session.add(node)
    fx_session.flush()
    patch = unittest.mock.patch('nekoyume.broadcast.BROADCAST_LIMIT', limit)
    with mock() as m, patch:
        m.register_uri('POST', 'http://test.neko/blocks', [
            {
                'json': {
                    'result': 'failed',
                    'block_id': 0,
                    'mesage': "new block isn't our next block."
                },
                'status_code': 403
            },
            {
                'json': {
                    'result': 'success',
                },
                'status_code': 200
            }
        ])
        multicast(
            serialized=block.serialize(
                use_bencode=False,
                include_suffix=True,
                include_moves=True,
                include_hash=True
            ),
            broadcast=broadcast_block,
        )
        assert m.call_count == expected
        assert node.last_connected_at > now
Ejemplo n.º 11
0
def test_broadcast_move_my_node(fx_session: scoped_session,
                                fx_user: User,
                                fx_novice_status: typing.Mapping[str, str]):
    url = 'http://test.neko'
    now = datetime.datetime.utcnow()
    node = Node(url=url, last_connected_at=now)
    move = fx_user.create_novice(fx_novice_status)
    fx_session.add_all([node, move])
    fx_session.commit()
    with Mocker() as m:
        m.post('http://test.neko/moves', json={'result': 'success'})
        expected = serialized = move.serialize(
            use_bencode=False,
            include_signature=True,
            include_id=True,
        )
        multicast(serialized=serialized, my_node=node,
                  broadcast=broadcast_move)
        expected['sent_node'] = 'http://test.neko'
        assert node.last_connected_at > now
        # check request.json value
        assert m.request_history[0].json() == expected
Ejemplo n.º 12
0
def test_broadcast_block_raise_exception(
        fx_session: scoped_session, fx_user: User,
        error: typing.Union[ConnectionError, Timeout]
):
    block = Block.create(fx_user, [])
    url = 'http://test.neko'
    now = datetime.datetime.utcnow()
    node = Node(url=url, last_connected_at=now)
    fx_session.add(node)
    fx_session.flush()
    with Mocker() as m:
        m.post('http://test.neko/blocks', exc=error)
        multicast(
            serialized=block.serialize(
                use_bencode=False,
                include_suffix=True,
                include_moves=True,
                include_hash=True
            ),
            broadcast=broadcast_block,
        )
        assert node.last_connected_at == now
Ejemplo n.º 13
0
def test_broadcast_block_my_node(fx_session: scoped_session, fx_user: User):
    block = Block.create(fx_user, [])
    url = 'http://test.neko'
    now = datetime.datetime.utcnow()
    node = Node(url=url, last_connected_at=now)
    fx_session.add(node)
    fx_session.flush()
    with Mocker() as m:
        m.post('http://test.neko/blocks', text='success')
        expected = serialized = block.serialize(
            use_bencode=False,
            include_suffix=True,
            include_moves=True,
            include_hash=True
        )
        multicast(serialized=serialized, my_node=node,
                  broadcast=broadcast_block)
        expected['sent_node'] = url
        assert node.last_connected_at > now
        assert node.last_connected_at > now
        # check request.json value
        assert m.request_history[0].json() == expected