Skip to content

Commit 759e6f3

Browse files
committed
extend tests for thread pool additions
1 parent 17a439f commit 759e6f3

File tree

3 files changed

+55
-42
lines changed

3 files changed

+55
-42
lines changed

test/test_io.py

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,6 @@
1111
from helpers import CountingHandler
1212

1313

14-
class NullHandler:
15-
16-
def node(self, n):
17-
pass
18-
19-
20-
def _run_file(fn):
21-
with osmium.io.Reader(fn) as rd:
22-
osmium.apply(rd, NullHandler())
23-
24-
2514
@pytest.mark.parametrize('as_string', [True, False])
2615
def test_file_simple(tmp_path, as_string):
2716
fn = tmp_path / f"{uuid.uuid4()}.opl"
@@ -48,36 +37,12 @@ def test_file_with_format(tmp_path, as_string):
4837
assert n.id == 1
4938

5039

51-
def test_node_only(test_data):
52-
_run_file(test_data('n1'))
53-
54-
55-
def test_way_only(test_data):
56-
_run_file(test_data('w1 Nn1,n2,n3'))
57-
58-
59-
def test_relation_only(test_data):
60-
_run_file(test_data('r573 Mw1@'))
61-
62-
63-
def test_node_with_tags(test_data):
64-
_run_file(test_data('n32 Tbar=xx'))
65-
66-
67-
def test_way_with_tags(test_data):
68-
_run_file(test_data('w5666 Nn1,n2,n3 Tbar=xx'))
69-
70-
71-
def test_relation_with_tags(test_data):
72-
_run_file(test_data('r573 Mw1@ Tbar=xx'))
73-
74-
7540
def test_broken_timestamp(test_data):
7641
fn = test_data('n1 tx')
7742

7843
with osmium.io.Reader(fn) as rd:
7944
with pytest.raises(RuntimeError):
80-
osmium.apply(rd, NullHandler())
45+
osmium.apply(rd, CountingHandler())
8146

8247

8348
@pytest.mark.parametrize('as_string', [True, False])
@@ -101,11 +66,38 @@ def test_file_header(tmp_path, as_string):
10166

10267
def test_reader_with_filebuffer():
10368
rd = osmium.io.Reader(osmium.io.FileBuffer('n1 x4 y1'.encode('utf-8'), 'opl'))
104-
handler = CountingHandler()
69+
try:
70+
handler = CountingHandler()
71+
72+
osmium.apply(rd, handler)
73+
74+
assert handler.counts == [1, 0, 0, 0]
75+
assert rd.eof()
76+
finally:
77+
rd.close()
78+
79+
80+
def test_reader_with_separate_thread_pool(test_data):
81+
with osmium.io.Reader(test_data('n1 x1 y1'), thread_pool=osmium.io.ThreadPool()) as rd:
82+
for obj in osmium.OsmFileIterator(rd):
83+
assert obj.id == 1
84+
85+
86+
@pytest.mark.parametrize("entities,expected", [(osmium.osm.NODE, [2, 0, 0, 0]),
87+
(osmium.osm.ALL, [2, 1, 1, 0])])
88+
def test_reader_with_entity_filter(test_data, entities, expected):
89+
fn = test_data("""\
90+
n1 x1 y2
91+
n2 x1 y3
92+
w34 Nn1,n2
93+
r67 Mw34@
94+
""")
10595

106-
osmium.apply(rd, handler)
96+
h = CountingHandler()
97+
with osmium.io.Reader(fn, entities) as rd:
98+
osmium.apply(rd, h)
10799

108-
assert handler.counts == [1, 0, 0, 0]
100+
assert h.counts == expected
109101

110102

111103
def test_thread_pool():

test/test_replication.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,28 @@ def test_get_diff_url(inp, outp):
4848
assert outp, svr.get_diff_url(inp)
4949

5050

51-
def test_get_newest_change_from_file(tmp_path):
51+
@pytest.mark.parametrize("as_string", [True, False])
52+
def test_get_newest_change_from_file(tmp_path, as_string):
5253
fn = tmp_path / f"{uuid.uuid4()}.opl"
5354
fn.write_text('n6365 v1 c63965061 t2018-10-29T03:56:07Z i8369524 ux x1 y7')
5455

55-
val = osmium.replication.newest_change_from_file(str(fn))
56+
if as_string:
57+
fn = str(fn)
58+
59+
val = osmium.replication.newest_change_from_file(fn)
5660
assert val == mkdate(2018, 10, 29, 3, 56, 7)
5761

5862

63+
def test_get_newest_change_from_reader():
64+
fb = osmium.io.FileBuffer(
65+
'n6365 v1 t2018-10-29T03:56:07Z x1 y7\n'
66+
'n6366 v1 t2018-10-29T04:56:07Z x1 y7\n'.encode('utf-8'), 'opl')
67+
68+
with osmium.io.Reader(fb, thread_pool=osmium.io.ThreadPool()) as rd:
69+
val = osmium.replication.newest_change_from_file(rd)
70+
assert val == mkdate(2018, 10, 29, 4, 56, 7)
71+
72+
5973
def test_get_state_valid(httpserver):
6074
httpserver.expect_request('/state.txt').respond_with_data("""\
6175
#Sat Aug 26 11:04:04 UTC 2017

test/test_writer.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
def test_writer(tmp_path):
2121
@contextmanager
2222
def _WriteExpect(filename, expected):
23-
with osmium.SimpleWriter(str(filename), 1024*1024) as writer:
23+
with osmium.SimpleWriter(filename, 1024*1024) as writer:
2424
yield writer
2525

2626
assert filename.read_text().strip() == expected
@@ -373,3 +373,10 @@ def test_write_to_file(tmp_path):
373373

374374
with osmium.SimpleWriter(osmium.io.File(test_file, 'opl'), bufsz=4000) as writer:
375375
writer.add_node(osmium.osm.mutable.Node(id=123))
376+
377+
378+
def test_write_with_pool(tmp_path):
379+
test_file = tmp_path / f"{uuid.uuid4()}.opl"
380+
381+
with osmium.SimpleWriter(test_file, bufsz=400, thread_pool=osmium.io.ThreadPool()) as writer:
382+
writer.add_node(osmium.osm.mutable.Node(id=123))

0 commit comments

Comments
 (0)