Skip to content

Commit d1bf948

Browse files
Testing with the timeout error logic.
1 parent ae32172 commit d1bf948

File tree

1 file changed

+73
-32
lines changed

1 file changed

+73
-32
lines changed

scripts/tests/run_tv_casting_test.py

+73-32
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
import pdb # SHAO remove when done testing!
1718
import logging
1819
import os
1920
import subprocess
@@ -31,14 +32,18 @@
3132
TV_APP_MAX_START_WAIT_SEC = 2
3233

3334
# The maximum amount of time to commission the Linux tv-casting-app and the tv-app before timeout.
34-
COMMISSIONING_STAGE_MAX_WAIT_SEC = 10
35+
COMMISSIONING_STAGE_MAX_WAIT_SEC = 15
3536

3637
# The maximum amount of time to test that the launchURL is sent from the Linux tv-casting-app and received on the tv-app before timeout.
3738
TEST_LAUNCHURL_MAX_WAIT_SEC = 10
3839

3940
# The maximum amount of time to verify the subscription state in the Linux tv-casting-app output before timeout.
4041
VERIFY_SUBSCRIPTION_STATE_MAX_WAIT_SEC = 10
4142

43+
# Start the clock for this function
44+
# START_TIME = 0 # SHAO remove when done testing!
45+
START_TIME = time.time()
46+
4247
# File names of logs for the Linux tv-casting-app and the Linux tv-app.
4348
LINUX_TV_APP_LOGS = 'Linux-tv-app-logs.txt'
4449
LINUX_TV_CASTING_APP_LOGS = 'Linux-tv-casting-app-logs.txt'
@@ -88,6 +93,8 @@ def dump_temporary_logs_to_console(log_file_path: str):
8893
def handle_casting_failure(casting_state: str, log_file_paths: List[str]):
8994
"""Log '{casting_state} failed!' as error, dump log files to console, exit on error."""
9095
logging.error(casting_state + ' failed!')
96+
pdb.set_trace()
97+
print('SHAO remove when done testing!!!')
9198

9299
for log_file_path in log_file_paths:
93100
try:
@@ -99,45 +106,53 @@ def handle_casting_failure(casting_state: str, log_file_paths: List[str]):
99106

100107

101108
# def extract_value_from_string(line: str) -> str: # SHAO OG
102-
def extract_value_from_string(line: str, casting_state: str, log_paths) -> str:
109+
def extract_value_from_string(line: str, value_name: str, casting_state: str, log_paths) -> str:
103110
"""Extract and return value from given input string.
104111
105112
The string is expected to be in the following format as it is received
106113
from the Linux tv-casting-app output:
107114
\x1b[0;34m[1715206773402] [20056:2842184] [DMG] Cluster = 0x506,\x1b[0m
108115
The substring to be extracted here is '0x506'.
109116
Or:
117+
\x1b[0;32m[1714582264602] [77989:2286038] [SVR] Discovered Commissioner #0\x1b[0m
118+
The value '0' will be extracted from the string.
119+
Or:
110120
\x1b[0;34m[1713741926895] [7276:9521344] [DIS] Vendor ID: 65521\x1b[0m
111121
The integer value to be extracted here is 65521.
112122
Or:
113123
\x1b[0;34m[1714583616179] [7029:2386956] [SVR] device Name: Test TV casting app\x1b[0m
114124
The substring to be extracted here is 'Test TV casting app'.
115125
"""
116-
# if '=' in line:
117-
# value = line.split('=')[-1].strip().replace(',\x1b[0m', '')
118-
# else:
119-
# value = line.split(':')[-1].strip().replace('\x1b[0m', '')
126+
if '=' in line:
127+
value = line.split('=')[-1].strip().replace(',\x1b[0m', '')
128+
else:
129+
value = line.split(':')[-1].strip().replace('\x1b[0m', '')
120130

121-
# return value
131+
return value
122132

123-
if ':' in line:
124-
if '=' in line:
125-
delimiter = '='
126-
else:
127-
delimiter = ':'
133+
# SHAO working
134+
# if ':' in line:
135+
# if '=' in line:
136+
# delimiter = '='
137+
# elif '#' in line:
138+
# delimiter = '#'
139+
# else:
140+
# delimiter = ':'
128141

129-
value = line.split(delimiter)[-1].strip().replace('\x1b[0m', '').rstrip(',')
130-
else:
131-
logging.error('Could not extract the value from the following line: %s', line)
132-
handle_casting_failure(casting_state, log_paths)
142+
# value = line.split(delimiter)[-1].strip().replace('\x1b[0m', '').rstrip(',') # SHAO OG working
143+
# # value = line.split(delimiter)[-1].strip().replace('\x1b[0m', '') # SHAO forcing timeout!
133144

134-
return value
145+
# else:
146+
# logging.error(f'Could not extract {value_name} from the following line: {line}')
147+
# handle_casting_failure(casting_state, log_paths)
148+
149+
# return value
135150

136151

137152
def validate_value(casting_state: str, expected_value: Union[str, int], log_paths: List[str], line: str, value_name: str) -> Optional[str]:
138153
"""Validate a value in a string against an expected value during a given casting state."""
139154
# value = extract_value_from_string(line) # SHAO OG
140-
value = extract_value_from_string(line, casting_state, log_paths)
155+
value = extract_value_from_string(line, value_name, casting_state, log_paths)
141156

142157
if isinstance(expected_value, int):
143158
value = int(value)
@@ -157,8 +172,10 @@ def start_up_tv_app_success(tv_app_process: subprocess.Popen, linux_tv_app_log_f
157172
start_wait_time = time.time()
158173

159174
while True:
175+
current_time = time.time()
160176
# Check if the time elapsed since the start wait time exceeds the maximum allowed startup time for the TV app.
161-
if time.time() - start_wait_time > TV_APP_MAX_START_WAIT_SEC:
177+
# if time.time() - start_wait_time > TV_APP_MAX_START_WAIT_SEC: # SHAO OG
178+
if current_time - start_wait_time > TV_APP_MAX_START_WAIT_SEC or current_time - START_TIME > 30: # SHAO remove when done
162179
logging.error('The Linux tv-app process did not start successfully within the timeout.')
163180
return False
164181

@@ -180,8 +197,10 @@ def initiate_cast_request_success(tv_casting_app_info: Tuple[subprocess.Popen, T
180197
start_wait_time = time.time()
181198

182199
while True:
200+
current_time = time.time()
183201
# Check if we exceeded the maximum wait time for initiating 'cast request' from the Linux tv-casting-app to the Linux tv-app.
184-
if time.time() - start_wait_time > COMMISSIONING_STAGE_MAX_WAIT_SEC:
202+
# if time.time() - start_wait_time > COMMISSIONING_STAGE_MAX_WAIT_SEC: # SHAO OG
203+
if current_time - start_wait_time > COMMISSIONING_STAGE_MAX_WAIT_SEC or current_time - START_TIME > 30: # SHAO remove when done
185204
logging.error('The command `cast request ' + valid_discovered_commissioner_number +
186205
'` was not issued to the Linux tv-casting-app process within the timeout.')
187206
return False
@@ -217,14 +236,14 @@ def extract_device_info_from_tv_casting_app(tv_casting_app_info: Tuple[subproces
217236

218237
if 'device Name' in line:
219238
# device_name = extract_value_from_string(line) # SHAO OG
220-
device_name = extract_value_from_string(line, casting_state, log_paths)
239+
device_name = extract_value_from_string(line, '`device Name`', casting_state, log_paths)
221240
elif 'vendor id' in line:
222241
# vendor_id = extract_value_from_string(line) # SHAO OG
223-
vendor_id = extract_value_from_string(line, casting_state, log_paths)
242+
vendor_id = extract_value_from_string(line, '`vendor id`', casting_state, log_paths)
224243
vendor_id = int(vendor_id)
225244
elif 'product id' in line:
226245
# product_id = extract_value_from_string(line) # SHAO OG
227-
product_id = extract_value_from_string(line, casting_state, log_paths)
246+
product_id = extract_value_from_string(line, '`product id`', casting_state, log_paths)
228247
product_id = int(product_id)
229248

230249
if device_name and vendor_id and product_id:
@@ -241,8 +260,11 @@ def validate_identification_declaration_message_on_tv_app(tv_app_info: Tuple[sub
241260
start_wait_time = time.time()
242261

243262
while True:
263+
current_time = time.time() # SHAO remove when done
244264
# Check if we exceeded the maximum wait time for validating the device information from the Linux tv-app to the corresponding values from the Linux tv-app.
245-
if time.time() - start_wait_time > COMMISSIONING_STAGE_MAX_WAIT_SEC:
265+
# if time.time() - start_wait_time > COMMISSIONING_STAGE_MAX_WAIT_SEC: # SHAO OG
266+
if current_time - start_wait_time > COMMISSIONING_STAGE_MAX_WAIT_SEC or current_time - START_TIME > 30: # SHAO remove when done
267+
# print(f'current_elapsed_time: {current_elapsed_time}, elapsed_time: {elapsed_time}, current_time: {current_time}, START_TIME: {START_TIME}') # SHAO remove when done testing
246268
logging.error(
247269
'The device information from the Linux tv-app output was not validated against the corresponding values from the Linux tv-casting-app output within the timeout.')
248270
return False
@@ -277,8 +299,10 @@ def validate_tv_casting_request_approval(tv_app_info: Tuple[subprocess.Popen, Te
277299
start_wait_time = time.time()
278300

279301
while True:
302+
current_time = time.time() # SHAO remove when done
280303
# Check if we exceeded the maximum wait time for sending 'controller ux ok' from the Linux tv-app to the Linux tv-casting-app.
281-
if time.time() - start_wait_time > COMMISSIONING_STAGE_MAX_WAIT_SEC:
304+
# if time.time() - start_wait_time > COMMISSIONING_STAGE_MAX_WAIT_SEC: # SHAO OG
305+
if current_time - start_wait_time > COMMISSIONING_STAGE_MAX_WAIT_SEC or current_time - START_TIME > 30: # SHAO remove when done
282306
logging.error('The cast request from the Linux tv-casting-app to the Linux tv-app was not approved within the timeout.')
283307
return False
284308

@@ -312,8 +336,10 @@ def validate_commissioning_success(tv_casting_app_info: Tuple[subprocess.Popen,
312336
start_wait_time = time.time()
313337

314338
while True:
339+
current_time = time.time() # SHAO remove when done
315340
# Check if we exceeded the maximum wait time for validating commissioning success between the Linux tv-casting-app and the Linux tv-app.
316-
if time.time() - start_wait_time > COMMISSIONING_STAGE_MAX_WAIT_SEC:
341+
if time.time() - start_wait_time > COMMISSIONING_STAGE_MAX_WAIT_SEC: # SHAO OG
342+
317343
logging.error(
318344
'The commissioning between the Linux tv-casting-app process and the Linux tv-app process did not complete successfully within the timeout.')
319345
return False
@@ -353,8 +379,10 @@ def parse_tv_casting_app_for_report_data_msg(tv_casting_app_info: Tuple[subproce
353379
start_wait_time = time.time()
354380

355381
while True:
382+
current_time = time.time()
356383
# Check if we exceeded the maximum wait time to parse the Linux tv-casting-app output for `ReportDataMessage` block.
357-
if time.time() - start_wait_time > VERIFY_SUBSCRIPTION_STATE_MAX_WAIT_SEC:
384+
# if time.time() - start_wait_time > VERIFY_SUBSCRIPTION_STATE_MAX_WAIT_SEC: # SHAO OG
385+
if current_time - start_wait_time > VERIFY_SUBSCRIPTION_STATE_MAX_WAIT_SEC or current_time - START_TIME > 30: # SHAO remove when done
358386
logging.error(
359387
'The relevant `ReportDataMessage` block was not found in the Linux tv-casting-app process within the timeout.')
360388
report_data_message.clear()
@@ -374,14 +402,14 @@ def parse_tv_casting_app_for_report_data_msg(tv_casting_app_info: Tuple[subproce
374402

375403
if 'Cluster =' in tv_casting_line:
376404
# cluster_value = extract_value_from_string(tv_casting_line) # SHAO OG
377-
cluster_value = extract_value_from_string(tv_casting_line, 'Testing subscription', log_paths)
405+
cluster_value = extract_value_from_string(tv_casting_line, '`Cluster ID`', 'Testing subscription', log_paths)
378406
if cluster_value != CLUSTER_MEDIA_PLAYBACK:
379407
report_data_message.clear()
380408
continue_parsing = False
381409

382410
elif 'Attribute =' in tv_casting_line:
383411
# attribute_value = extract_value_from_string(tv_casting_line) # SHAO OG
384-
attribute_value = extract_value_from_string(tv_casting_line, 'Testing subscription', log_paths)
412+
attribute_value = extract_value_from_string(tv_casting_line, '`Attribute ID`', 'Testing subscription', log_paths)
385413
if attribute_value != ATTRIBUTE_CURRENT_PLAYBACK_STATE:
386414
report_data_message.clear()
387415
continue_parsing = False
@@ -403,8 +431,10 @@ def parse_tv_app_output_for_launchUrl_msg_success(tv_app_info: Tuple[subprocess.
403431
start_wait_time = time.time()
404432

405433
while True:
434+
current_time = time.time() # SHAO remove when done
406435
# Check if we exceeded the maximum wait time to parse the Linux tv-app output for the string related to the launchUrl.
407-
if time.time() - start_wait_time > COMMISSIONING_STAGE_MAX_WAIT_SEC:
436+
# if time.time() - start_wait_time > COMMISSIONING_STAGE_MAX_WAIT_SEC: # SHAO OG
437+
if current_time - start_wait_time > COMMISSIONING_STAGE_MAX_WAIT_SEC or current_time - START_TIME > 30: # SHAO remove when done
408438
logging.error(
409439
'The relevant launchUrl string was not found in the Linux tv-app process within the timeout.')
410440
return False
@@ -431,8 +461,10 @@ def parse_tv_casting_app_output_for_launchUrl_msg_success(tv_casting_app_info: T
431461
start_wait_time = time.time()
432462

433463
while True:
464+
current_time = time.time()
434465
# Check if we exceeded the maximum wait time to parse the Linux tv-casting-app output for strings related to the launchUrl.
435-
if time.time() - start_wait_time > TEST_LAUNCHURL_MAX_WAIT_SEC:
466+
# if time.time() - start_wait_time > TEST_LAUNCHURL_MAX_WAIT_SEC: # SHAO OG
467+
if current_time - start_wait_time > TEST_LAUNCHURL_MAX_WAIT_SEC or current_time - START_TIME > 30: # SHAO remove when done
436468
logging.error(
437469
'The relevant launchUrl strings were not found in the Linux tv-casting-app process within the timeout.')
438470
return False
@@ -479,6 +511,11 @@ def test_discovery_fn(tv_casting_app_info: Tuple[subprocess.Popen, TextIO], log_
479511
linux_tv_casting_app_log_file.write(line)
480512
linux_tv_casting_app_log_file.flush()
481513

514+
# SHAO remove when done
515+
if time.time() - START_TIME > 30:
516+
logging.error('Could not find a valid discovered commissioner within timeout.')
517+
handle_casting_failure('Discovery', log_paths)
518+
482519
# Fail fast if "No commissioner discovered" string found.
483520
if "No commissioner discovered" in line:
484521
logging.error(line.rstrip('\n'))
@@ -570,6 +607,9 @@ def test_casting_fn(tv_app_rel_path, tv_casting_app_rel_path):
570607
For example: python3 run_tv_casting_test.py --tv-app-rel-path=path/to/tv-app
571608
--tv-casting-app-rel-path=path/to/tv-casting-app
572609
"""
610+
# START_TIME = time.time() # SHAO added
611+
# print(f'START_TIME set at beginning: {START_TIME}')
612+
573613
# Store the log files to a temporary directory.
574614
with tempfile.TemporaryDirectory() as temp_dir:
575615
linux_tv_app_log_path = os.path.join(temp_dir, LINUX_TV_APP_LOGS)
@@ -604,7 +644,8 @@ def test_casting_fn(tv_app_rel_path, tv_casting_app_rel_path):
604644
# We need the valid discovered commissioner number to continue with commissioning.
605645
# Example string: \x1b[0;32m[1714582264602] [77989:2286038] [SVR] Discovered Commissioner #0\x1b[0m
606646
# The value '0' will be extracted from the string.
607-
valid_discovered_commissioner_number = valid_discovered_commissioner.split('#')[-1].replace('\x1b[0m', '')
647+
valid_discovered_commissioner_number = valid_discovered_commissioner.split('#')[-1].replace('\x1b[0m', '') # SHAO OG
648+
# valid_discovered_commissioner_number = extract_value_from_string(valid_discovered_commissioner, '`Discovered Commissioner Number`', 'Discovery', log_paths)
608649

609650
test_commissioning_fn(valid_discovered_commissioner_number, tv_casting_app_info, tv_app_info, log_paths)
610651
test_subscription_fn(tv_casting_app_info, log_paths)

0 commit comments

Comments
 (0)