summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Ludwig <f.ludwig@greyrook.com>2020-11-21 13:31:27 +0100
committerFlorian Ludwig <f.ludwig@greyrook.com>2020-11-21 14:08:01 +0100
commit508caa425d7ab4857666a73ed23853ad02044d46 (patch)
treeb901dd3f413ec905d3ea41d377a4827d82fc0142
parent02d4af124463050721bfb58cd3068c125fd007ac (diff)
downloadrdflib-508caa425d7ab4857666a73ed23853ad02044d46.tar.gz
also use retry in sparqlstore tests
-rw-r--r--test/helper.py23
-rw-r--r--test/test_sparql_service.py34
-rw-r--r--test/test_sparqlstore.py29
3 files changed, 48 insertions, 38 deletions
diff --git a/test/helper.py b/test/helper.py
new file mode 100644
index 00000000..12c529d5
--- /dev/null
+++ b/test/helper.py
@@ -0,0 +1,23 @@
+import time
+import urllib.error
+
+import rdflib
+import rdflib.query
+
+
+MAX_RETRY = 3
+def query_with_retry(graph: rdflib.Graph, query: str, **kwargs) -> rdflib.query.Result:
+ backoff = 0
+ for i in range(MAX_RETRY):
+ try:
+ result = graph.query(query, **kwargs)
+ 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
diff --git a/test/test_sparql_service.py b/test/test_sparql_service.py
index f8454cc6..ea1af413 100644
--- a/test/test_sparql_service.py
+++ b/test/test_sparql_service.py
@@ -1,26 +1,8 @@
-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
+from . import helper
def test_service():
@@ -36,7 +18,7 @@ def test_service():
<http://www.w3.org/2000/01/rdf-schema#comment> ?dbpComment .
} } } limit 2"""
- results = run_query_and_retry_on_network_error(g, q)
+ results = helper.query_with_retry(g, q)
assert len(results) == 2
for r in results:
@@ -58,7 +40,7 @@ def test_service_with_bind():
<http://dbpedia.org/ontology/deathPlace> ?dbpDeathPlace .
} } } limit 2"""
- results = run_query_and_retry_on_network_error(g, q)
+ results = helper.query_with_retry(g, q)
assert len(results) == 2
for r in results:
@@ -80,7 +62,7 @@ def test_service_with_values():
<http://dbpedia.org/ontology/deathPlace> ?dbpDeathPlace .
} } } limit 2"""
- results = run_query_and_retry_on_network_error(g, q)
+ results = helper.query_with_retry(g, q)
assert len(results) == 2
for r in results:
@@ -96,7 +78,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 = run_query_and_retry_on_network_error(g, q)
+ results = helper.query_with_retry(g, q)
assert len(results) == 2
for r in results:
@@ -113,7 +95,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 = run_query_and_retry_on_network_error(g, q)
+ results = helper.query_with_retry(g, q)
assert len(results) == 2
for r in results:
@@ -130,7 +112,7 @@ def test_service_with_implicit_select_and_base():
{
values (?s ?p ?o) {(<a> <b> 1) (<a> <b> 2)}
}} limit 2"""
- results = run_query_and_retry_on_network_error(g, q)
+ results = helper.query_with_retry(g, q)
assert len(results) == 2
for r in results:
@@ -147,7 +129,7 @@ def test_service_with_implicit_select_and_allcaps():
?s <http://purl.org/linguistics/gold/hypernym> <http://dbpedia.org/resource/Leveller> .
}
} LIMIT 3"""
- results = run_query_and_retry_on_network_error(g, q)
+ results = helper.query_with_retry(g, q)
assert len(results) == 3
diff --git a/test/test_sparqlstore.py b/test/test_sparqlstore.py
index 63b475cc..8154b10c 100644
--- a/test/test_sparqlstore.py
+++ b/test/test_sparqlstore.py
@@ -7,6 +7,9 @@ from http.server import BaseHTTPRequestHandler, HTTPServer
import socket
from threading import Thread
+from . import helper
+
+
try:
assert len(urlopen("http://dbpedia.org/sparql").read()) > 0
except:
@@ -30,7 +33,7 @@ class SPARQLStoreDBPediaTestCase(unittest.TestCase):
def test_Query(self):
query = "select distinct ?Concept where {[] a ?Concept} LIMIT 1"
- res = self.graph.query(query, initNs={})
+ res = helper.query_with_retry(self.graph, query, initNs={})
for i in res:
assert type(i[0]) == URIRef, i[0].n3()
@@ -39,7 +42,7 @@ class SPARQLStoreDBPediaTestCase(unittest.TestCase):
SELECT ?label WHERE
{ ?s a xyzzy:Concept ; xyzzy:prefLabel ?label . } LIMIT 10
"""
- res = self.graph.query(
+ res = helper.query_with_retry(self.graph,
query, initNs={"xyzzy": "http://www.w3.org/2004/02/skos/core#"}
)
for i in res:
@@ -60,12 +63,12 @@ class SPARQLStoreDBPediaTestCase(unittest.TestCase):
SELECT ?label WHERE
{ ?s a xyzzy:Concept ; xyzzy:prefLabel ?label . } LIMIT 10
"""
- res = self.graph.query(prologue + query)
+ res = helper.query_with_retry(self.graph, prologue + query)
for i in res:
assert type(i[0]) == Literal, i[0].n3()
def test_counting_graph_and_store_queries(self):
- q = """
+ query = """
SELECT ?s
WHERE {
?s ?p ?o .
@@ -74,19 +77,21 @@ class SPARQLStoreDBPediaTestCase(unittest.TestCase):
"""
g = Graph("SPARQLStore")
g.open(self.path)
- c = 0
- for r in g.query(q):
- c += 1
+ count = 0
+ result = helper.query_with_retry(g, query)
+ for _ in result:
+ count += 1
- assert c == 5, "Graph(\"SPARQLStore\") didn't return 5 records"
+ assert count == 5, "Graph(\"SPARQLStore\") didn't return 5 records"
from rdflib.plugins.stores.sparqlstore import SPARQLStore
st = SPARQLStore(query_endpoint=self.path)
- c = 0
- for r in st.query(q):
- c += 1
+ count = 0
+ result = helper.query_with_retry(st, query)
+ for _ in result:
+ count += 1
- assert c == 5, "SPARQLStore() didn't return 5 records"
+ assert count == 5, "SPARQLStore() didn't return 5 records"
class SPARQLStoreUpdateTestCase(unittest.TestCase):