diff options
author | Florian Ludwig <f.ludwig@greyrook.com> | 2020-11-21 09:20:18 +0100 |
---|---|---|
committer | Florian Ludwig <f.ludwig@greyrook.com> | 2020-11-21 09:20:18 +0100 |
commit | 02d4af124463050721bfb58cd3068c125fd007ac (patch) | |
tree | 612917cd9e2fd3fa9afedeec4d52b9f2f48a1546 | |
parent | dde9db804d30fe0ab0c3291c105093ed691b0ef4 (diff) | |
download | rdflib-02d4af124463050721bfb58cd3068c125fd007ac.tar.gz |
retry on network error
-rw-r--r-- | test/test_sparql_service.py | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/test/test_sparql_service.py b/test/test_sparql_service.py index 550bfcb2..f8454cc6 100644 --- a/test/test_sparql_service.py +++ b/test/test_sparql_service.py @@ -1,7 +1,27 @@ +import time +import urllib.error + from rdflib import Graph, URIRef, Literal, Variable from rdflib.plugins.sparql import prepareQuery from rdflib.compare import isomorphic +MAX_RETRY = 3 +def run_query_and_retry_on_network_error(graph, query): + backoff = 0 + for i in range(MAX_RETRY): + try: + result = graph.query(query) + result.bindings # access bindings to ensure no lazy loading + return result + except urllib.error.URLError as e: + if i == MAX_RETRY -1: + raise e + + backoff_s = 1.2 ** backoff + print(f"Network eroror {e} during query, waiting for {backoff_s}s and retrying") + time.sleep(1) + backoff += 1 + def test_service(): g = Graph() @@ -16,7 +36,7 @@ def test_service(): <http://www.w3.org/2000/01/rdf-schema#comment> ?dbpComment . } } } limit 2""" - results = g.query(q) + results = run_query_and_retry_on_network_error(g, q) assert len(results) == 2 for r in results: @@ -38,7 +58,7 @@ def test_service_with_bind(): <http://dbpedia.org/ontology/deathPlace> ?dbpDeathPlace . } } } limit 2""" - results = g.query(q) + results = run_query_and_retry_on_network_error(g, q) assert len(results) == 2 for r in results: @@ -60,7 +80,7 @@ def test_service_with_values(): <http://dbpedia.org/ontology/deathPlace> ?dbpDeathPlace . } } } limit 2""" - results = g.query(q) + results = run_query_and_retry_on_network_error(g, q) assert len(results) == 2 for r in results: @@ -76,7 +96,7 @@ def test_service_with_implicit_select(): { values (?s ?p ?o) {(<http://example.org/a> <http://example.org/b> 1) (<http://example.org/a> <http://example.org/b> 2)} }} limit 2""" - results = g.query(q) + results = run_query_and_retry_on_network_error(g, q) assert len(results) == 2 for r in results: @@ -93,7 +113,7 @@ def test_service_with_implicit_select_and_prefix(): { values (?s ?p ?o) {(ex:a ex:b 1) (<http://example.org/a> <http://example.org/b> 2)} }} limit 2""" - results = g.query(q) + results = run_query_and_retry_on_network_error(g, q) assert len(results) == 2 for r in results: @@ -110,7 +130,7 @@ def test_service_with_implicit_select_and_base(): { values (?s ?p ?o) {(<a> <b> 1) (<a> <b> 2)} }} limit 2""" - results = g.query(q) + results = run_query_and_retry_on_network_error(g, q) assert len(results) == 2 for r in results: @@ -127,7 +147,7 @@ def test_service_with_implicit_select_and_allcaps(): ?s <http://purl.org/linguistics/gold/hypernym> <http://dbpedia.org/resource/Leveller> . } } LIMIT 3""" - results = g.query(q) + results = run_query_and_retry_on_network_error(g, q) assert len(results) == 3 |