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)
def test_sync(fx_user, fx_session, fx_other_user, fx_other_session, fx_server, fx_novice_status): assert fx_other_session.query(Block).count() == 0 assert fx_session.query(Block).count() == 0 Block.sync(Node(url=fx_server.url), fx_other_session) assert fx_other_session.query(Block).count() == 0 assert fx_session.query(Block).count() == 0 Block.create(fx_other_user, []) Block.sync(Node(url=fx_server.url), fx_other_session) assert fx_other_session.query(Block).count() == 1 assert fx_session.query(Block).count() == 0 move = fx_user.create_novice(fx_novice_status) Block.create(fx_user, [move]) Block.create(fx_user, []) Block.create(fx_user, []) assert fx_other_session.query(Block).count() == 1 assert fx_other_session.query(Move).count() == 0 assert fx_session.query(Block).count() == 3 assert fx_session.query(Move).count() == 1 Block.sync(Node(url=fx_server.url), fx_other_session) assert fx_other_session.query(Block).count() == 3 assert fx_other_session.query(Move).count() == 1 assert fx_session.query(Block).count() == 3 assert fx_session.query(Move).count() == 1
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
def test_sync_node_unaviable_on_branch_point( fx_user: User, fx_session: scoped_session, fx_server: WSGIServer, fx_other_session: Session, fx_novice_status: typing.Mapping[str, str], code: int): move = fx_user.create_novice(fx_novice_status) block = Block.create(fx_user, [move]) Block.sync(Node(url=fx_server.url), fx_other_session) assert fx_other_session.query(Block).count() == 1 serialized = block.serialize(use_bencode=False, include_suffix=True, include_moves=True, include_hash=True) serialized['id'] = block.id + 1 with Mocker() as m: m.register_uri( 'GET', f'{fx_server.url}/blocks/last', json={'block': serialized}, status_code=200, ) m.register_uri( 'GET', f'{fx_server.url}/blocks/1', status_code=code, ) assert not Block.sync(Node(url=fx_server.url), fx_other_session)
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) broadcast_node(serialized={'url': fx_other_server.url}) assert not fx_session.query(Node).filter(Node.url == node2.url).first() assert node.last_connected_at == now
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 }]) broadcast_block( block.serialize(use_bencode=False, include_suffix=True, include_moves=True, include_hash=True)) assert m.call_count == expected assert node.last_connected_at > now
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() broadcast_node(serialized={'url': fx_other_server.url}) assert fx_session.query(Node).filter(Node.url == node2.url).first() assert node.last_connected_at > now
def test_sync_node_unavailable_on_get_last_block( fx_user: User, fx_session: scoped_session, fx_other_session: Session, fx_server: WSGIServer, fx_novice_status: typing.Mapping[str, str], code: int): move = fx_user.create_novice(fx_novice_status) Block.create(fx_user, [move]) with Mocker() as m: m.get(url=f'{fx_server.url}/blocks/last', status_code=code) Block.sync(Node(url=fx_server.url), fx_other_session) assert fx_other_session.query(Block).count() == 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: broadcast_node(serialized={'url': url}, sent_node=node) assert not m.called assert node.last_connected_at == now
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() broadcast_block(block.serialize(use_bencode=False, include_suffix=True, include_moves=True, include_hash=True), sent_node=node) assert node.last_connected_at == now
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'}) broadcast_node({'url': url}, my_node=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' }
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) broadcast_move(serialized=serialized) assert node.last_connected_at == now
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, ) broadcast_move(serialized=serialized, sent_node=node) assert not m.called assert node.last_connected_at == now
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) broadcast_block( block.serialize(use_bencode=False, include_suffix=True, include_moves=True, include_hash=True)) assert node.last_connected_at == now
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) broadcast_block(serialized, my_node=node) 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
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, ) broadcast_move(serialized=serialized, my_node=node) expected['sent_node'] = 'http://test.neko' assert node.last_connected_at > now # check request.json value assert m.request_history[0].json() == expected
def test_find_branch_point_404(fx_session: scoped_session): node = Node(url='http://test.neko') with Mocker() as m: m.get('http://test.neko/blocks/1', status_code=404) assert find_branch_point(node, fx_session, 1, 1) == 0
def test_find_branch_point_raise_error(fx_session: scoped_session, code: int): node = Node(url='http://test.neko') with raises(NodeUnavailable), Mocker() as m: m.get('http://test.neko/blocks/1', status_code=500) find_branch_point(node, fx_session, 1, 1)
def test_find_branch_point(fx_session: scoped_session, fx_server: WSGIServer, fx_user: User, value: int, high: int, expected: int): node = Node(url=fx_server.url) Block.create(fx_user, []) assert find_branch_point(node, fx_session, value, high) == expected