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_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_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_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) broadcast_block( block.serialize(use_bencode=False, include_suffix=True, include_moves=True, include_hash=True)) assert node.last_connected_at > now assert fx_session.query(Block).count() == 1 assert fx_other_session.query(Block).get(block.id)