Skip to content

Commit a7b553f

Browse files
committed
Tests: Replace remaining uses of wait_for_download_to_start_and_finish() with wait_for_download_task_to_start_and_finish()
Also: * Tests: Remove now-unused wait_for_download_to_start_and_finish()
1 parent afdec04 commit a7b553f

File tree

10 files changed

+148
-233
lines changed

10 files changed

+148
-233
lines changed

src/crystal/tests/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,13 @@ download.
324324
It's not recommended to manually wait for a download to complete using a
325325
fixed timeout because they can take a highly variable amount of time to complete,
326326
depending on how many embedded subresources there are, among other factors.
327-
Instead use the `wait_for_download_to_start_and_finish` utility method,
327+
Instead use the `wait_for_download_task_to_start_and_finish` utility method,
328328
which looks for continued *progress* in a download, regardless of how long the
329329
total download task takes to finish:
330330

331331
```
332-
home_ti.Expand()
333-
await wait_for_download_to_start_and_finish(mw.task_tree)
332+
async with wait_for_download_task_to_start_and_finish(project):
333+
home_ti.Expand()
334334
```
335335

336336
## Waiting for other things to happen

src/crystal/tests/test_download.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from crystal.tests.util.controls import click_button, TreeItem
88
from crystal.tests.util.runner import bg_sleep
99
from crystal.tests.util.server import MockHttpServer, served_project
10-
from crystal.tests.util.tasks import wait_for_download_to_start_and_finish
10+
from crystal.tests.util.tasks import wait_for_download_task_to_start_and_finish
1111
from crystal.tests.util.wait import DEFAULT_WAIT_PERIOD, wait_for
1212
from crystal.tests.util.windows import NewGroupDialog, OpenOrCreateDialog
1313
import crystal.tests.util.xtempfile as xtempfile
@@ -37,12 +37,11 @@ async def test_given_downloading_resource_when_start_download_resource_then_exis
3737
async with (await OpenOrCreateDialog.wait_for()).create() as (mw, project):
3838
r = Resource(project, home_url)
3939

40-
rr_future = r.download()
41-
42-
rr_future2 = r.download()
43-
assert rr_future2 is rr_future
44-
45-
await wait_for_download_to_start_and_finish(mw.task_tree)
40+
async with wait_for_download_task_to_start_and_finish(project):
41+
rr_future = r.download()
42+
43+
rr_future2 = r.download()
44+
assert rr_future2 is rr_future
4645

4746

4847
# ------------------------------------------------------------------------------

src/crystal/tests/test_entitytree.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from crystal.tests.util.downloads import network_down
88
from crystal.tests.util.runner import bg_sleep
99
from crystal.tests.util.server import extracted_project, served_project
10-
from crystal.tests.util.tasks import wait_for_download_to_start_and_finish
10+
from crystal.tests.util.tasks import wait_for_download_task_to_start_and_finish
1111
from crystal.tests.util.wait import (
1212
DEFAULT_WAIT_PERIOD, first_child_of_tree_item_is_not_loading_condition,
1313
wait_for,
@@ -725,11 +725,11 @@ async def test_given_rr_is_downloaded_and_is_error_when_expand_rrn_then_shows_er
725725
with network_down():
726726
r = Resource(project, home_url)
727727
home_rr = RootResource(project, 'Home', r)
728-
revision_future = home_rr.download()
729-
while not revision_future.done():
730-
await bg_sleep(DEFAULT_WAIT_PERIOD)
731-
# Wait for download to complete, including the trailing wait
732-
await wait_for_download_to_start_and_finish(mw.task_tree, immediate_finish_ok=True)
728+
async with wait_for_download_task_to_start_and_finish(project):
729+
revision_future = home_rr.download()
730+
while not revision_future.done():
731+
await bg_sleep(DEFAULT_WAIT_PERIOD)
732+
# (Wait for download to complete, including the trailing wait)
733733

734734
rr = revision_future.result()
735735
assert DownloadErrorDict(
@@ -780,16 +780,15 @@ async def test_given_rr_is_downloaded_but_revision_body_missing_when_expand_rrn_
780780
# TODO: In the future, block on the redownload finishing
781781
# and list the links in the redownloaded revision,
782782
# WITHOUT needing to reopen the project later
783-
home_ti.Expand()
784-
await wait_for(first_child_of_tree_item_is_not_loading_condition(home_ti))
785-
(error_ti,) = home_ti.Children
786-
assert (
787-
'Cannot list links: URL revision body is missing. Recommend delete and redownload.' ==
788-
error_ti.Text
789-
)
790-
791-
# Wait for redownload to complete
792-
await wait_for_download_to_start_and_finish(mw.task_tree, immediate_finish_ok=True)
783+
async with wait_for_download_task_to_start_and_finish(project):
784+
home_ti.Expand()
785+
await wait_for(first_child_of_tree_item_is_not_loading_condition(home_ti))
786+
(error_ti,) = home_ti.Children
787+
assert (
788+
'Cannot list links: URL revision body is missing. Recommend delete and redownload.' ==
789+
error_ti.Text
790+
)
791+
# (Wait for redownload to complete)
793792

794793
# Reexpand RootResourceNode and ensure the children are the same
795794
home_ti.Collapse()

src/crystal/tests/test_load_urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from crystal.tests.util.server import extracted_project, served_project
77
from crystal.tests.util.ssd import database_on_ssd
88
from crystal.tests.util.subtests import awith_subtests, SubtestsContext
9-
from crystal.tests.util.tasks import wait_for_download_task_to_start_and_finish, wait_for_download_to_start_and_finish
9+
from crystal.tests.util.tasks import wait_for_download_task_to_start_and_finish
1010
from crystal.tests.util.wait import (
1111
first_child_of_tree_item_is_not_loading_condition,
1212
tree_has_no_children_condition, wait_for,

src/crystal/tests/test_new_group.py

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from crystal.tests.util.controls import click_button, click_checkbox, TreeItem
77
from crystal.tests.util.server import MockHttpServer, served_project
88
from crystal.tests.util.ssd import database_on_ssd
9-
from crystal.tests.util.tasks import wait_for_download_task_to_start_and_finish, wait_for_download_to_start_and_finish
9+
from crystal.tests.util.tasks import wait_for_download_task_to_start_and_finish
1010
from crystal.tests.util.wait import (
1111
first_child_of_tree_item_is_not_loading_condition,
1212
tree_has_no_children_condition, tree_item_has_no_children_condition,
@@ -190,8 +190,8 @@ async def test_given_resource_node_with_multiple_link_children_matching_url_patt
190190
(home_ti,) = root_ti.Children
191191

192192
# Expand home URL
193-
home_ti.Expand()
194-
await wait_for_download_to_start_and_finish(mw.task_tree)
193+
async with wait_for_download_task_to_start_and_finish(project):
194+
home_ti.Expand()
195195
assert first_child_of_tree_item_is_not_loading_condition(home_ti)()
196196

197197
# Select a comic link from the home URL
@@ -326,7 +326,7 @@ async def test_given_node_is_selected_in_entity_tree_when_press_new_group_button
326326
comic_rgn = _find_child(root_ti, comic_pattern, url_prefix)
327327

328328
# Children of RootResourceNode (a kind of _ResourceNode) that is inside RootNode
329-
await _expand_node(home_rrn, mw, will_download=True)
329+
await _expand_node(home_rrn, project, will_download=True)
330330
home_rrn__atom_feed_rrn = _find_child(
331331
home_rrn, atom_feed_url, url_prefix)
332332
home_rrn__about_rrn = _find_child(
@@ -361,21 +361,21 @@ async def test_given_node_is_selected_in_entity_tree_when_press_new_group_button
361361
# ---
362362

363363
# Children of LinkedResourceNode (a kind of _ResourceNode) that is inside GroupedLinkedResourceNode
364-
await _expand_node(home_rrn__feed_glrn__rss_feed_lrn, mw, will_download=True)
364+
await _expand_node(home_rrn__feed_glrn__rss_feed_lrn, project, will_download=True)
365365
home_rrn__feed_glrn__rss_feed_lrn__home_rrn = _find_child(
366366
home_rrn__feed_glrn__rss_feed_lrn, home_url, url_prefix)
367367

368368
# Children of LinkedResourceNode (a kind of _ResourceNode) that is inside RootResourceNode
369369
# HACK: Relies on Crystal's "Not in Archive" page to provide a
370370
# link to the original URL (which is offsite)
371-
await _expand_node(home_rrn__archive_lrn, mw) # not in archive
371+
await _expand_node(home_rrn__archive_lrn, project) # not in archive
372372
home_rrn__archive_lrn__offsite_cn = _find_child_by_title(
373373
home_rrn__archive_lrn, 'Offsite')
374374

375375
# Children of LinkedResourceNode (a kind of _ResourceNode) that is inside ClusterNode
376376
# HACK: Relies on Crystal's "Not in Archive" page to provide a
377377
# link to the original URL (which is offsite)
378-
await _expand_node(home_rrn__offsite_cn__twitter_lrn, mw) # not in archive
378+
await _expand_node(home_rrn__offsite_cn__twitter_lrn, project) # not in archive
379379
home_rrn__offsite_cn__twitter_lrn__offsite_cn = _find_child_by_title(
380380
home_rrn__offsite_cn__twitter_lrn, 'Offsite')
381381
await _expand_node(home_rrn__embedded_cn__stylesheet_lrn) # already downloaded
@@ -430,15 +430,14 @@ async def test_given_node_is_selected_in_entity_tree_when_press_new_group_button
430430
assertEqual('Feed', await _source_name_for_node(feed_rgn__rss_feed_nrn__home_rrn, mw))
431431

432432

433-
async def _expand_node(node_ti: TreeItem, mw: MainWindow | None=None, *, will_download: bool=False) -> None:
434-
node_ti.Expand()
433+
async def _expand_node(node_ti: TreeItem, project: Project | None=None, *, will_download: bool=False) -> None:
435434
if will_download:
436-
if mw is None:
437-
raise ValueError('Need mw parameter when will_download=True')
438-
await wait_for_download_to_start_and_finish(
439-
mw.task_tree,
440-
immediate_finish_ok=True,
441-
stacklevel_extra=1)
435+
if project is None:
436+
raise ValueError('Need project parameter when will_download=True')
437+
async with wait_for_download_task_to_start_and_finish(project):
438+
node_ti.Expand()
439+
else:
440+
node_ti.Expand()
442441
await wait_for(
443442
first_child_of_tree_item_is_not_loading_condition(node_ti),
444443
timeout=3.0, # took 2.2s on Windows CI
@@ -501,8 +500,8 @@ async def test_given_urls_loaded_and_new_url_created_when_show_new_group_dialog_
501500
assert progress_listener_method.call_count >= 1
502501

503502
# Expand home URL, to discover initial comic URLs
504-
home_ti.Expand()
505-
await wait_for_download_to_start_and_finish(mw.task_tree)
503+
async with wait_for_download_task_to_start_and_finish(project):
504+
home_ti.Expand()
506505
assert first_child_of_tree_item_is_not_loading_condition(home_ti)()
507506
assert len(comic_ti.Children) >= 2 # contains at least the first and last comics
508507

@@ -532,8 +531,8 @@ async def test_when_add_group_then_does_not_download_immediately_by_default() ->
532531
with _served_simple_site_with_group() as (home_url, comic_pattern):
533532
async with (await OpenOrCreateDialog.wait_for()).create() as (mw, project):
534533
rr = RootResource(project, 'Home', Resource(project, home_url))
535-
rr.download()
536-
await wait_for_download_to_start_and_finish(mw.task_tree)
534+
async with wait_for_download_task_to_start_and_finish(project):
535+
rr.download()
537536

538537
assert mw.new_group_button.Enabled
539538
click_button(mw.new_group_button)
@@ -553,8 +552,8 @@ async def test_when_add_group_can_download_group_immediately_with_1_extra_click(
553552
with _served_simple_site_with_group() as (home_url, comic_pattern):
554553
async with (await OpenOrCreateDialog.wait_for()).create() as (mw, project):
555554
rr = RootResource(project, 'Home', Resource(project, home_url))
556-
rr.download()
557-
await wait_for_download_to_start_and_finish(mw.task_tree)
555+
async with wait_for_download_task_to_start_and_finish(project):
556+
rr.download()
558557

559558
assert mw.new_group_button.Enabled
560559
click_button(mw.new_group_button)
@@ -563,12 +562,12 @@ async def test_when_add_group_can_download_group_immediately_with_1_extra_click(
563562
ngd.pattern_field.Value = comic_pattern
564563
ngd.name_field.Value = 'Comic'
565564
click_checkbox(ngd.download_immediately_checkbox) # extra click #1
566-
await ngd.ok()
567-
568-
# Ensure started downloading
569-
root_ti = TreeItem.GetRootItem(mw.entity_tree.window)
570-
root_ti.find_child(comic_pattern, project.default_url_prefix)
571-
await wait_for_download_to_start_and_finish(mw.task_tree)
565+
async with wait_for_download_task_to_start_and_finish(project):
566+
await ngd.ok()
567+
568+
# Ensure started downloading
569+
root_ti = TreeItem.GetRootItem(mw.entity_tree.window)
570+
root_ti.find_child(comic_pattern, project.default_url_prefix)
572571

573572

574573
async def test_when_edit_group_then_new_group_options_not_shown() -> None:

src/crystal/tests/test_new_root_url.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
append_deferred_top_level_tasks,
1515
scheduler_disabled,
1616
wait_for_download_task_to_start_and_finish,
17-
wait_for_download_to_start_and_finish,
1817
)
1918
from crystal.tests.util.wait import (
2019
DEFAULT_WAIT_PERIOD, first_child_of_tree_item_is_not_loading_condition,
@@ -157,8 +156,8 @@ async def test_given_resource_node_with_links_can_create_new_root_url_to_label_l
157156
(home_ti,) = root_ti.Children
158157

159158
# Expand home URL
160-
home_ti.Expand()
161-
await wait_for_download_to_start_and_finish(mw.task_tree)
159+
async with wait_for_download_task_to_start_and_finish(project):
160+
home_ti.Expand()
162161
assert first_child_of_tree_item_is_not_loading_condition(home_ti)()
163162

164163
# Select the Atom Feed link from the home URL
@@ -225,12 +224,12 @@ async def test_when_add_url_then_downloads_url_immediately_by_default() -> None:
225224
assert nud.download_immediately_checkbox.Value
226225

227226
nud.url_field.Value = home_url
228-
await nud.ok()
229-
230-
# Ensure started downloading
231-
root_ti = TreeItem.GetRootItem(mw.entity_tree.window)
232-
home_ti = root_ti.find_child(home_url, project.default_url_prefix)
233-
await wait_for_download_to_start_and_finish(mw.task_tree)
227+
async with wait_for_download_task_to_start_and_finish(project):
228+
await nud.ok()
229+
230+
# Ensure started downloading
231+
root_ti = TreeItem.GetRootItem(mw.entity_tree.window)
232+
home_ti = root_ti.find_child(home_url, project.default_url_prefix)
234233
await _assert_tree_item_icon_tooltip_contains(home_ti, 'Fresh')
235234

236235

src/crystal/tests/test_readonly_mode.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from crystal.tests.util.server import MockHttpServer, served_project, extracted_project
1616
from crystal.tests.util.skip import skipTest
1717
from crystal.tests.util.subtests import awith_subtests, SubtestsContext
18-
from crystal.tests.util.tasks import scheduler_disabled, step_scheduler_until_done, wait_for_download_to_start_and_finish
18+
from crystal.tests.util.tasks import scheduler_disabled, step_scheduler_until_done, wait_for_download_task_to_start_and_finish
1919
from crystal.tests.util.wait import tree_has_no_children_condition, wait_for, first_child_of_tree_item_is_not_loading_condition
2020
from crystal.tests.util.windows import MainWindow, MenuitemDisabledError, OpenOrCreateDialog, NewGroupDialog, NewRootUrlDialog, PreferencesDialog
2121
from crystal.util.db import DatabaseCursor
@@ -241,8 +241,8 @@ async def test_when_readonly_project_is_saved_then_becomes_writable_and_all_unsa
241241

242242
root_ti = TreeItem.GetRootItem(mw.entity_tree.window)
243243
(home_ti,) = root_ti.Children
244-
home_ti.Expand()
245-
await wait_for_download_to_start_and_finish(mw.task_tree)
244+
async with wait_for_download_task_to_start_and_finish(project):
245+
home_ti.Expand()
246246

247247
# Reopen the project as read-only
248248
async with (await OpenOrCreateDialog.wait_for()).open(project_dirpath, readonly=True) as (mw, project):

0 commit comments

Comments
 (0)