1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3""" 4Copyright (c) 2024 Huawei Device Co., Ltd. 5Licensed under the Apache License, Version 2.0 (the "License"); 6you may not use this file except in compliance with the License. 7You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11Unless required by applicable law or agreed to in writing, software 12distributed under the License is distributed on an "AS IS" BASIS, 13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14See the License for the specific language governing permissions and 15limitations under the License. 16 17Description: Scenario test case. 18""" 19 20import logging 21import os 22import time 23 24import pytest 25 26from aw import Application 27from aw import Utils 28from aw import debugger 29from aw.api import debugger_api, runtime_api 30 31 32@pytest.mark.debug 33@pytest.mark.timeout(60) 34class TestWorkerMixedDebug: 35 """ 36 测试用例:多实例 debug 混合调试 37 测试步骤: 38 1. 连接 connect server 和主线程 debugger server 39 2. 主线程使能 Runtime 和 Debugger 40 3. 主线程使能混合调试(Debugger.setMixedDebugEnabled) 41 4. 触发点击事件,创建子线程,连接子线程 debugger server 42 5. 子线程使能 Runtime 和 Debugger 43 6. 子线程使能混合调试(Debugger.setMixedDebugEnabled) 44 7. 子线程 Worker.ets 文件设置断点(Debugger.getPossibleAndSetBreakpointByUrl) 45 8. 子线程 resume,停在断点处(Debugger.resume) 46 9. 子线程 stepInto,进入 native 调试(Debugger.stepInto) 47 7. 子线程收到 native 返回的消息后应答,停在下一断点处(Debugger.replyNativeCalling) 48 8. 子线程 stepOver,停在下一行(Debugger.stepOver) 49 9. 子线程 resume(Debugger.resume) 50 10. 所有线程去使能 debugger(Debugger.disable) 51 10. 关闭所有线程 debugger server 和 connect server 连接 52 关键代码: 53 Index.ets 54 .OnClick(() => { 55 let myWorker = new worker.ThreadWorker("entry/ets/workers/Worker.ets") 56 myWorker.onmessage = (e: MessageEvents) => {} 57 myWorker.postMessage("message from the main thread") 58 }) 59 Worker.ets 60 const workerPort: ThreadWorkerGlobalScope = worker.workerPort; 61 workerPort.onmessage = (e: MessageEvents) => { 62 let num = testNapi.add(1, 2) 63 let ans = testNapi.add(num, 3) 64 workerPort.postMessage("message from worker thread") 65 } 66 """ 67 68 def setup_method(self): 69 logging.info('Start running TestWorkerMixedDebug: setup') 70 71 self.log_path = rf'{os.path.dirname(__file__)}\..\log' 72 self.hilog_file_name = 'test_worker_mixed_debug.hilog.txt' 73 self.id_generator = Utils.message_id_generator() 74 75 # receive the hilog before the test start 76 Utils.clear_fault_log() 77 self.hilog_process, self.write_thread = Utils.save_hilog(log_path=self.log_path, 78 file_name=self.hilog_file_name, 79 debug_on=True) 80 81 def teardown_method(self): 82 Application.uninstall(self.config['bundle_name']) 83 84 # terminate the hilog receive process after the test done 85 time.sleep(3) 86 self.hilog_process.stdout.close() 87 self.hilog_process.terminate() 88 self.hilog_process.wait() 89 self.write_thread.join() 90 91 Utils.save_fault_log(log_path=self.log_path) 92 logging.info('TestWorkerMixedDebug done') 93 94 def test(self, test_suite_native_02_debug): 95 logging.info('Start running TestWorkerMixedDebug: test') 96 self.config = test_suite_native_02_debug 97 websocket = self.config['websocket'] 98 taskpool = self.config['taskpool'] 99 pid = self.config['pid'] 100 self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) 101 self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) 102 103 taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) 104 taskpool.await_taskpool() 105 taskpool.task_join() 106 if taskpool.task_exception: 107 raise taskpool.task_exception 108 109 async def procedure(self, websocket): 110 ################################################################################################################ 111 # main thread: connect the debugger server 112 ################################################################################################################ 113 main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) 114 logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') 115 ################################################################################################################ 116 # main thread: Runtime.enable 117 ################################################################################################################ 118 await self.runtime_impl.send("Runtime.enable", main_thread) 119 ################################################################################################################ 120 # main thread: Debugger.enable 121 ################################################################################################################ 122 await self.debugger_impl.send("Debugger.enable", main_thread) 123 ################################################################################################################ 124 # main thread: Debugger.setMixedDebugEnabled 125 ################################################################################################################ 126 params = debugger.SetMixedDebugEnabledParams(enabled=True, mixed_stack_enabled=False) 127 await self.debugger_impl.send("Debugger.setMixedDebugEnabled", main_thread, params) 128 ################################################################################################################ 129 # main thread: Runtime.runIfWaitingForDebugger 130 ################################################################################################################ 131 await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", main_thread) 132 ################################################################################################################ 133 # main thread: Debugger.scriptParsed 134 ################################################################################################################ 135 response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) 136 assert response['params']['url'] == self.config['file_path']['entry_ability'] 137 assert response['params']['endLine'] == 0 138 ################################################################################################################ 139 # main thread: Debugger.paused 140 ################################################################################################################ 141 response = await self.debugger_impl.recv("Debugger.paused", main_thread) 142 assert response['params']['callFrames'][0]['url'] == self.config['file_path']['entry_ability'] 143 assert response['params']['reason'] == 'Break on start' 144 ################################################################################################################ 145 # main thread: Debugger.resume 146 ################################################################################################################ 147 await self.debugger_impl.send("Debugger.resume", main_thread) 148 ################################################################################################################ 149 # main thread: Debugger.scriptParsed 150 ################################################################################################################ 151 response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) 152 assert response['params']['url'] == self.config['file_path']['index'] 153 assert response['params']['endLine'] == 0 154 ################################################################################################################ 155 # main thread: Debugger.paused 156 ################################################################################################################ 157 response = await self.debugger_impl.recv("Debugger.paused", main_thread) 158 assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] 159 assert response['params']['reason'] == 'Break on start' 160 ################################################################################################################ 161 # main thread: Debugger.resume 162 ################################################################################################################ 163 await self.debugger_impl.send("Debugger.resume", main_thread) 164 ################################################################################################################ 165 # main thread: click on the screen 166 ################################################################################################################ 167 Application.click_on_middle() 168 ################################################################################################################ 169 # worker thread: connect the debugger server 170 ################################################################################################################ 171 worker_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) 172 logging.info(f'Connect to the debugger server of instance: {worker_thread.instance_id}') 173 ################################################################################################################ 174 # worker thread: Runtime.enable 175 ################################################################################################################ 176 await self.runtime_impl.send("Runtime.enable", worker_thread) 177 ################################################################################################################ 178 # worker thread: Debugger.enable 179 ################################################################################################################ 180 await self.debugger_impl.send("Debugger.enable", worker_thread) 181 ################################################################################################################ 182 # worker thread: Debugger.setMixedDebugEnabled 183 ################################################################################################################ 184 params = debugger.SetMixedDebugEnabledParams(enabled=True, mixed_stack_enabled=False) 185 await self.debugger_impl.send("Debugger.setMixedDebugEnabled", worker_thread, params) 186 ################################################################################################################ 187 # worker thread: Runtime.runIfWaitingForDebugger 188 ################################################################################################################ 189 await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", worker_thread) 190 ################################################################################################################ 191 # worker thread: Debugger.scriptParsed 192 ################################################################################################################ 193 response = await self.debugger_impl.recv("Debugger.scriptParsed", worker_thread) 194 assert response['params']['url'] == self.config['file_path']['worker'] 195 assert response['params']['endLine'] == 0 196 ################################################################################################################ 197 # worker thread: Debugger.paused 198 ################################################################################################################ 199 response = await self.debugger_impl.recv("Debugger.paused", worker_thread) 200 assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] 201 assert response['params']['reason'] == 'Break on start' 202 ################################################################################################################ 203 # worker thread: Debugger.removeBreakpointsByUrl 204 ################################################################################################################ 205 params = debugger.RemoveBreakpointsUrl(self.config['file_path']['worker']) 206 await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", worker_thread, params) 207 ################################################################################################################ 208 # worker thread: Debugger.getPossibleAndSetBreakpointByUrl 209 ################################################################################################################ 210 locations = [debugger.BreakLocationUrl(url=self.config['file_path']['worker'], line_number=12), 211 debugger.BreakLocationUrl(url=self.config['file_path']['worker'], line_number=13)] 212 params = debugger.SetBreakpointsLocations(locations) 213 response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", 214 worker_thread, params) 215 assert response['result']['locations'][0]['id'] == 'id:12:0:' + self.config['file_path']['worker'] 216 assert response['result']['locations'][1]['id'] == 'id:13:0:' + self.config['file_path']['worker'] 217 ################################################################################################################ 218 # worker thread: Debugger.resume 219 ################################################################################################################ 220 await self.debugger_impl.send("Debugger.resume", worker_thread) 221 # worker thread: Debugger.paused 222 response = await self.debugger_impl.recv("Debugger.paused", worker_thread) 223 assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] 224 assert response['params']['reason'] == 'other' 225 assert response['params']['hitBreakpoints'] == ['id:12:14:' + self.config['file_path']['worker']] 226 ################################################################################################################ 227 # worker thread: Debugger.stepInto 228 ################################################################################################################ 229 await self.debugger_impl.send("Debugger.stepInto", worker_thread) 230 ################################################################################################################ 231 # worker thread: Debugger.nativeCalling 232 ################################################################################################################ 233 response = await self.debugger_impl.recv("Debugger.nativeCalling", worker_thread) 234 assert response['params']['isStepInto'] is True 235 ################################################################################################################ 236 # worker thread: Debugger.replyNativeCalling 237 ################################################################################################################ 238 params = debugger.ReplyNativeCallingParams() 239 await self.debugger_impl.send("Debugger.replyNativeCalling", worker_thread, params) 240 # worker thread: Debugger.paused 241 response = await self.debugger_impl.recv("Debugger.paused", worker_thread) 242 assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] 243 assert response['params']['hitBreakpoints'] == ['id:13:14:' + self.config['file_path']['worker']] 244 ################################################################################################################ 245 # worker thread: Debugger.stepOver 246 ################################################################################################################ 247 await self.debugger_impl.send("Debugger.stepOver", worker_thread) 248 # worker thread: Debugger.paused 249 response = await self.debugger_impl.recv("Debugger.paused", worker_thread) 250 assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] 251 assert response['params']['hitBreakpoints'] == [] 252 ################################################################################################################ 253 # worker thread: Debugger.disable 254 ################################################################################################################ 255 await self.debugger_impl.send("Debugger.disable", worker_thread) 256 ################################################################################################################ 257 # main thread: Debugger.disable 258 ################################################################################################################ 259 await self.debugger_impl.send("Debugger.disable", main_thread) 260 ################################################################################################################ 261 # close the websocket connections 262 ################################################################################################################ 263 await websocket.send_msg_to_debugger_server(worker_thread.instance_id, worker_thread.send_msg_queue, 'close') 264 await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') 265 await websocket.send_msg_to_connect_server('close') 266 ################################################################################################################