Skip to content

Commit e5811a3

Browse files
authored
Wait for a window title to be written before replacing it in a test (#1824)
test_instance_titles_do_not_cross_between_concurrent_select_menus sets the title "B" while the second menu is running and then, once the first menu has finished, "B active". A title set during a prompt is written by a callback on the prompt's event loop, and a title set before that callback runs replaces it. Nothing made the second menu's loop run before "B active" was set, so under load "B" was never written and the test failed. Wait for the second menu's loop to get past the callback before going on.
1 parent fa81c19 commit e5811a3

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

‎tests/test_window_title.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,12 @@ def after_render(_sender):
210210
def test_instance_titles_do_not_cross_between_concurrent_select_menus(monkeypatch):
211211
streams = [TtyStream(), TtyStream()]
212212
rendered = [threading.Event(), threading.Event()]
213-
menus = []
213+
menus = {}
214214
run = Application.run
215215

216216
def run_menu(menu, *args, **kwargs):
217217
index = next(index for index, stream in enumerate(streams) if menu.output.stdout is stream)
218-
menus.append(menu)
218+
menus[index] = menu
219219
menu.after_render += lambda _sender: rendered[index].set()
220220
return run(menu, *args, set_exception_handler=False, **kwargs)
221221

@@ -231,12 +231,19 @@ def run_menu(menu, *args, **kwargs):
231231
assert all(event.wait(timeout=5) for event in rendered)
232232
apps[0].set_window_title("A")
233233
apps[1].set_window_title("B")
234+
235+
# The second menu's loop writes "B" in a callback. Wait for that loop to get past it, since a title
236+
# set before the callback runs replaces "B" instead of following it.
237+
title_written = threading.Event()
238+
menus[1].loop.call_soon_threadsafe(title_written.set)
239+
assert title_written.wait(timeout=5)
240+
234241
inputs[0].send_text("\r")
235242
assert results[0].result(timeout=5) == "selected"
236243
apps[0].set_window_title("A finished")
237244
apps[1].set_window_title("B active")
238245
finally:
239-
for menu in menus:
246+
for menu in menus.values():
240247
loop = menu.loop
241248
if loop is not None and loop.is_running():
242249
loop.call_soon_threadsafe(lambda menu=menu: menu.exit(result="selected"))

0 commit comments

Comments
 (0)