44"""BLE advertising tests for nrf5340bsim."""
55
66import logging
7- import time
87
98import pytest
109
3938print("adv run start")
4039adapter.start_advertising(advertisement, connectable=False)
4140print("adv running")
42- while True:
43- time.sleep(0.2)
41+ time.sleep(10)
42+ adapter.stop_advertising()
43+ print("adv run done")
4444"""
4545
4646
@@ -50,54 +50,34 @@ def test_bsim_advertise_and_scan(bsim_phy, circuitpython, zephyr_sample):
5050 """Advertise from CircuitPython and verify Zephyr observer sees traffic."""
5151 observer = zephyr_sample
5252
53- start_time = time .time ()
54- while time .time () - start_time < 12.0 :
55- observer_output = observer .serial .all_output
56- adv_ready = "adv started" in circuitpython .serial .all_output
57- if (
58- "Device found:" in observer_output
59- and "AD data len 10" in observer_output
60- and adv_ready
61- ):
62- break
63- time .sleep (0.05 )
53+ circuitpython .wait_until_done ()
6454
55+ cp_output = circuitpython .serial .all_output
6556 observer_output = observer .serial .all_output
66- assert "adv start" in circuitpython .serial .all_output
67- assert "adv started" in circuitpython .serial .all_output
57+ assert "adv start" in cp_output
58+ assert "adv started" in cp_output
59+ assert "adv stop" in cp_output
6860 assert "Device found:" in observer_output
6961 assert "AD data len 10" in observer_output
7062
7163
72- @pytest .mark .zephyr_sample ("bluetooth/observer" , timeout = 20.0 )
64+ @pytest .mark .zephyr_sample ("bluetooth/observer" )
65+ @pytest .mark .code_py_runs (2 )
66+ @pytest .mark .duration (25 )
7367@pytest .mark .circuitpy_drive ({"code.py" : BSIM_ADV_INTERRUPT_RELOAD_CODE })
7468def test_bsim_advertise_ctrl_c_reload (bsim_phy , circuitpython , zephyr_sample ):
7569 """Ensure advertising resumes after Ctrl-C and a reload."""
7670 observer = zephyr_sample
7771
78- start_time = time .time ()
79- sent_ctrl_c = False
80- sent_reload = False
81- observer_count_before = 0
82-
83- while time .time () - start_time < 22.0 :
84- cp_output = circuitpython .serial .all_output
85- observer_output = observer .serial .all_output
86- device_found_count = observer_output .count ("Device found:" )
87-
88- if not sent_ctrl_c and "adv running" in cp_output and device_found_count > 0 :
89- circuitpython .serial .write ("\x03 " )
90- sent_ctrl_c = True
91- observer_count_before = device_found_count
92-
93- if sent_ctrl_c and not sent_reload and "KeyboardInterrupt" in cp_output :
94- circuitpython .serial .write ("\x04 " )
95- sent_reload = True
72+ circuitpython .serial .wait_for ("adv running" )
73+ observer .serial .wait_for ("Device found:" )
74+ observer_count_before = observer .serial .all_output .count ("Device found:" )
9675
97- if sent_reload and cp_output . count ( "adv running" ) >= 2 :
98- break
76+ circuitpython . serial . write ( " \x03 " )
77+ circuitpython . serial . wait_for ( "KeyboardInterrupt" )
9978
100- time .sleep (0.05 )
79+ circuitpython .serial .write ("\x04 " )
80+ circuitpython .wait_until_done ()
10181
10282 cp_output = circuitpython .serial .all_output
10383 observer_output = observer .serial .all_output
@@ -107,5 +87,6 @@ def test_bsim_advertise_ctrl_c_reload(bsim_phy, circuitpython, zephyr_sample):
10787 assert "adv run start" in cp_output
10888 assert "KeyboardInterrupt" in cp_output
10989 assert cp_output .count ("adv running" ) >= 2
110- assert observer_output .count ("Device found:" ) >= observer_count_before
90+ assert cp_output .count ("adv run done" ) >= 1
91+ assert observer_output .count ("Device found:" ) >= observer_count_before + 1
11192 assert "Already advertising" not in cp_output
0 commit comments