@@ -210,12 +210,12 @@ def after_render(_sender):
210210def 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