def test_raises_error_if_primary_and_backup_both_failed():
    executor = ThreadPoolExecutor(2)
    client = jsonrpc.Client("url",
                            rs=jsonrpc.RequestWithBackups(backups=["url"],
                                                          executor=executor))
    with pytest.raises(jsonrpc.NetworkError):
        assert client.get_currencies()

    client = jsonrpc.Client("url",
                            rs=jsonrpc.RequestWithBackups(backups=["url"],
                                                          executor=executor,
                                                          fallback=True))
    with pytest.raises(jsonrpc.NetworkError):
        assert client.get_currencies()
def test_fallback_to_backups_when_primary_failed():
    client = jsonrpc.Client(
        "primary",
        rs=jsonrpc.RequestWithBackups(backups=["backup"],
                                      executor=ThreadPoolExecutor(2),
                                      fallback=True),
    )
    client._send_http_request = gen_metadata_response(client, fail="primary")
    for _ in range(10):
        assert client.get_metadata().script_hash_allow_list == ["backup"]
def test_fallback_to_second_if_first_failed():
    executor = ThreadPoolExecutor(2)
    rs = jsonrpc.RequestWithBackups(backups=["backup"], executor=executor)
    client = jsonrpc.Client("primary", rs=rs)

    # primary will fail immediately, backup is slow but success
    client._send_http_request = gen_metadata_response(client,
                                                      fail="primary",
                                                      snap="backup")
    assert client.get_metadata().script_hash_allow_list == ["backup"]
    executor.shutdown()
def test_fallback_strategy_always_returns_primary_response_if_it_successes():
    client = jsonrpc.Client(
        "primary",
        rs=jsonrpc.RequestWithBackups(backups=["backup"],
                                      executor=ThreadPoolExecutor(2),
                                      fallback=True),
    )
    client._send_http_request = gen_metadata_response(client)
    for _ in range(10):
        metadata = client.get_metadata()
        assert metadata.script_hash_allow_list == ["primary"]
def test_first_success_strategy_returns_first_completed_success_response():
    executor = ThreadPoolExecutor(2)
    rs = jsonrpc.RequestWithBackups(backups=["backup"], executor=executor)
    client = jsonrpc.Client("primary", rs=rs)

    client._send_http_request = gen_metadata_response(client, snap="primary")
    assert client.get_metadata().script_hash_allow_list == ["backup"]

    client._send_http_request = gen_metadata_response(client, snap="backup")
    assert client.get_metadata().script_hash_allow_list == ["primary"]
    executor.shutdown()