diff --git a/test/autotest/aw/__init__.py b/test/autotest/aw/__init__.py index 50005d3837a7d825fbbf51da0ade8abd2a31fb09..a8f80748600f12b82dc69637a245d712c1bccdba 100644 --- a/test/autotest/aw/__init__.py +++ b/test/autotest/aw/__init__.py @@ -24,7 +24,7 @@ from aw.taskpool import TaskPool from aw.cdp import debugger from aw.cdp import runtime from aw.cdp import heap_profiler -from aw.cdp import cpu_profiler +from aw.cdp import profiler communicate_with_debugger_server = Utils.communicate_with_debugger_server diff --git a/test/autotest/aw/api/debugger_api.py b/test/autotest/aw/api/debugger_api.py index d22cd0192cbc23aace95037221e2fe3a57624f94..6f95a441ee25a81cabcf289b69960315196b2567 100644 --- a/test/autotest/aw/api/debugger_api.py +++ b/test/autotest/aw/api/debugger_api.py @@ -19,16 +19,17 @@ Description: Python Debugger Domain Interfaces import json import logging -from http.client import responses from aw import communicate_with_debugger_server, Utils from aw.cdp import debugger from aw.types import ThreadConnectionInfo, ProtocolType +from aw.api.protocol_api import ProtocolImpl -class DebuggerImpl(object): +class DebuggerImpl(ProtocolImpl): - def __init__(self, id_generator): + def __init__(self, id_generator, websocket): + super().__init__(id_generator, websocket) self.dispatch_table = {"enable": (self.enable, ProtocolType.send), "disable": (self.disable, ProtocolType.send), "resume": (self.resume, ProtocolType.send), @@ -37,180 +38,217 @@ class DebuggerImpl(object): ProtocolType.send), "stepOver": (self.step_over, ProtocolType.send), "stepInto": (self.step_into, ProtocolType.send), + "stepOut": (self.step_out, ProtocolType.send), + "setPauseOnExceptions": (self.set_pause_on_exceptions, ProtocolType.send), + "evaluateOnCallFrame": (self.evaluate_on_call_frame, ProtocolType.send), + "smartStepInto": (self.smart_step_into, ProtocolType.send), + "setMixedDebugEnabled": (self.set_mixed_debug_enabled, ProtocolType.send), + "replyNativeCalling": (self.reply_native_calling, ProtocolType.send), + "dropFrame": (self.drop_frame, ProtocolType.send), "scriptParsed": (self.script_parsed, ProtocolType.recv), "paused": (self.paused, ProtocolType.recv), - "stepOut": (self.step_out, ProtocolType.send)} - self.id_generator = id_generator + "nativeCalling": (self.native_calling, ProtocolType.recv)} - @classmethod - async def connect_to_debugger_server(cls, websocket, pid, is_main=True): + async def connect_to_debugger_server(self, pid, is_main=True): if is_main: send_msg = {"type": "connected"} - await websocket.send_msg_to_connect_server(send_msg) - response = await websocket.recv_msg_of_connect_server() + await self.websocket.send_msg_to_connect_server(send_msg) + response = await self.websocket.recv_msg_of_connect_server() response = json.loads(response) assert response['type'] == 'addInstance' assert response['instanceId'] == 0, logging.error('InstanceId of the main thread is not 0') assert response['tid'] == pid else: - response = await websocket.recv_msg_of_connect_server() + response = await self.websocket.recv_msg_of_connect_server() response = json.loads(response) assert response['type'] == 'addInstance' assert response['instanceId'] != 0 assert response['tid'] != pid assert 'workerThread_' in response['name'] - instance_id = await websocket.get_instance() - send_queue = websocket.to_send_msg_queues[instance_id] - recv_queue = websocket.received_msg_queues[instance_id] + instance_id = await self.websocket.get_instance() + send_queue = self.websocket.to_send_msg_queues[instance_id] + recv_queue = self.websocket.received_msg_queues[instance_id] connection = ThreadConnectionInfo(instance_id, send_queue, recv_queue) return connection - @classmethod - async def destroy_instance(cls, websocket): - response = await websocket.recv_msg_of_connect_server() + async def destroy_instance(self): + response = await self.websocket.recv_msg_of_connect_server() response = json.loads(response) + assert response['type'] == 'destroyInstance' return response - @classmethod - async def enable(cls, message_id, connection, websocket, params=None): + async def enable(self, message_id, connection, params): response = await communicate_with_debugger_server(connection.instance_id, connection.send_msg_queue, connection.received_msg_queue, debugger.enable(), message_id) while response.startswith('{"method":"Debugger.scriptParsed"'): - response = await websocket.recv_msg_of_debugger_server(connection.instance_id, - connection.received_msg_queue) + response = await self.websocket.recv_msg_of_debugger_server(connection.instance_id, + connection.received_msg_queue) response = json.loads(response) assert response == {"id": message_id, "result": {"debuggerId": "0", "protocols": Utils.get_custom_protocols()}} - @classmethod - async def disable(cls, message_id, connection, websocket, params=None): + async def disable(self, message_id, connection, params): response = await communicate_with_debugger_server(connection.instance_id, connection.send_msg_queue, connection.received_msg_queue, debugger.disable(), message_id) assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(connection.instance_id, - connection.received_msg_queue) + response = await self.websocket.recv_msg_of_debugger_server(connection.instance_id, + connection.received_msg_queue) response = json.loads(response) assert response == {"id": message_id, "result": {}} return response - @classmethod - async def script_parsed(cls, connection, websocket, params=None): - response = await websocket.recv_msg_of_debugger_server(connection.instance_id, - connection.received_msg_queue) + async def script_parsed(self, connection, params): + response = await self.websocket.recv_msg_of_debugger_server(connection.instance_id, + connection.received_msg_queue) response = json.loads(response) return response - @classmethod - async def paused(cls, connection, websocket, params=None): - response = await websocket.recv_msg_of_debugger_server(connection.instance_id, - connection.received_msg_queue) + async def paused(self, connection, params): + response = await self.websocket.recv_msg_of_debugger_server(connection.instance_id, + connection.received_msg_queue) response = json.loads(response) assert response['method'] == 'Debugger.paused' return response - @classmethod - async def resume(cls, message_id, connection, websocket, params=None): + async def resume(self, message_id, connection, params): response = await communicate_with_debugger_server(connection.instance_id, connection.send_msg_queue, connection.received_msg_queue, debugger.resume(), message_id) assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(connection.instance_id, - connection.received_msg_queue) + response = await self.websocket.recv_msg_of_debugger_server(connection.instance_id, + connection.received_msg_queue) response = json.loads(response) assert response == {"id": message_id, "result": {}} return response - @classmethod - async def remove_breakpoints_by_url(cls, message_id, connection, websocket=None, params=None): + async def remove_breakpoints_by_url(self, message_id, connection, params): response = await communicate_with_debugger_server(connection.instance_id, connection.send_msg_queue, connection.received_msg_queue, - debugger.remove_breakpoints_by_url(params.url), message_id) + debugger.remove_breakpoints_by_url(params), message_id) response = json.loads(response) assert response == {"id": message_id, "result": {}} return response - @classmethod - async def get_possible_and_set_breakpoints_by_url(cls, message_id, connection, websocket=None, params=None): + async def get_possible_and_set_breakpoints_by_url(self, message_id, connection, params): response = await communicate_with_debugger_server(connection.instance_id, connection.send_msg_queue, connection.received_msg_queue, - debugger.get_possible_and_set_breakpoint_by_url(params.locations), + debugger.get_possible_and_set_breakpoint_by_url(params), message_id) response = json.loads(response) assert response['id'] == message_id return response - @classmethod - async def step_over(cls, message_id, connection, websocket, params=None): + async def step_over(self, message_id, connection, params): response = await communicate_with_debugger_server(connection.instance_id, connection.send_msg_queue, connection.received_msg_queue, debugger.step_over(), message_id) assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(connection.instance_id, - connection.received_msg_queue) + response = await self.websocket.recv_msg_of_debugger_server(connection.instance_id, + connection.received_msg_queue) response = json.loads(response) assert response == {"id": message_id, "result": {}} return response - @classmethod - async def step_out(cls, message_id, connection, websocket, params=None): + async def step_out(self, message_id, connection, params): response = await communicate_with_debugger_server(connection.instance_id, connection.send_msg_queue, connection.received_msg_queue, debugger.step_out(), message_id) assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(connection.instance_id, - connection.received_msg_queue) + response = await self.websocket.recv_msg_of_debugger_server(connection.instance_id, + connection.received_msg_queue) response = json.loads(response) assert response == {"id": message_id, "result": {}} return response - @classmethod - async def step_into(cls, message_id, connection, websocket, params=None): + async def step_into(self, message_id, connection, params): response = await communicate_with_debugger_server(connection.instance_id, connection.send_msg_queue, connection.received_msg_queue, debugger.step_into(), message_id) assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(connection.instance_id, - connection.received_msg_queue) + response = await self.websocket.recv_msg_of_debugger_server(connection.instance_id, + connection.received_msg_queue) response = json.loads(response) assert response == {"id": message_id, "result": {}} return response - async def send(self, protocol_name, connection, websocket, params=None): - protocol = self._check_and_parse_protocol(protocol_name) - if self.dispatch_table.get(protocol) is not None: - if self.dispatch_table.get(protocol)[1] != ProtocolType.send: - raise AssertionError("DebuggerImpl send ProtocolType inconsistent: Protocol {}, calling {}, should be {}" - .format(protocol_name, "send", self.dispatch_table.get(protocol)[1])) - message_id = next(self.id_generator) - return await self.dispatch_table.get(protocol)[0](message_id, connection, websocket, params) - - async def recv(self, protocol_name, connection, websocket, params=None): - protocol = self._check_and_parse_protocol(protocol_name) - if self.dispatch_table.get(protocol) is not None: - if self.dispatch_table.get(protocol)[1] != ProtocolType.recv: - raise AssertionError("DebuggerImpl recv ProtocolType inconsistent: Protocol {}, calling {}, should be {}" - .format(protocol_name, "recv", self.dispatch_table.get(protocol)[1])) - return await self.dispatch_table.get(protocol)[0](connection, websocket, params) - - def _check_and_parse_protocol(self, protocol_name): - res = protocol_name.split('.') - if len(res) != 2: - raise AssertionError("DebuggerImpl parse protocol name error: protocol_name {} is invalid" - .format(protocol_name)) - domain, protocol = res[0], res[1] - if domain != 'Debugger': - raise AssertionError("DebuggerImpl parse protocol name error: protocol_name {} has the wrong domain" - .format(protocol_name)) - if protocol not in self.dispatch_table: - raise AssertionError("DebuggerImpl parse protocol name error: protocol_name {} has not been defined" - .format(protocol_name)) - return protocol \ No newline at end of file + async def set_pause_on_exceptions(self, message_id, connection, params): + response = await communicate_with_debugger_server(connection.instance_id, + connection.send_msg_queue, + connection.received_msg_queue, + debugger.set_pause_on_exceptions(params), message_id) + response = json.loads(response) + assert response == {"id": message_id, "result": {}} + return response + + async def evaluate_on_call_frame(self, message_id, connection, params): + response = await communicate_with_debugger_server(connection.instance_id, + connection.send_msg_queue, + connection.received_msg_queue, + debugger.evaluate_on_call_frame(params), message_id) + response = json.loads(response) + assert response['id'] == message_id + return response + + async def pause(self, message_id, connection, params): + response = await communicate_with_debugger_server(connection.instance_id, + connection.send_msg_queue, + connection.received_msg_queue, + debugger.pause(), message_id) + response = json.loads(response) + assert response == {"id": message_id, "result": {}} + return response + + async def smart_step_into(self, message_id, connection, params): + response = await communicate_with_debugger_server(connection.instance_id, + connection.send_msg_queue, + connection.received_msg_queue, + debugger.smart_step_into(params), message_id) + response = json.loads(response) + assert response == {"id": message_id, "result": {}} + return response + + async def set_mixed_debug_enabled(self, message_id, connection, params): + response = await communicate_with_debugger_server(connection.instance_id, + connection.send_msg_queue, + connection.received_msg_queue, + debugger.set_mixed_debug_enabled(params), message_id) + response = json.loads(response) + assert response == {"id": message_id, "result": {}} + return response + + async def native_calling(self, connection, params): + response = await self.websocket.recv_msg_of_debugger_server(connection.instance_id, + connection.received_msg_queue) + response = json.loads(response) + assert response['method'] == 'Debugger.nativeCalling' + return response + + async def reply_native_calling(self, message_id, connection, params): + response = await communicate_with_debugger_server(connection.instance_id, + connection.send_msg_queue, + connection.received_msg_queue, + debugger.reply_native_calling(params), message_id) + assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} + response = await self.websocket.recv_msg_of_debugger_server(connection.instance_id, + connection.received_msg_queue) + response = json.loads(response) + assert response == {"id": message_id, "result": {}} + return response + + async def drop_frame(self, message_id, connection, params): + response = await communicate_with_debugger_server(connection.instance_id, + connection.send_msg_queue, + connection.received_msg_queue, + debugger.drop_frame(params), message_id) + response = json.loads(response) + assert response == {"id": message_id, "result": {}} + return response \ No newline at end of file diff --git a/test/autotest/aw/api/heap_profiler_api.py b/test/autotest/aw/api/heap_profiler_api.py new file mode 100644 index 0000000000000000000000000000000000000000..4b1452affb24d7fb42e18ce0cef23b115c43b499 --- /dev/null +++ b/test/autotest/aw/api/heap_profiler_api.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Copyright (c) 2024 Huawei Device Co., Ltd. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Description: Python HeapProfiler Domain Interfaces +""" + +import json + +from aw import communicate_with_debugger_server +from aw.cdp import heap_profiler +from aw.types import ProtocolType +from aw.api.protocol_api import ProtocolImpl + + +class HeapProfilerImpl(ProtocolImpl): + + def __init__(self, id_generator, websocket): + super().__init__(id_generator, websocket) + self.dispatch_table = {"startTrackingHeapObjects": (self.start_tracking_heap_objects, ProtocolType.send), + "stopTrackingHeapObjects": (self.stop_tracking_heap_objects, ProtocolType.send), + "takeHeapSnapshot": (self.take_heap_snapshot, ProtocolType.send)} + + async def start_tracking_heap_objects(self, message_id, connection, params): + response = await communicate_with_debugger_server(connection.instance_id, + connection.send_msg_queue, + connection.received_msg_queue, + heap_profiler.start_tracking_heap_objects(params), message_id) + response = json.loads(response) + assert response == {"id": message_id, "result": {"protocols": []}} + return response + + async def stop_tracking_heap_objects(self, message_id, connection, params): + response = await communicate_with_debugger_server(connection.instance_id, + connection.send_msg_queue, + connection.received_msg_queue, + heap_profiler.stop_tracking_heap_objects(), message_id) + while response.startswith('{"method":"HeapProfiler.lastSeenObjectId"'): + response = await self.websocket.recv_msg_of_debugger_server(connection.instance_id, + connection.received_msg_queue) + assert r'\"location_fields\":[\"object_index\",\"script_id\",\"line\",\"column\"]' in response + pre_response = response + while response.startswith('{"method":"HeapProfiler.addHeapSnapshotChunk"') or \ + response.startswith('{"method":"HeapProfiler.lastSeenObjectId"'): + if response.startswith('{"method":"HeapProfiler.addHeapSnapshotChunk"'): + pre_response = response + response = await self.websocket.recv_msg_of_debugger_server(connection.instance_id, + connection.received_msg_queue) + assert pre_response.endswith(r'\n]\n}\n"}}') + response = json.loads(response) + assert response == {"id": message_id, "result": {}} + assert all(i >= 0 for i in response['result']['profile']['timeDeltas']) + return response + + async def take_heap_snapshot(self, message_id, connection, params): + response = await communicate_with_debugger_server(connection.instance_id, + connection.send_msg_queue, + connection.received_msg_queue, + heap_profiler.take_heap_snapshot(), message_id) + assert r'\"location_fields\":[\"object_index\",\"script_id\",\"line\",\"column\"]' in response + pre_response = response + while response.startswith('{"method":"HeapProfiler.addHeapSnapshotChunk"'): + pre_response = response + response = await self.websocket.recv_msg_of_debugger_server(connection.instance_id, + connection.received_msg_queue) + assert pre_response.endswith(r'\n]\n}\n"}}') + response = json.loads(response) + assert response == {"id": message_id, "result": {"protocols": []}} + return response \ No newline at end of file diff --git a/test/autotest/aw/api/profiler_api.py b/test/autotest/aw/api/profiler_api.py new file mode 100644 index 0000000000000000000000000000000000000000..416cb6f3bb108919ae514be53d8b20a831f29b42 --- /dev/null +++ b/test/autotest/aw/api/profiler_api.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Copyright (c) 2024 Huawei Device Co., Ltd. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Description: Python Profiler Domain Interfaces +""" + +import json + +from aw import communicate_with_debugger_server +from aw.cdp import profiler +from aw.types import ProtocolType +from aw.api.protocol_api import ProtocolImpl + + +class ProfilerImpl(ProtocolImpl): + + def __init__(self, id_generator, websocket): + super().__init__(id_generator, websocket) + self.dispatch_table = {"start": (self.start, ProtocolType.send), + "stop": (self.stop, ProtocolType.send), + "setSamplingInterval": (self.set_sampling_interval, ProtocolType.send)} + + async def start(self, message_id, connection, params): + response = await communicate_with_debugger_server(connection.instance_id, + connection.send_msg_queue, + connection.received_msg_queue, + profiler.start(), message_id) + response = json.loads(response) + assert response == {"id": message_id, "result": {"protocols": []}} + return response + + async def stop(self, message_id, connection, params): + response = await communicate_with_debugger_server(connection.instance_id, + connection.send_msg_queue, + connection.received_msg_queue, + profiler.stop(), message_id) + response = json.loads(response) + assert response == {"id": message_id, "result": {}} + assert all(i >= 0 for i in response['result']['profile']['timeDeltas']) + return response + + async def set_sampling_interval(self, message_id, connection, params): + response = await communicate_with_debugger_server(connection.instance_id, + connection.send_msg_queue, + connection.received_msg_queue, + profiler.set_sampling_interval(params), message_id) + response = json.loads(response) + assert response == {"id": message_id, "result": {"protocols": []}} + return response \ No newline at end of file diff --git a/test/autotest/aw/api/protocol_api.py b/test/autotest/aw/api/protocol_api.py new file mode 100644 index 0000000000000000000000000000000000000000..bf4ca9c3696a2d383fb76d75246cc0cf79d6a0c6 --- /dev/null +++ b/test/autotest/aw/api/protocol_api.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Copyright (c) 2024 Huawei Device Co., Ltd. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Description: Python Protocol Domain Interfaces +""" + +from aw.types import ProtocolType + + +class ProtocolImpl(object): + + def __init__(self, id_generator, websocket): + self.id_generator = id_generator + self.class_name = self.__class__.__name__ + self.domain = self.class_name[:-4] + self.dispatch_table = {} + self.websocket = websocket + + async def send(self, protocol_name, connection, params=None): + protocol = self._check_and_parse_protocol(protocol_name) + if self.dispatch_table.get(protocol) is not None: + if self.dispatch_table.get(protocol)[1] != ProtocolType.send: + raise AssertionError("{} send ProtocolType inconsistent: Protocol {}, calling {}, should be {}" + .format(self.class_name, protocol_name, "send", + self.dispatch_table.get(protocol)[1])) + message_id = next(self.id_generator) + return await self.dispatch_table.get(protocol)[0](message_id, connection, params) + + async def recv(self, protocol_name, connection, params=None): + protocol = self._check_and_parse_protocol(protocol_name) + if self.dispatch_table.get(protocol) is not None: + if self.dispatch_table.get(protocol)[1] != ProtocolType.recv: + raise AssertionError("{} recv ProtocolType inconsistent: Protocol {}, calling {}, should be {}" + .format(self.class_name, protocol_name, "recv", + self.dispatch_table.get(protocol)[1])) + return await self.dispatch_table.get(protocol)[0](connection, params) + + def _check_and_parse_protocol(self, protocol_name): + res = protocol_name.split('.') + if len(res) != 2: + raise AssertionError("{} parse protocol name error: protocol_name {} is invalid" + .format(self.class_name, protocol_name)) + domain, protocol = res[0], res[1] + if domain != self.domain: + raise AssertionError("{} parse protocol name error: protocol_name {} has the wrong domain" + .format(self.class_name, protocol_name)) + if protocol not in self.dispatch_table: + raise AssertionError("{} parse protocol name error: protocol_name {} has not been defined" + .format(self.class_name, protocol_name)) + return protocol \ No newline at end of file diff --git a/test/autotest/aw/api/runtime_api.py b/test/autotest/aw/api/runtime_api.py index af6d00029cdb459e2ae16b0b31f737d3161253b9..d9160c7bd965e2a02931c538c9b79ba5d392d314 100644 --- a/test/autotest/aw/api/runtime_api.py +++ b/test/autotest/aw/api/runtime_api.py @@ -22,18 +22,19 @@ import json from aw import communicate_with_debugger_server from aw.cdp import runtime from aw.types import ProtocolType +from aw.api.protocol_api import ProtocolImpl -class RuntimeImpl(object): +class RuntimeImpl(ProtocolImpl): - def __init__(self, id_generator): + def __init__(self, id_generator, websocket): + super().__init__(id_generator, websocket) self.dispatch_table = {"enable": (self.enable, ProtocolType.send), "runIfWaitingForDebugger": (self.run_if_waiting_for_debugger, ProtocolType.send), - "getProperties": (self.get_properties, ProtocolType.send)} - self.id_generator = id_generator + "getProperties": (self.get_properties, ProtocolType.send), + "getHeapUsage": (self.get_heap_usage, ProtocolType.send)} - @classmethod - async def enable(cls, message_id, connection, params=None): + async def enable(self, message_id, connection, params): response = await communicate_with_debugger_server(connection.instance_id, connection.send_msg_queue, connection.received_msg_queue, @@ -42,8 +43,7 @@ class RuntimeImpl(object): assert response == {"id": message_id, "result": {"protocols": []}} return response - @classmethod - async def run_if_waiting_for_debugger(cls, message_id, connection, params=None): + async def run_if_waiting_for_debugger(self, message_id, connection, params): response = await communicate_with_debugger_server(connection.instance_id, connection.send_msg_queue, connection.received_msg_queue, @@ -52,47 +52,26 @@ class RuntimeImpl(object): assert response == {"id": message_id, "result": {}} return response - @classmethod - async def get_properties(cls, message_id, connection, params=None): + async def get_properties(self, message_id, connection, params): response = await communicate_with_debugger_server(connection.instance_id, connection.send_msg_queue, connection.received_msg_queue, - runtime.get_properties(object_id=params.object_id, - own_properties=params.own_properties, - accessor_properties_only=params.accessor_properties_only, - generate_preview=params.generate_preview), message_id) + runtime.get_properties(params), message_id) response = json.loads(response) assert response["id"] == message_id return response - async def send(self, protocol_name, connection, params=None): - protocol = self._check_and_parse_protocol(protocol_name) - if self.dispatch_table.get(protocol) is not None: - if self.dispatch_table.get(protocol)[1] != ProtocolType.send: - raise AssertionError("RuntimeImpl send ProtocolType inconsistent: Protocol {}, calling {}, should be {}" - .format(protocol_name, "send", self.dispatch_table.get(protocol)[1])) - message_id = next(self.id_generator) - return await self.dispatch_table.get(protocol)[0](message_id, connection, params) - - async def recv(self, protocol_name, connection, params=None): - protocol = self._check_and_parse_protocol(protocol_name) - if self.dispatch_table.get(protocol) is not None: - if self.dispatch_table.get(protocol)[1] != ProtocolType.recv: - raise AssertionError("RuntimeImpl recv ProtocolType inconsistent: Protocol {}, calling {}, should be {}" - .format(protocol_name, "recv", self.dispatch_table.get(protocol)[1])) - message_id = next(self.id_generator) - return await self.dispatch_table.get(protocol)[0](message_id, connection, params) - - def _check_and_parse_protocol(self, protocol_name): - res = protocol_name.split('.') - if len(res) != 2: - raise AssertionError("RuntimeImpl parse protocol name error: protocol_name {} is invalid" - .format(protocol_name)) - domain, protocol = res[0], res[1] - if domain != 'Runtime': - raise AssertionError("RuntimeImpl parse protocol name error: protocol_name {} has the wrong domain" - .format(protocol_name)) - if protocol not in self.dispatch_table: - raise AssertionError("RuntimeImpl parse protocol name error: protocol_name {} has not been defined" - .format(protocol_name)) - return protocol + async def get_heap_usage(self, message_id, connection, params): + response = await communicate_with_debugger_server(connection.instance_id, + connection.send_msg_queue, + connection.received_msg_queue, + runtime.get_heap_usage(), message_id) + while response.startswith('{"method":"HeapProfiler.lastSeenObjectId"') or \ + response.startswith('{"method":"HeapProfiler.heapStatsUpdate"'): + response = await communicate_with_debugger_server(connection.instance_id, + connection.received_msg_queue) + response = json.loads(response) + assert response["id"] == message_id + assert response['result']['usedSize'] > 0 + assert response['result']['totalSize'] > 0 + return response \ No newline at end of file diff --git a/test/autotest/aw/cdp/debugger.py b/test/autotest/aw/cdp/debugger.py index 3628d90b6504409e6f7b590b510a3c164c6cbf85..846c0daa7926f9989a5d500f6eeb4cf4979bd089 100644 --- a/test/autotest/aw/cdp/debugger.py +++ b/test/autotest/aw/cdp/debugger.py @@ -18,19 +18,63 @@ Description: Python CDP Debugger. """ from dataclasses import dataclass, field +from enum import Enum from typing import Optional, List +@dataclass +class DropFrameParams: + dropped_depth: int = 1 + + +@dataclass +class ReplyNativeCallingParams: + user_code: bool = True + + +@dataclass +class SetMixedDebugEnabledParams: + enabled: bool + mixed_stack_enabled: bool + + +@dataclass +class SmartStepIntoParams: + url: str + line_number: int + + +@dataclass +class PauseOnExceptionsState(Enum): + ALL = 'all' + NONE = 'none' + CAUGHT = 'caught' + UNCAUGHT = 'uncaught' + + + +@dataclass +class EvaluateOnCallFrameParams: + expression: str + call_frame_id: int = 0 + object_group: str = "console" + include_command_line_api: bool = True + silent: bool = True + + @dataclass class BreakLocationUrl: url: str line_number: int column_number: Optional[int] = 0 + condition: Optional[str] = None def to_json(self): json = {'url': self.url, 'lineNumber': self.line_number, 'columnNumber': self.column_number} + if self.condition is not None: + json['condition'] = self.condition return json @@ -44,10 +88,8 @@ class SetBreakpointsLocations: locations: list = field(default_factory=list) -def enable(max_scripts_cache_size: Optional[float] = None): +def enable(): command = {'method': 'Debugger.enable'} - if max_scripts_cache_size: - command['params'] = {'maxScriptsCacheSize': max_scripts_cache_size} return command @@ -56,18 +98,18 @@ def resume(): return command -def remove_breakpoints_by_url(url: str): +def remove_breakpoints_by_url(params: RemoveBreakpointsUrl): command = {'method': 'Debugger.removeBreakpointsByUrl', - 'params': {'url': url}} + 'params': {'url': params.url}} return command -def get_possible_and_set_breakpoint_by_url(locations: List[BreakLocationUrl]): - params = {'locations': []} - for location in locations: - params['locations'].append(location.to_json()) +def get_possible_and_set_breakpoint_by_url(params: SetBreakpointsLocations): + locations = [] + for location in params.locations: + locations.append(location.to_json()) command = {'method': 'Debugger.getPossibleAndSetBreakpointByUrl', - 'params': params} + 'params': {'locations': locations}} return command @@ -88,4 +130,50 @@ def step_out(): def disable(): command = {'method': 'Debugger.disable'} + return command + + +def set_pause_on_exceptions(params: PauseOnExceptionsState): + command = {'method': 'Debugger.setPauseOnExceptions', + 'params': {'state': params.value}} + return command + + +def evaluate_on_call_frame(params: EvaluateOnCallFrameParams): + command = {'method': 'Debugger.evaluateOnCallFrame', + 'params': { + 'callFrameId': str(params.call_frame_id), + 'expression': params.expression, + 'includeCommandLineApi': params.include_command_line_api, + 'objectGroup': params.object_group, + 'silent': params.silent}} + return command + + +def pause(): + command = {'method': 'Debugger.pause'} + return command + + +def set_mixed_debug_enabled(params: SetMixedDebugEnabledParams): + command = {'method': 'Debugger.setMixedDebugEnabled', + 'params': {'enabled': params.enabled, 'mixedStackEnabled': params.mixed_stack_enabled}} + return command + + +def reply_native_calling(params: ReplyNativeCallingParams): + command = {'method': 'Debugger.replyNativeCalling', + 'params': {'userCode': params.user_code}} + return command + + +def drop_frame(params: DropFrameParams): + command = {'method': 'Debugger.dropFrame', + 'params': {'droppedDepth': params.dropped_depth}} + return command + + +def smart_step_into(params: SmartStepIntoParams): + command = {'method': 'Debugger.smartStepInto', + 'params': {'url': params.url, 'lineNumber': params.line_number}} return command \ No newline at end of file diff --git a/test/autotest/aw/cdp/heap_profiler.py b/test/autotest/aw/cdp/heap_profiler.py index 59743356be8981a598bc4fd71d86995a94190745..29f480a3b81e1e26361907b613ffb544c253f26c 100644 --- a/test/autotest/aw/cdp/heap_profiler.py +++ b/test/autotest/aw/cdp/heap_profiler.py @@ -16,7 +16,13 @@ limitations under the License. Description: Python CDP Heap Profiler. """ -import typing + +from dataclasses import dataclass + + +@dataclass +class TrackingHeapObjectsPrams: + track_allocations: bool def enable(): @@ -27,11 +33,9 @@ def disable(): return {'method': 'HeapProfiler.disable'} -def start_tracking_heap_objects(track_allocations: typing.Optional[bool] = None): - params = dict() - if track_allocations is not None: - params['trackAllocations'] = track_allocations - return {'method': 'HeapProfiler.startTrackingHeapObjects', 'params': params} +def start_tracking_heap_objects(params: TrackingHeapObjectsPrams): + return {'method': 'HeapProfiler.startTrackingHeapObjects', + 'params': {'trackAllocations': params.track_allocations}} def stop_tracking_heap_objects(): diff --git a/test/autotest/aw/cdp/cpu_profiler.py b/test/autotest/aw/cdp/profiler.py similarity index 79% rename from test/autotest/aw/cdp/cpu_profiler.py rename to test/autotest/aw/cdp/profiler.py index 4e0be651cf9a39f84e840937e987c71f0dfe24fb..722da3332dc476ab908b82988d6882dbad1bf9fc 100644 --- a/test/autotest/aw/cdp/cpu_profiler.py +++ b/test/autotest/aw/cdp/profiler.py @@ -17,6 +17,13 @@ limitations under the License. Description: Python CDP CPU Profiler. """ +from dataclasses import dataclass + + +@dataclass +class SamplingInterval: + interval: int + def enable(): return {'method': 'Profiler.enable'} @@ -34,6 +41,6 @@ def stop(): return {'method': 'Profiler.stop'} -def set_sampling_interval(interval: int): +def set_sampling_interval(params: SamplingInterval): return {'method': 'Profiler.setSamplingInterval', - 'params': {'interval': interval}} \ No newline at end of file + 'params': {'interval': params.interval}} \ No newline at end of file diff --git a/test/autotest/aw/cdp/runtime.py b/test/autotest/aw/cdp/runtime.py index 7a18cd257bef56cef5641a98b84128d8255d47e0..ac1c96f2580f9f2e5d145e084cd7463b9ee064e7 100644 --- a/test/autotest/aw/cdp/runtime.py +++ b/test/autotest/aw/cdp/runtime.py @@ -16,6 +16,7 @@ limitations under the License. Description: Python CDP Runtime. """ + from dataclasses import dataclass from typing import Optional @@ -26,7 +27,7 @@ class GetPropertiesParams: own_properties: bool = True accessor_properties_only: bool = False generate_preview: bool = True - + non_indexed_properties_only: Optional[bool] = None def enable(): command = {'method': 'Runtime.enable'} @@ -38,22 +39,14 @@ def run_if_waiting_for_debugger(): return command -def get_properties(object_id: str, - own_properties: Optional[bool] = None, - accessor_properties_only: Optional[bool] = None, - generate_preview: Optional[bool] = None, - non_indexed_properties_only: Optional[bool] = None): - command = {'method': 'Runtime.getProperties'} - params = {'objectId': object_id} - if own_properties is not None: - params['ownProperties'] = own_properties - if accessor_properties_only is not None: - params['accessorPropertiesOnly'] = accessor_properties_only - if generate_preview is not None: - params['generatePreview'] = generate_preview - if non_indexed_properties_only is not None: - params['nonIndexedPropertiesOnly'] = non_indexed_properties_only - command['params'] = params +def get_properties(params: GetPropertiesParams): + command = {'method': 'Runtime.getProperties', + 'params': {'objectId': params.object_id, + 'ownProperties': params.own_properties, + 'accessorPropertiesOnly': params.accessor_properties_only, + 'generatePreview': params.generate_preview}} + if params.non_indexed_properties_only is not None: + command['params']['nonIndexedPropertiesOnly'] = params.non_indexed_properties_only return command diff --git a/test/autotest/aw/websocket.py b/test/autotest/aw/websocket.py index 28d2bfecb9322ecbb5153253f7c45649b3d6fa6b..fec8b18484e4214272b2d2755bf703d6071c0ad6 100644 --- a/test/autotest/aw/websocket.py +++ b/test/autotest/aw/websocket.py @@ -90,7 +90,7 @@ class WebSocket(object): logging.info(f'[==>] Connect server send message: {message}') return True - async def main_task(self, taskpool, websocket, procedure, pid): + async def main_task(self, taskpool, procedure, pid): # the async queue must be initialized in task self.to_send_msg_queue_for_connect_server = asyncio.Queue() self.received_msg_queue_for_connect_server = asyncio.Queue() @@ -101,7 +101,7 @@ class WebSocket(object): taskpool.submit(self._receiver_of_connect_server(connect_server_client, self.received_msg_queue_for_connect_server, taskpool, pid)) - taskpool.submit(procedure(websocket)) + taskpool.submit(procedure(self)) def _connect_connect_server(self): client = connect(f'ws://localhost:{self.connect_server_port}', diff --git a/test/autotest/resource/archive.json b/test/autotest/resource/archive.json index 5d18fb46c2a8a4e0c2c54988d4a8e38e39df8f70..46f035be15caba653c59fa25d5c99f1b93ea808f 100644 --- a/test/autotest/resource/archive.json +++ b/test/autotest/resource/archive.json @@ -2,16 +2,61 @@ { "application directory": "MultiWorker01", "hap name": "MultiWorker01.hap", - "bundle name": "com.example.multiWorker01" + "bundle name": "com.example.multiWorker01", + "description": "" }, { "application directory": "MultiWorker02", "hap name": "MultiWorker02.hap", - "bundle name": "com.example.multiWorker02" + "bundle name": "com.example.multiWorker02", + "description": "" + }, + { + "application directory": "MultiWorker03", + "hap name": "MultiWorker03.hap", + "bundle name": "com.example.multiWorker03", + "description": "" + }, + { + "application directory": "MultiWorker04", + "hap name": "MultiWorker04.hap", + "bundle name": "com.example.multiWorker04", + "description": "" + }, + { + "application directory": "MultiWorker05", + "hap name": "MultiWorker05.hap", + "bundle name": "com.example.multiWorker05", + "description": "" + }, + { + "application directory": "MultiWorker06", + "hap name": "MultiWorker06.hap", + "bundle name": "com.example.multiWorker06", + "description": "" }, { "application directory": "TaskPool01", "hap name": "TaskPool01.hap", - "bundle name": "com.example.taskPool01" + "bundle name": "com.example.taskPool01", + "description": "" + }, + { + "application directory": "TaskPool02", + "hap name": "TaskPool02.hap", + "bundle name": "com.example.taskPool02", + "description": "" + }, + { + "application directory": "Native01", + "hap name": "Native01.hap", + "bundle name": "com.example.native01", + "description": "" + }, + { + "application directory": "Native02", + "hap name": "Native02.hap", + "bundle name": "com.example.native02", + "description": "" } ] \ No newline at end of file diff --git a/test/autotest/scenario_test/conftest.py b/test/autotest/scenario_test/conftest.py index 98822b5ffeaa5ca94111ca29ee3c6df5d94c9f3f..8421c9e35e3581705bdea5a021ca2772d5ad3b32 100644 --- a/test/autotest/scenario_test/conftest.py +++ b/test/autotest/scenario_test/conftest.py @@ -26,15 +26,15 @@ from aw import Application, Fport, WebSocket, TaskPool @pytest.fixture(scope='class') -def test_suite_worker_01(): - logging.info('running worker_01 in default mode') +def test_suite_worker_06_debug(): + logging.info('running worker_06 in debug mode') config = { - 'start_mode': None, - 'connect_server_port': 15678, - 'debugger_server_port': 15679, - 'bundle_name': 'com.example.multiWorker01', - 'hap_name': 'MultiWorker01.hap', - 'hap_path': rf'{os.path.dirname(__file__)}\..\resource\MultiWorker01.hap', + 'start_mode': '-D', + 'connect_server_port': 15694, + 'debugger_server_port': 15695, + 'bundle_name': 'com.example.multiWorker06', + 'hap_name': 'MultiWorker06.hap', + 'hap_path': rf'{os.path.dirname(__file__)}\..\resource\MultiWorker06.hap', 'file_path': { 'entry_ability': 'entry|entry|1.0.0|src/main/ets/entryability/EntryAbility.ts', 'index': 'entry|entry|1.0.0|src/main/ets/pages/Index.ts', @@ -46,15 +46,55 @@ def test_suite_worker_01(): @pytest.fixture(scope='class') -def test_suite_worker_01_debug(): - logging.info('running worker_01 in debug mode') +def test_suite_worker_05_debug(): + logging.info('running worker_05 in debug mode') config = { 'start_mode': '-D', - 'connect_server_port': 15678, - 'debugger_server_port': 15679, - 'bundle_name': 'com.example.multiWorker01', - 'hap_name': 'MultiWorker01.hap', - 'hap_path': rf'{os.path.dirname(__file__)}\..\resource\MultiWorker01.hap', + 'connect_server_port': 15692, + 'debugger_server_port': 15693, + 'bundle_name': 'com.example.multiWorker05', + 'hap_name': 'MultiWorker05.hap', + 'hap_path': rf'{os.path.dirname(__file__)}\..\resource\MultiWorker05.hap', + 'file_path': { + 'entry_ability': 'entry|entry|1.0.0|src/main/ets/entryability/EntryAbility.ts', + 'index': 'entry|entry|1.0.0|src/main/ets/pages/Index.ts', + 'worker': 'entry|entry|1.0.0|src/main/ets/workers/Worker.ts' + } + } + launch_hap(config) + return config + + +@pytest.fixture(scope='class') +def test_suite_worker_04_debug(): + logging.info('running worker_04 in debug mode') + config = { + 'start_mode': '-D', + 'connect_server_port': 15690, + 'debugger_server_port': 15691, + 'bundle_name': 'com.example.multiWorker04', + 'hap_name': 'MultiWorker04.hap', + 'hap_path': rf'{os.path.dirname(__file__)}\..\resource\MultiWorker04.hap', + 'file_path': { + 'entry_ability': 'entry|entry|1.0.0|src/main/ets/entryability/EntryAbility.ts', + 'index': 'entry|entry|1.0.0|src/main/ets/pages/Index.ts', + 'worker': 'entry|entry|1.0.0|src/main/ets/workers/Worker.ts' + } + } + launch_hap(config) + return config + + +@pytest.fixture(scope='class') +def test_suite_worker_03_debug(): + logging.info('running worker_03 in debug mode') + config = { + 'start_mode': '-D', + 'connect_server_port': 15688, + 'debugger_server_port': 15689, + 'bundle_name': 'com.example.multiWorker03', + 'hap_name': 'MultiWorker03.hap', + 'hap_path': rf'{os.path.dirname(__file__)}\..\resource\MultiWorker03.hap', 'file_path': { 'entry_ability': 'entry|entry|1.0.0|src/main/ets/entryability/EntryAbility.ts', 'index': 'entry|entry|1.0.0|src/main/ets/pages/Index.ts', @@ -65,13 +105,28 @@ def test_suite_worker_01_debug(): return config +@pytest.fixture(scope='class') +def test_suite_worker_02_profile(): + logging.info('running worker_02 in profile mode') + config = { + 'start_mode': '-p profile jsperf', + 'connect_server_port': 15686, + 'debugger_server_port': 15687, + 'bundle_name': 'com.example.multiWorker02', + 'hap_name': 'MultiWorker02.hap', + 'hap_path': rf'{os.path.dirname(__file__)}\..\resource\MultiWorker02.hap' + } + launch_hap(config) + return config + + @pytest.fixture(scope='class') def test_suite_worker_02(): logging.info('running worker_02 in default mode') config = { 'start_mode': None, - 'connect_server_port': 15680, - 'debugger_server_port': 15681, + 'connect_server_port': 15684, + 'debugger_server_port': 15685, 'bundle_name': 'com.example.multiWorker02', 'hap_name': 'MultiWorker02.hap', 'hap_path': rf'{os.path.dirname(__file__)}\..\resource\MultiWorker02.hap' @@ -81,15 +136,98 @@ def test_suite_worker_02(): @pytest.fixture(scope='class') -def test_suite_worker_02_profile(): - logging.info('running worker_02 in profile mode') +def test_suite_worker_01_debug(): + logging.info('running worker_01 in debug mode') config = { - 'start_mode': '-p profile jsperf', + 'start_mode': '-D', + 'connect_server_port': 15682, + 'debugger_server_port': 15683, + 'bundle_name': 'com.example.multiWorker01', + 'hap_name': 'MultiWorker01.hap', + 'hap_path': rf'{os.path.dirname(__file__)}\..\resource\MultiWorker01.hap', + 'file_path': { + 'entry_ability': 'entry|entry|1.0.0|src/main/ets/entryability/EntryAbility.ts', + 'index': 'entry|entry|1.0.0|src/main/ets/pages/Index.ts', + 'worker': 'entry|entry|1.0.0|src/main/ets/workers/Worker.ts' + } + } + launch_hap(config) + return config + + +@pytest.fixture(scope='class') +def test_suite_worker_01(): + logging.info('running worker_01 in default mode') + config = { + 'start_mode': None, 'connect_server_port': 15680, 'debugger_server_port': 15681, - 'bundle_name': 'com.example.multiWorker02', - 'hap_name': 'MultiWorker02.hap', - 'hap_path': rf'{os.path.dirname(__file__)}\..\resource\MultiWorker02.hap' + 'bundle_name': 'com.example.multiWorker01', + 'hap_name': 'MultiWorker01.hap', + 'hap_path': rf'{os.path.dirname(__file__)}\..\resource\MultiWorker01.hap', + 'file_path': { + 'entry_ability': 'entry|entry|1.0.0|src/main/ets/entryability/EntryAbility.ts', + 'index': 'entry|entry|1.0.0|src/main/ets/pages/Index.ts', + 'worker': 'entry|entry|1.0.0|src/main/ets/workers/Worker.ts' + } + } + launch_hap(config) + return config + + +@pytest.fixture(scope='class') +def test_suite_native_02_debug(): + logging.info('running native_02 in debug mode') + config = { + 'start_mode': '-D', + 'connect_server_port': 15678, + 'debugger_server_port': 15679, + 'bundle_name': 'com.example.native02', + 'hap_name': 'Native02.hap', + 'hap_path': rf'{os.path.dirname(__file__)}\..\resource\Native02.hap', + 'file_path': { + 'entry_ability': 'entry|entry|1.0.0|src/main/ets/entryability/EntryAbility.ts', + 'index': 'entry|entry|1.0.0|src/main/ets/pages/Index.ts', + 'worker': 'entry|entry|1.0.0|src/main/ets/workers/Worker.ts' + } + } + launch_hap(config) + return config + + +@pytest.fixture(scope='class') +def test_suite_native_01_debug(): + logging.info('running native_01 in debug mode') + config = { + 'start_mode': '-D', + 'connect_server_port': 15676, + 'debugger_server_port': 15677, + 'bundle_name': 'com.example.native01', + 'hap_name': 'Native01.hap', + 'hap_path': rf'{os.path.dirname(__file__)}\..\resource\Native01.hap', + 'file_path': { + 'entry_ability': 'entry|entry|1.0.0|src/main/ets/entryability/EntryAbility.ts', + 'index': 'entry|entry|1.0.0|src/main/ets/pages/Index.ts' + } + } + launch_hap(config) + return config + + +@pytest.fixture(scope='class') +def test_suite_taskpool_02_debug(): + logging.info('running taskpool_02 in debug mode') + config = { + 'start_mode': '-D', + 'connect_server_port': 15674, + 'debugger_server_port': 15675, + 'bundle_name': 'com.example.taskPool02', + 'hap_name': 'TaskPool02.hap', + 'hap_path': rf'{os.path.dirname(__file__)}\..\resource\TaskPool02.hap', + 'file_path': { + 'entry_ability': 'entry|entry|1.0.0|src/main/ets/entryability/EntryAbility.ts', + 'index': 'entry|entry|1.0.0|src/main/ets/pages/Index.ts', + } } launch_hap(config) return config @@ -100,8 +238,8 @@ def test_suite_taskpool_01(): logging.info('running taskpool_01 in default mode') config = { 'start_mode': None, - 'connect_server_port': 15682, - 'debugger_server_port': 15683, + 'connect_server_port': 15672, + 'debugger_server_port': 15673, 'bundle_name': 'com.example.taskPool01', 'hap_name': 'TaskPool01.hap', 'hap_path': rf'{os.path.dirname(__file__)}\..\resource\TaskPool01.hap', @@ -119,8 +257,8 @@ def test_suite_taskpool_01_debug(): logging.info('running taskpool_01 in debug mode') config = { 'start_mode': '-D', - 'connect_server_port': 15682, - 'debugger_server_port': 15683, + 'connect_server_port': 15670, + 'debugger_server_port': 15671, 'bundle_name': 'com.example.taskPool01', 'hap_name': 'TaskPool01.hap', 'hap_path': rf'{os.path.dirname(__file__)}\..\resource\TaskPool01.hap', diff --git a/test/autotest/scenario_test/test_cpu_profiler_01.py b/test/autotest/scenario_test/test_cpu_profiler_01.py index 0161f3e8a3ec0811f540724b312617d2fd1fb17a..0dd15805d90b8b5539127f01cc4d2457e209edc4 100644 --- a/test/autotest/scenario_test/test_cpu_profiler_01.py +++ b/test/autotest/scenario_test/test_cpu_profiler_01.py @@ -17,7 +17,6 @@ limitations under the License. Description: Scenario test case. """ -import json import logging import os import time @@ -26,8 +25,8 @@ import pytest from aw import Utils from aw import Application -from aw import communicate_with_debugger_server -from aw import runtime, debugger, cpu_profiler +from aw import profiler +from aw.api import debugger_api, runtime_api, profiler_api @pytest.mark.cpu_profiler @@ -79,8 +78,11 @@ class TestCpuProfiler01: websocket = self.config['websocket'] taskpool = self.config['taskpool'] pid = self.config['pid'] + self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) + self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) + self.profiler_impl = profiler_api.ProfilerImpl(self.id_generator, websocket) - taskpool.submit(websocket.main_task(taskpool, websocket, self.procedure, pid)) + taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) taskpool.await_taskpool() taskpool.task_join() if taskpool.task_exception: @@ -90,112 +92,53 @@ class TestCpuProfiler01: ################################################################################################################ # main thread: connect the debugger server ################################################################################################################ - send_msg = {"type": "connected"} - await websocket.send_msg_to_connect_server(send_msg) - response = await websocket.recv_msg_of_connect_server() - response = json.loads(response) - assert response['type'] == 'addInstance' - assert response['instanceId'] == 0, logging.error('instance id of the main thread not equal to 0') - assert response['tid'] == self.config['pid'] - main_thread_instance_id = await websocket.get_instance() - main_thread_to_send_queue = websocket.to_send_msg_queues[main_thread_instance_id] - main_thread_received_queue = websocket.received_msg_queues[main_thread_instance_id] - logging.info(f'Connect to the debugger server of instance: {main_thread_instance_id}') + main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) + logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') ################################################################################################################ # worker thread: connect the debugger server ################################################################################################################ - workers_num = 2 - worker_instances_id = [] - worker_thread_to_send_queues = [] - worker_thread_received_queues = [] - for i in range(workers_num): - response = await websocket.recv_msg_of_connect_server() - response = json.loads(response) - assert response['type'] == 'addInstance' - assert response['instanceId'] != 0 - assert response['tid'] != self.config['pid'] - assert 'workerThread_' in response['name'] - worker_instance_id = await websocket.get_instance() - worker_instances_id.append(worker_instance_id) - worker_thread_to_send_queues.append(websocket.to_send_msg_queues[worker_instance_id]) - worker_thread_received_queues.append(websocket.received_msg_queues[worker_instance_id]) - logging.info(f'Connect to the debugger server of instance: {worker_instance_id}') + worker_thread_1 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread_1.instance_id}') + worker_thread_2 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread_2.instance_id}') ################################################################################################################ # main thread: Runtime.enable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - runtime.enable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {"protocols": []}} + await self.runtime_impl.send("Runtime.enable", main_thread) ################################################################################################################ # worker thread: Runtime.enable ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - runtime.enable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {"protocols": []}} + await self.runtime_impl.send("Runtime.enable", worker_thread_1) + await self.runtime_impl.send("Runtime.enable", worker_thread_2) ################################################################################################################ # main thread: Profiler.setSamplingInterval ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - cpu_profiler.set_sampling_interval(500), message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + params = profiler.SamplingInterval(500) + await self.profiler_impl.send("Profiler.setSamplingInterval", main_thread, params) ################################################################################################################ # worker thread: Profiler.setSamplingInterval ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - cpu_profiler.set_sampling_interval(500), message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + params = profiler.SamplingInterval(500) + await self.profiler_impl.send("Profiler.setSamplingInterval", worker_thread_1, params) + await self.profiler_impl.send("Profiler.setSamplingInterval", worker_thread_2, params) ################################################################################################################ # main thread: Profiler.start ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - cpu_profiler.start(), message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.profiler_impl.send("Profiler.start", main_thread) ################################################################################################################ # worker thread: Profiler.start ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - cpu_profiler.start(), message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.profiler_impl.send("Profiler.start", worker_thread_1) + await self.profiler_impl.send("Profiler.start", worker_thread_2) ################################################################################################################ # main thread: Debugger.disable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.disable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.disable", main_thread) ################################################################################################################ # worker thread: Debugger.disable ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - debugger.disable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.disable", worker_thread_1) + await self.debugger_impl.send("Debugger.disable", worker_thread_2) ################################################################################################################ # all thread: sleep 10 seconds ################################################################################################################ @@ -203,35 +146,17 @@ class TestCpuProfiler01: ################################################################################################################ # main thread: Profiler.stop ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - cpu_profiler.stop(), message_id) - response = json.loads(response) - assert response['id'] == message_id - assert all(i >= 0 for i in response['result']['profile']['timeDeltas']) + await self.profiler_impl.send("Profiler.stop", main_thread) ################################################################################################################ # worker thread: Profiler.stop ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - cpu_profiler.stop(), message_id) - response = json.loads(response) - assert response['id'] == message_id - assert all(i >= 0 for i in response['result']['profile']['timeDeltas']) - ################################################################################################################ - # worker thread: destroy instance - ################################################################################################################ - for i in range(workers_num): - await websocket.send_msg_to_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], 'close') + await self.profiler_impl.send("Profiler.stop", worker_thread_1) + await self.profiler_impl.send("Profiler.stop", worker_thread_2) ################################################################################################################ # close the websocket connections ################################################################################################################ - await websocket.send_msg_to_debugger_server(main_thread_instance_id, main_thread_to_send_queue, 'close') + await websocket.send_msg_to_debugger_server(worker_thread_1.instance_id, worker_thread_1.send_msg_queue, 'close') + await websocket.send_msg_to_debugger_server(worker_thread_2.instance_id, worker_thread_2.send_msg_queue, 'close') + await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') await websocket.send_msg_to_connect_server('close') ################################################################################################################ \ No newline at end of file diff --git a/test/autotest/scenario_test/test_cpu_profiler_02.py b/test/autotest/scenario_test/test_cpu_profiler_02.py index 48b67c284c5726fc158eb66afa72e44c99066b6a..15cfbd5c950d467a6a8bb017092a3853d5228aef 100644 --- a/test/autotest/scenario_test/test_cpu_profiler_02.py +++ b/test/autotest/scenario_test/test_cpu_profiler_02.py @@ -17,7 +17,6 @@ limitations under the License. Description: Scenario test case. """ -import json import logging import os import time @@ -26,8 +25,8 @@ import pytest from aw import Utils from aw import Application -from aw import communicate_with_debugger_server -from aw import runtime, debugger, cpu_profiler +from aw import profiler +from aw.api import debugger_api, runtime_api, profiler_api @pytest.mark.cpu_profiler @@ -81,8 +80,11 @@ class TestCpuProfiler02: websocket = self.config['websocket'] taskpool = self.config['taskpool'] pid = self.config['pid'] + self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) + self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) + self.profiler_impl = profiler_api.ProfilerImpl(self.id_generator, websocket) - taskpool.submit(websocket.main_task(taskpool, websocket, self.procedure, pid)) + taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) taskpool.await_taskpool() taskpool.task_join() if taskpool.task_exception: @@ -92,76 +94,36 @@ class TestCpuProfiler02: ################################################################################################################ # main thread: connect the debugger server ################################################################################################################ - send_msg = {"type": "connected"} - await websocket.send_msg_to_connect_server(send_msg) - response = await websocket.recv_msg_of_connect_server() - response = json.loads(response) - assert response['type'] == 'addInstance' - assert response['instanceId'] == 0, logging.error('instance id of the main thread not equal to 0') - assert response['tid'] == self.config['pid'] - main_thread_instance_id = await websocket.get_instance() - main_thread_to_send_queue = websocket.to_send_msg_queues[main_thread_instance_id] - main_thread_received_queue = websocket.received_msg_queues[main_thread_instance_id] - logging.info(f'Connect to the debugger server of instance: {main_thread_instance_id}') + main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) + logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') ################################################################################################################ # worker thread: connect the debugger server ################################################################################################################ - workers_num = 2 - worker_instances_id = [] - worker_thread_to_send_queues = [] - worker_thread_received_queues = [] - for i in range(workers_num): - response = await websocket.recv_msg_of_connect_server() - response = json.loads(response) - assert response['type'] == 'addInstance' - assert response['instanceId'] != 0 - assert response['tid'] != self.config['pid'] - assert 'workerThread_' in response['name'] - worker_instance_id = await websocket.get_instance() - worker_instances_id.append(worker_instance_id) - worker_thread_to_send_queues.append(websocket.to_send_msg_queues[worker_instance_id]) - worker_thread_received_queues.append(websocket.received_msg_queues[worker_instance_id]) - logging.info(f'Connect to the debugger server of instance: {worker_instance_id}') + worker_thread_1 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread_1.instance_id}') + worker_thread_2 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread_2.instance_id}') ################################################################################################################ # worker thread: Runtime.enable ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - runtime.enable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {"protocols": []}} + await self.runtime_impl.send("Runtime.enable", worker_thread_1) + await self.runtime_impl.send("Runtime.enable", worker_thread_2) ################################################################################################################ # worker thread: Profiler.setSamplingInterval ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - cpu_profiler.set_sampling_interval(500), message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + params = profiler.SamplingInterval(500) + await self.profiler_impl.send("Profiler.setSamplingInterval", worker_thread_1, params) + await self.profiler_impl.send("Profiler.setSamplingInterval", worker_thread_2, params) ################################################################################################################ # worker thread: Profiler.start ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - cpu_profiler.start(), message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.profiler_impl.send("Profiler.start", worker_thread_1) + await self.profiler_impl.send("Profiler.start", worker_thread_2) ################################################################################################################ # worker thread: Debugger.disable ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - debugger.disable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.disable", worker_thread_1) + await self.debugger_impl.send("Debugger.disable", worker_thread_2) ################################################################################################################ # all thread: sleep 10 seconds ################################################################################################################ @@ -169,35 +131,17 @@ class TestCpuProfiler02: ################################################################################################################ # worker thread: Profiler.stop ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - cpu_profiler.stop(), message_id) - response = json.loads(response) - assert response['id'] == message_id - assert all(i >= 0 for i in response['result']['profile']['timeDeltas']) - ################################################################################################################ - # worker thread: destroy instance - ################################################################################################################ - for i in range(workers_num): - await websocket.send_msg_to_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], 'close') - ################################################################################################################ - # main thread: Profiler.stop - ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - cpu_profiler.stop(), message_id) - response = json.loads(response) - assert response['id'] == message_id - assert all(i >= 0 for i in response['result']['profile']['timeDeltas']) + await self.profiler_impl.send("Profiler.stop", worker_thread_1) + await self.profiler_impl.send("Profiler.stop", worker_thread_2) + ################################################################################################################ + # main thread: Profiler.stop + ################################################################################################################ + await self.profiler_impl.send("Profiler.stop", main_thread) ################################################################################################################ # close the websocket connections ################################################################################################################ - await websocket.send_msg_to_debugger_server(main_thread_instance_id, main_thread_to_send_queue, 'close') + await websocket.send_msg_to_debugger_server(worker_thread_1.instance_id, worker_thread_1.send_msg_queue, 'close') + await websocket.send_msg_to_debugger_server(worker_thread_2.instance_id, worker_thread_2.send_msg_queue, 'close') + await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') await websocket.send_msg_to_connect_server('close') ################################################################################################################ \ No newline at end of file diff --git a/test/autotest/scenario_test/test_debug_01.py b/test/autotest/scenario_test/test_debug_01.py index c3eadbe39be47a75fb7d1fc20b02b5299a815f9d..c2892df9d66376c1b331f5b203be20656feda37e 100644 --- a/test/autotest/scenario_test/test_debug_01.py +++ b/test/autotest/scenario_test/test_debug_01.py @@ -71,8 +71,6 @@ class TestDebug01: self.hilog_process, self.write_thread = Utils.save_hilog(log_path=self.log_path, file_name=self.hilog_file_name, debug_on=True) - self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator) - self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator) def teardown_method(self): Application.uninstall(self.config['bundle_name']) @@ -93,8 +91,10 @@ class TestDebug01: websocket = self.config['websocket'] taskpool = self.config['taskpool'] pid = self.config['pid'] + self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) + self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) - taskpool.submit(websocket.main_task(taskpool, websocket, self.procedure, pid)) + taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) taskpool.await_taskpool() taskpool.task_join() if taskpool.task_exception: @@ -104,7 +104,7 @@ class TestDebug01: ################################################################################################################ # main thread: connect the debugger server ################################################################################################################ - main_thread = await self.debugger_impl.connect_to_debugger_server(websocket, self.config['pid'], True) + main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') ################################################################################################################ # main thread: Runtime.enable @@ -113,7 +113,7 @@ class TestDebug01: ################################################################################################################ # main thread: Debugger.enable ################################################################################################################ - await self.debugger_impl.send("Debugger.enable", main_thread, websocket) + await self.debugger_impl.send("Debugger.enable", main_thread) ################################################################################################################ # main thread: Runtime.runIfWaitingForDebugger ################################################################################################################ @@ -121,36 +121,36 @@ class TestDebug01: ################################################################################################################ # main thread: Debugger.scriptParsed ################################################################################################################ - response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread, websocket) + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) assert response['params']['url'] == self.config['file_path']['entry_ability'] assert response['params']['endLine'] == 0 ################################################################################################################ # main thread: Debugger.paused ################################################################################################################ - response = await self.debugger_impl.recv("Debugger.paused", main_thread, websocket) + response = await self.debugger_impl.recv("Debugger.paused", main_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['entry_ability'] assert response['params']['reason'] == 'Break on start' ################################################################################################################ # main thread: Debugger.resume ################################################################################################################ - await self.debugger_impl.send("Debugger.resume", main_thread, websocket) + await self.debugger_impl.send("Debugger.resume", main_thread) ################################################################################################################ # main thread: Debugger.scriptParsed ################################################################################################################ - response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread, websocket) + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) assert response['params']['url'] == self.config['file_path']['index'] assert response['params']['endLine'] == 0 ################################################################################################################ # main thread: Debugger.paused ################################################################################################################ - response = await self.debugger_impl.recv("Debugger.paused", main_thread, websocket) + response = await self.debugger_impl.recv("Debugger.paused", main_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['reason'] == 'Break on start' ################################################################################################################ # main thread: Debugger.removeBreakpointsByUrl ################################################################################################################ params = debugger.RemoveBreakpointsUrl(self.config['file_path']['index']) - await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", main_thread, websocket, params) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", main_thread, params) ################################################################################################################ # main thread: Debugger.getPossibleAndSetBreakpointByUrl ################################################################################################################ @@ -159,40 +159,40 @@ class TestDebug01: debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=57)] params = debugger.SetBreakpointsLocations(locations) response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", - main_thread, websocket, params) + main_thread, params) assert response['result']['locations'][0]['id'] == 'id:12:0:' + self.config['file_path']['index'] assert response['result']['locations'][1]['id'] == 'id:53:0:' + self.config['file_path']['index'] assert response['result']['locations'][2]['id'] == 'id:57:0:' + self.config['file_path']['index'] ################################################################################################################ # main thread: Debugger.resume ################################################################################################################ - await self.debugger_impl.send("Debugger.resume", main_thread, websocket) + await self.debugger_impl.send("Debugger.resume", main_thread) ################################################################################################################ # main thread: Debugger.paused, hit breakpoint ################################################################################################################ - response = await self.debugger_impl.recv("Debugger.paused", main_thread, websocket) + response = await self.debugger_impl.recv("Debugger.paused", main_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['hitBreakpoints'] == ['id:12:4:' + self.config['file_path']['index']] ################################################################################################################ # main thread: Debugger.stepOut ################################################################################################################ - await self.debugger_impl.send("Debugger.stepOut", main_thread, websocket) - response = await self.debugger_impl.recv("Debugger.paused", main_thread, websocket) + await self.debugger_impl.send("Debugger.stepOut", main_thread) + response = await self.debugger_impl.recv("Debugger.paused", main_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['hitBreakpoints'] == ['id:12:4:' + self.config['file_path']['index']] ################################################################################################################ # worker thread: connect the debugger server ################################################################################################################ - worker_thread_1 = await self.debugger_impl.connect_to_debugger_server(websocket, self.config['pid'], False) + worker_thread_1 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) logging.info(f'Connect to the debugger server of instance: {worker_thread_1.instance_id}') ################################################################################################################ # main thread: Debugger.resume ################################################################################################################ - await self.debugger_impl.send("Debugger.resume", main_thread, websocket) + await self.debugger_impl.send("Debugger.resume", main_thread) ################################################################################################################ # worker thread: connect the debugger server ################################################################################################################ - worker_thread_2 = await self.debugger_impl.connect_to_debugger_server(websocket, self.config['pid'], False) + worker_thread_2 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) logging.info(f'Connect to the debugger server of instance: {worker_thread_2.instance_id}') ################################################################################################################ # worker thread: Runtime.enable @@ -202,53 +202,53 @@ class TestDebug01: ################################################################################################################ # worker thread: Debugger.enable ################################################################################################################ - await self.debugger_impl.send("Debugger.enable", worker_thread_1, websocket) - await self.debugger_impl.send("Debugger.enable", worker_thread_2, websocket) + await self.debugger_impl.send("Debugger.enable", worker_thread_1) + await self.debugger_impl.send("Debugger.enable", worker_thread_2) ################################################################################################################ # worker thread: Runtime.runIfWaitingForDebugger ################################################################################################################ - # worker1 thread: Runtime.runIfWaitingForDebugger + # worker thread 1: Runtime.runIfWaitingForDebugger await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", worker_thread_1) - # worker1 thread: Debugger.scriptParsed - response = await self.debugger_impl.recv("Debugger.scriptParsed", worker_thread_1, websocket) + # worker thread 1: Debugger.scriptParsed + response = await self.debugger_impl.recv("Debugger.scriptParsed", worker_thread_1) assert response['params']['url'] == self.config['file_path']['worker'] assert response['params']['endLine'] == 0 - # worker1 thread: Debugger.paused - response = await self.debugger_impl.recv("Debugger.paused", worker_thread_1, websocket) + # worker thread 1: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread_1) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] assert response['params']['reason'] == 'Break on start' - # worker2 thread: Runtime.runIfWaitingForDebugger + # worker thread 2: Runtime.runIfWaitingForDebugger await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", worker_thread_2) - # worker2 thread: Debugger.scriptParsed - response = await self.debugger_impl.recv("Debugger.scriptParsed", worker_thread_2, websocket) + # worker thread 2: Debugger.scriptParsed + response = await self.debugger_impl.recv("Debugger.scriptParsed", worker_thread_2) assert response['params']['url'] == self.config['file_path']['worker'] assert response['params']['endLine'] == 0 - # worker2 thread: Debugger.paused - response = await self.debugger_impl.recv("Debugger.paused", worker_thread_2, websocket) + # worker thread 2: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread_2) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] assert response['params']['reason'] == 'Break on start' ################################################################################################################ # worker thread: Debugger.removeBreakpointsByUrl ################################################################################################################ params = debugger.RemoveBreakpointsUrl(self.config['file_path']['worker']) - await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", worker_thread_1, websocket, params) - await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", worker_thread_2, websocket, params) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", worker_thread_1, params) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", worker_thread_2, params) ################################################################################################################ # worker thread: Debugger.getPossibleAndSetBreakpointByUrl ################################################################################################################ locations = [debugger.BreakLocationUrl(url=self.config['file_path']['worker'], line_number=11)] params = debugger.SetBreakpointsLocations(locations) response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", - worker_thread_1, websocket, params) + worker_thread_1, params) assert response['result']['locations'][0]['id'] == 'id:11:0:' + self.config['file_path']['worker'] response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", - worker_thread_2, websocket, params) + worker_thread_2, params) assert response['result']['locations'][0]['id'] == 'id:11:0:' + self.config['file_path']['worker'] ################################################################################################################ # worker thread: Debugger.resume ################################################################################################################ - await self.debugger_impl.send("Debugger.resume", worker_thread_1, websocket) - await self.debugger_impl.send("Debugger.resume", worker_thread_2, websocket) + await self.debugger_impl.send("Debugger.resume", worker_thread_1) + await self.debugger_impl.send("Debugger.resume", worker_thread_2) ################################################################################################################ # main thread: click on the screen ################################################################################################################ @@ -256,20 +256,20 @@ class TestDebug01: ################################################################################################################ # main thread: Debugger.paused, hit breakpoint ################################################################################################################ - response = await self.debugger_impl.recv("Debugger.paused", main_thread, websocket) + response = await self.debugger_impl.recv("Debugger.paused", main_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['hitBreakpoints'] == ['id:53:16:' + self.config['file_path']['index']] ################################################################################################################ # worker thread: destroy instance ################################################################################################################ # worker thread 2 destroyed - response = await self.debugger_impl.destroy_instance(websocket) - assert response['type'] == 'destroyInstance' + response = await self.debugger_impl.destroy_instance() + assert response['instanceId'] == worker_thread_2.instance_id ################################################################################################################ # main thread: Debugger.stepInto ################################################################################################################ - await self.debugger_impl.send("Debugger.stepInto", main_thread, websocket) - await self.debugger_impl.recv("Debugger.paused", main_thread, websocket) + await self.debugger_impl.send("Debugger.stepInto", main_thread) + await self.debugger_impl.recv("Debugger.paused", main_thread) ################################################################################################################ # main thread: Runtime.getProperties ################################################################################################################ @@ -282,17 +282,17 @@ class TestDebug01: ################################################################################################################ # main thread: Debugger.resume ################################################################################################################ - await self.debugger_impl.send("Debugger.resume", main_thread, websocket) + await self.debugger_impl.send("Debugger.resume", main_thread) ################################################################################################################ # main thread: Debugger.paused, hit breakpoint ################################################################################################################ - response = await self.debugger_impl.recv("Debugger.paused", main_thread, websocket) + response = await self.debugger_impl.recv("Debugger.paused", main_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['hitBreakpoints'] == ['id:57:20:' + self.config['file_path']['index']] ################################################################################################################ # worker thread: connect the debugger server ################################################################################################################ - worker_thread_2 = await self.debugger_impl.connect_to_debugger_server(websocket, self.config['pid'], False) + worker_thread_2 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) logging.info(f'Connect to the debugger server of instance: {worker_thread_2.instance_id}') ################################################################################################################ # worker thread: Runtime.enable @@ -301,45 +301,47 @@ class TestDebug01: ################################################################################################################ # worker thread: Debugger.enable ################################################################################################################ - await self.debugger_impl.send("Debugger.enable", worker_thread_2, websocket) + await self.debugger_impl.send("Debugger.enable", worker_thread_2) ################################################################################################################ # worker thread: Runtime.runIfWaitingForDebugger ################################################################################################################ await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", worker_thread_2) - response = await self.debugger_impl.recv("Debugger.scriptParsed", worker_thread_2, websocket) + # worker thread: Debugger.scriptParsed + response = await self.debugger_impl.recv("Debugger.scriptParsed", worker_thread_2) assert response['params']['url'] == self.config['file_path']['worker'] assert response['params']['endLine'] == 0 - response = await self.debugger_impl.recv("Debugger.paused", worker_thread_2, websocket) + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread_2) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] assert response['params']['reason'] == 'Break on start' ################################################################################################################ # worker thread: Debugger.removeBreakpointsByUrl ################################################################################################################ params = debugger.RemoveBreakpointsUrl(self.config['file_path']['worker']) - await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", worker_thread_2, websocket, params) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", worker_thread_2, params) ################################################################################################################ # worker thread: Debugger.getPossibleAndSetBreakpointByUrl ################################################################################################################ locations = [debugger.BreakLocationUrl(url=self.config['file_path']['worker'], line_number=11)] params = debugger.SetBreakpointsLocations(locations) response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", - worker_thread_2, websocket, params) + worker_thread_2, params) assert response['result']['locations'][0]['id'] == 'id:11:0:' + self.config['file_path']['worker'] ################################################################################################################ # worker thread: Debugger.resume ################################################################################################################ - await self.debugger_impl.send("Debugger.resume", worker_thread_2, websocket) + await self.debugger_impl.send("Debugger.resume", worker_thread_2) ################################################################################################################ - # main thread: Debugger.resume + # main thread: Debugger.stepOut ################################################################################################################ - await self.debugger_impl.send("Debugger.resume", main_thread, websocket) + await self.debugger_impl.send("Debugger.stepOut", main_thread) # main thread: Debugger.paused - response = await self.debugger_impl.recv("Debugger.paused", main_thread, websocket) + response = await self.debugger_impl.recv("Debugger.paused", main_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['reason'] == 'other' assert response['params']['hitBreakpoints'] == ['id:57:20:' + self.config['file_path']['index']] # worker thread: Debugger.paused - response = await self.debugger_impl.recv("Debugger.paused", worker_thread_1, websocket) + response = await self.debugger_impl.recv("Debugger.paused", worker_thread_1) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] assert response['params']['reason'] == 'other' assert response['params']['hitBreakpoints'] == ['id:11:4:' + self.config['file_path']['worker']] @@ -355,50 +357,48 @@ class TestDebug01: ################################################################################################################ # worker thread: Debugger.stepOut ################################################################################################################ - await self.debugger_impl.send("Debugger.stepOut", worker_thread_1, websocket) + await self.debugger_impl.send("Debugger.stepOut", worker_thread_1) ################################################################################################################ # worker thread: Debugger.disable ################################################################################################################ - await self.debugger_impl.send("Debugger.disable", worker_thread_1, websocket) + await self.debugger_impl.send("Debugger.disable", worker_thread_1) ################################################################################################################ - # main thread: Debugger.stepOver + # main thread: Debugger.stepInto ################################################################################################################ - await self.debugger_impl.send("Debugger.stepOver", main_thread, websocket) + await self.debugger_impl.send("Debugger.stepInto", main_thread) # main thread: Debugger.paused - response = await self.debugger_impl.recv("Debugger.paused", main_thread, websocket) + response = await self.debugger_impl.recv("Debugger.paused", main_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['reason'] == 'other' assert response['params']['hitBreakpoints'] == [] # worker thread: Debugger.paused - response = await self.debugger_impl.recv("Debugger.paused", worker_thread_2, websocket) + response = await self.debugger_impl.recv("Debugger.paused", worker_thread_2) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] assert response['params']['reason'] == 'other' assert response['params']['hitBreakpoints'] == ['id:11:4:' + self.config['file_path']['worker']] ################################################################################################################ # worker thread: Debugger.resume ################################################################################################################ - await self.debugger_impl.send("Debugger.resume", worker_thread_2, websocket) + await self.debugger_impl.send("Debugger.resume", worker_thread_2) ################################################################################################################ # worker thread: Debugger.disable ################################################################################################################ - await self.debugger_impl.send("Debugger.disable", worker_thread_2, websocket) + await self.debugger_impl.send("Debugger.disable", worker_thread_2) ################################################################################################################ # main thread: Debugger.resume ################################################################################################################ - await self.debugger_impl.send("Debugger.resume", main_thread, websocket) + await self.debugger_impl.send("Debugger.resume", main_thread) ################################################################################################################ # worker thread: destroy instance ################################################################################################################ - response = await self.debugger_impl.destroy_instance(websocket) - assert response['type'] == 'destroyInstance' + response = await self.debugger_impl.destroy_instance() assert response['instanceId'] != self.config['pid'] - response = await self.debugger_impl.destroy_instance(websocket) - assert response['type'] == 'destroyInstance' + response = await self.debugger_impl.destroy_instance() assert response['instanceId'] != self.config['pid'] ################################################################################################################ # main thread: Debugger.disable ################################################################################################################ - await self.debugger_impl.send("Debugger.disable", main_thread, websocket) + await self.debugger_impl.send("Debugger.disable", main_thread) ################################################################################################################ # close the websocket connections ################################################################################################################ diff --git a/test/autotest/scenario_test/test_debug_02.py b/test/autotest/scenario_test/test_debug_02.py index e039a20975a32d25183546e193ae27000bf49fa5..32c18387c5aa9733d4c26fa02b4682df13e8bc31 100644 --- a/test/autotest/scenario_test/test_debug_02.py +++ b/test/autotest/scenario_test/test_debug_02.py @@ -26,8 +26,8 @@ import pytest from aw import Application from aw import Utils -from aw import communicate_with_debugger_server -from aw import debugger, runtime +from aw import debugger +from aw.api import debugger_api, runtime_api @pytest.mark.debug @@ -86,9 +86,11 @@ class TestDebug02: websocket = self.config['websocket'] taskpool = self.config['taskpool'] pid = self.config['pid'] + self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) + self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) Application.attach(self.config['bundle_name']) - taskpool.submit(websocket.main_task(taskpool, websocket, self.procedure, pid)) + taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) taskpool.await_taskpool() taskpool.task_join() if taskpool.task_exception: @@ -98,122 +100,55 @@ class TestDebug02: ################################################################################################################ # main thread: connect the debugger server ################################################################################################################ - send_msg = {"type": "connected"} - await websocket.send_msg_to_connect_server(send_msg) - response = await websocket.recv_msg_of_connect_server() - response = json.loads(response) - assert response['type'] == 'addInstance' - assert response['instanceId'] == 0, logging.error('instance id of the main thread not equal to 0') - assert response['tid'] == self.config['pid'] - main_thread_instance_id = await websocket.get_instance() - main_thread_to_send_queue = websocket.to_send_msg_queues[main_thread_instance_id] - main_thread_received_queue = websocket.received_msg_queues[main_thread_instance_id] - logging.info(f'Connect to the debugger server of instance: {main_thread_instance_id}') + main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) + logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') ################################################################################################################ # worker thread: connect the debugger server ################################################################################################################ - workers_num = 2 - worker_instances_id = [] - worker_thread_to_send_queues = [] - worker_thread_received_queues = [] - for i in range(workers_num): - response = await websocket.recv_msg_of_connect_server() - response = json.loads(response) - assert response['type'] == 'addInstance' - assert response['instanceId'] != 0 - assert response['tid'] != self.config['pid'] - assert 'workerThread_' in response['name'] - worker_instance_id = await websocket.get_instance() - worker_instances_id.append(worker_instance_id) - worker_thread_to_send_queues.append(websocket.to_send_msg_queues[worker_instance_id]) - worker_thread_received_queues.append(websocket.received_msg_queues[worker_instance_id]) - logging.info(f'Connect to the debugger server of instance: {worker_instance_id}') + worker_thread_1 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread_1.instance_id}') + worker_thread_2 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread_2.instance_id}') ################################################################################################################ # worker thread: Runtime.enable ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - runtime.enable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {"protocols": []}} + await self.runtime_impl.send("Runtime.enable", worker_thread_1) + await self.runtime_impl.send("Runtime.enable", worker_thread_2) ################################################################################################################ # worker thread: Debugger.enable ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - debugger.enable(), message_id) - response = json.loads(response) - assert response['method'] == 'Debugger.scriptParsed' - assert response['params']['url'] == self.config['file_path']['worker'] - assert response['params']['endLine'] == 0 - response = await websocket.recv_msg_of_debugger_server(worker_instances_id[i], - worker_thread_received_queues[i]) - assert json.loads(response) == {"id": message_id, "result": {"debuggerId": "0", - "protocols": Utils.get_custom_protocols()}} + await self.debugger_impl.send("Debugger.enable", worker_thread_1) + await self.debugger_impl.send("Debugger.enable", worker_thread_2) ################################################################################################################ # worker thread: Runtime.runIfWaitingForDebugger ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - runtime.run_if_waiting_for_debugger(), message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", worker_thread_1) + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", worker_thread_2) + ################################################################################################################ + # main thread: Runtime.enable + ################################################################################################################ + await self.runtime_impl.send("Runtime.enable", main_thread) ################################################################################################################ # main thread: Debugger.enable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.enable(), message_id) - assert json.loads(response)['method'] == 'Debugger.scriptParsed' - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - assert json.loads(response)['method'] == 'Debugger.scriptParsed' - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - assert json.loads(response) == {"id": message_id, "result": {"debuggerId": "0", - "protocols": Utils.get_custom_protocols()}} + await self.debugger_impl.send("Debugger.enable", main_thread) ################################################################################################################ # main thread: Runtime.runIfWaitingForDebugger ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - runtime.run_if_waiting_for_debugger(), message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", main_thread) ################################################################################################################ # main thread: Debugger.removeBreakpointsByUrl ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.remove_breakpoints_by_url( - self.config['file_path']['index']), - message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + params = debugger.RemoveBreakpointsUrl(self.config['file_path']['index']) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", main_thread, params) ################################################################################################################ # main thread: Debugger.getPossibleAndSetBreakpointByUrl ################################################################################################################ - message_id = next(self.id_generator) locations = [debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=53), debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=57)] - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.get_possible_and_set_breakpoint_by_url(locations), - message_id) - response = json.loads(response) - assert response['id'] == message_id + params = debugger.SetBreakpointsLocations(locations) + response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", + main_thread, params) assert response['result']['locations'][0]['id'] == 'id:53:0:' + self.config['file_path']['index'] assert response['result']['locations'][1]['id'] == 'id:57:0:' + self.config['file_path']['index'] ################################################################################################################ @@ -223,315 +158,149 @@ class TestDebug02: ################################################################################################################ # main thread: Debugger.paused, hit breakpoint ################################################################################################################ - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - response = json.loads(response) - assert response['method'] == 'Debugger.paused' + response = await self.debugger_impl.recv("Debugger.paused", main_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['hitBreakpoints'] == ['id:53:16:' + self.config['file_path']['index']] ################################################################################################################ # worker thread: destroy instance ################################################################################################################ - response = await websocket.recv_msg_of_connect_server() - response = json.loads(response) - assert response['type'] == 'destroyInstance' - if response['instanceId'] == worker_instances_id[0]: - worker_instances_id[0] = worker_instances_id[1] - worker_thread_to_send_queues[0] = worker_thread_to_send_queues[1] - worker_thread_received_queues[0] = worker_thread_received_queues[1] + response = await self.debugger_impl.destroy_instance() + if response['instanceId'] == worker_thread_1.instance_id: + worker_thread_1 = worker_thread_2 ################################################################################################################ # worker thread: Debugger.removeBreakpointsByUrl ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[0], - worker_thread_to_send_queues[0], - worker_thread_received_queues[0], - debugger.remove_breakpoints_by_url( - self.config['file_path']['worker']), - message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + params = debugger.RemoveBreakpointsUrl(self.config['file_path']['worker']) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", worker_thread_1, params) ################################################################################################################ # worker thread: Debugger.getPossibleAndSetBreakpointByUrl ################################################################################################################ - message_id = next(self.id_generator) locations = [debugger.BreakLocationUrl(url=self.config['file_path']['worker'], line_number=11)] - response = await communicate_with_debugger_server(worker_instances_id[0], - worker_thread_to_send_queues[0], - worker_thread_received_queues[0], - debugger.get_possible_and_set_breakpoint_by_url(locations), - message_id) - response = json.loads(response) - assert response['id'] == message_id + params = debugger.SetBreakpointsLocations(locations) + response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", + worker_thread_1, params) assert response['result']['locations'][0]['id'] == 'id:11:0:' + self.config['file_path']['worker'] ################################################################################################################ # main thread: Debugger.resume ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.resume(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - assert json.loads(response) == {"id": message_id, "result": {}} - ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) # main thread: Debugger.paused, hit breakpoint - ################################################################################################################ - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - response = json.loads(response) - assert response['method'] == 'Debugger.paused' + response = await self.debugger_impl.recv("Debugger.paused", main_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['hitBreakpoints'] == ['id:57:20:' + self.config['file_path']['index']] ################################################################################################################ # worker thread: connect the debugger server ################################################################################################################ - response = await websocket.recv_msg_of_connect_server() - response = json.loads(response) - assert response['type'] == 'addInstance' - assert response['instanceId'] != 0 - assert response['tid'] != self.config['pid'] - assert 'workerThread_' in response['name'] - worker_instances_id[1] = await websocket.get_instance() - worker_thread_to_send_queues[1] = websocket.to_send_msg_queues[worker_instances_id[1]] - worker_thread_received_queues[1] = websocket.received_msg_queues[worker_instances_id[1]] - logging.info(f'Connect to the debugger server of instance: {worker_instances_id[1]}') + worker_thread_2 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread_2.instance_id}') ################################################################################################################ # worker thread: Runtime.enable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[1], - worker_thread_to_send_queues[1], - worker_thread_received_queues[1], - runtime.enable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {"protocols": []}} + await self.runtime_impl.send("Runtime.enable", worker_thread_2) ################################################################################################################ # worker thread: Debugger.enable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[1], - worker_thread_to_send_queues[1], - worker_thread_received_queues[1], - debugger.enable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {"debuggerId": "0", - "protocols": Utils.get_custom_protocols()}} + await self.debugger_impl.send("Debugger.enable", worker_thread_2) ################################################################################################################ # worker thread: Runtime.runIfWaitingForDebugger ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[1], - worker_thread_to_send_queues[1], - worker_thread_received_queues[1], - runtime.run_if_waiting_for_debugger(), message_id) - assert json.loads(response) == {"id": message_id, "result": {}} - response = await websocket.recv_msg_of_debugger_server(worker_instances_id[1], - worker_thread_received_queues[1]) - response = json.loads(response) - assert response['method'] == 'Debugger.scriptParsed' + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", worker_thread_2) + # worker thread: Debugger.scriptParsed + response = await self.debugger_impl.recv("Debugger.scriptParsed", worker_thread_2) assert response['params']['url'] == self.config['file_path']['worker'] assert response['params']['endLine'] == 0 + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread_2) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'Break on start' ################################################################################################################ # worker thread: Debugger.removeBreakpointsByUrl ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[1], - worker_thread_to_send_queues[1], - worker_thread_received_queues[1], - debugger.remove_breakpoints_by_url( - self.config['file_path']['worker']), - message_id) - response = json.loads(response) - assert response['method'] == 'Debugger.paused' - assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] - assert response['params']['reason'] == 'Break on start' - response = await websocket.recv_msg_of_debugger_server(worker_instances_id[1], - worker_thread_received_queues[1]) - assert json.loads(response) == {"id": message_id, "result": {}} + params = debugger.RemoveBreakpointsUrl(self.config['file_path']['worker']) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", worker_thread_2, params) ################################################################################################################ # worker thread: Debugger.getPossibleAndSetBreakpointByUrl ################################################################################################################ - message_id = next(self.id_generator) locations = [debugger.BreakLocationUrl(url=self.config['file_path']['worker'], line_number=11)] - response = await communicate_with_debugger_server(worker_instances_id[1], - worker_thread_to_send_queues[1], - worker_thread_received_queues[1], - debugger.get_possible_and_set_breakpoint_by_url(locations), - message_id) - response = json.loads(response) - assert response['id'] == message_id + params = debugger.SetBreakpointsLocations(locations) + response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", + worker_thread_2, params) assert response['result']['locations'][0]['id'] == 'id:11:0:' + self.config['file_path']['worker'] ################################################################################################################ # worker thread: Debugger.resume ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[1], - worker_thread_to_send_queues[1], - worker_thread_received_queues[1], - debugger.resume(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(worker_instances_id[1], - worker_thread_received_queues[1]) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.resume", worker_thread_2) ################################################################################################################ # main thread: Debugger.resume ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.resume(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.resume", main_thread) # main thread: Debugger.paused - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - response = json.loads(response) - assert response['method'] == 'Debugger.paused' + response = await self.debugger_impl.recv("Debugger.paused", main_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['reason'] == 'other' assert response['params']['hitBreakpoints'] == ['id:57:20:' + self.config['file_path']['index']] # worker thread: Debugger.paused - response = await websocket.recv_msg_of_debugger_server(worker_instances_id[0], - worker_thread_received_queues[0]) - response = json.loads(response) - assert response['method'] == 'Debugger.paused' + response = await self.debugger_impl.recv("Debugger.paused", worker_thread_1) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] assert response['params']['reason'] == 'Break on start' assert response['params']['hitBreakpoints'] == [] ################################################################################################################ # worker thread: Debugger.resume ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[0], - worker_thread_to_send_queues[0], - worker_thread_received_queues[0], - debugger.resume(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(worker_instances_id[0], - worker_thread_received_queues[0]) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.resume", worker_thread_1) # worker thread: Debugger.paused - response = await websocket.recv_msg_of_debugger_server(worker_instances_id[0], - worker_thread_received_queues[0]) - response = json.loads(response) - assert response['method'] == 'Debugger.paused' + response = await self.debugger_impl.recv("Debugger.paused", worker_thread_1) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] assert response['params']['reason'] == 'other' assert response['params']['hitBreakpoints'] == ['id:11:4:' + self.config['file_path']['worker']] ################################################################################################################ # worker thread: Debugger.stepOut ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[0], - worker_thread_to_send_queues[0], - worker_thread_received_queues[0], - debugger.step_out(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(worker_instances_id[0], - worker_thread_received_queues[0]) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.stepOut", worker_thread_1) ################################################################################################################ # worker thread: Debugger.disable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[0], - worker_thread_to_send_queues[0], - worker_thread_received_queues[0], - debugger.disable(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(worker_instances_id[0], - worker_thread_received_queues[0]) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.disable", worker_thread_1) ################################################################################################################ # main thread: Debugger.stepOver ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.step_over(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.stepOver", main_thread) # main thread: Debugger.paused - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - response = json.loads(response) - assert response['method'] == 'Debugger.paused' + response = await self.debugger_impl.recv("Debugger.paused", main_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['reason'] == 'other' assert response['params']['hitBreakpoints'] == [] # worker thread: Debugger.paused - response = await websocket.recv_msg_of_debugger_server(worker_instances_id[1], - worker_thread_received_queues[1]) - response = json.loads(response) - assert response['method'] == 'Debugger.paused' + response = await self.debugger_impl.recv("Debugger.paused", worker_thread_2) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] assert response['params']['reason'] == 'other' assert response['params']['hitBreakpoints'] == ['id:11:4:' + self.config['file_path']['worker']] ################################################################################################################ # worker thread: Debugger.resume ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[1], - worker_thread_to_send_queues[1], - worker_thread_received_queues[1], - debugger.resume(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(worker_instances_id[1], - worker_thread_received_queues[1]) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.resume", worker_thread_2) ################################################################################################################ # worker thread: Debugger.disable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[1], - worker_thread_to_send_queues[1], - worker_thread_received_queues[1], - debugger.disable(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(worker_instances_id[1], - worker_thread_received_queues[1]) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.disable", worker_thread_2) ################################################################################################################ # main thread: Debugger.resume ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.resume(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.resume", main_thread) ################################################################################################################ # worker thread: destroy instance ################################################################################################################ - for i in range(workers_num): - response = await websocket.recv_msg_of_connect_server() - response = json.loads(response) - assert response['type'] == 'destroyInstance' - assert response['instanceId'] in worker_instances_id + response = await self.debugger_impl.destroy_instance() + assert response['instanceId'] != self.config['pid'] + response = await self.debugger_impl.destroy_instance() + assert response['instanceId'] != self.config['pid'] ################################################################################################################ # main thread: Debugger.disable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.disable(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.disable", main_thread) ################################################################################################################ # close the websocket connections ################################################################################################################ - await websocket.send_msg_to_debugger_server(main_thread_instance_id, main_thread_to_send_queue, 'close') + await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') await websocket.send_msg_to_connect_server('close') ################################################################################################################ \ No newline at end of file diff --git a/test/autotest/scenario_test/test_debug_03.py b/test/autotest/scenario_test/test_debug_03.py index 0c631b865ad4baa43b62231bf30780b67b198ec0..bbc8edf2b9ec061a0d0ebbd8f17cffa9fbff25dc 100644 --- a/test/autotest/scenario_test/test_debug_03.py +++ b/test/autotest/scenario_test/test_debug_03.py @@ -17,7 +17,6 @@ limitations under the License. Description: Scenario test case. """ -import json import logging import os import time @@ -26,8 +25,8 @@ import pytest from aw import Application from aw import Utils -from aw import communicate_with_debugger_server from aw import debugger, runtime +from aw.api import debugger_api, runtime_api @pytest.mark.debug @@ -84,8 +83,10 @@ class TestDebug03: websocket = self.config['websocket'] taskpool = self.config['taskpool'] pid = self.config['pid'] + self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) + self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) - taskpool.submit(websocket.main_task(taskpool, websocket, self.procedure, pid)) + taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) taskpool.await_taskpool() taskpool.task_join() if taskpool.task_exception: @@ -95,171 +96,83 @@ class TestDebug03: ################################################################################################################ # main thread: connect the debugger server ################################################################################################################ - send_msg = {"type": "connected"} - await websocket.send_msg_to_connect_server(send_msg) - response = await websocket.recv_msg_of_connect_server() - response = json.loads(response) - assert response['type'] == 'addInstance' - assert response['instanceId'] == 0, logging.error('instance id of the main thread not equal to 0') - assert response['tid'] == self.config['pid'] - main_thread_instance_id = await websocket.get_instance() - main_thread_to_send_queue = websocket.to_send_msg_queues[main_thread_instance_id] - main_thread_received_queue = websocket.received_msg_queues[main_thread_instance_id] - logging.info(f'Connect to the debugger server of instance: {main_thread_instance_id}') + main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) + logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') ################################################################################################################ # main thread: Runtime.enable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - runtime.enable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {"protocols": []}} + await self.runtime_impl.send("Runtime.enable", main_thread) ################################################################################################################ # main thread: Debugger.enable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.enable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {"debuggerId": "0", - "protocols": Utils.get_custom_protocols()}} + await self.debugger_impl.send("Debugger.enable", main_thread) ################################################################################################################ # main thread: Runtime.runIfWaitingForDebugger ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - runtime.run_if_waiting_for_debugger(), message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", main_thread) ################################################################################################################ # main thread: Debugger.scriptParsed ################################################################################################################ - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - response = json.loads(response) - assert response['method'] == 'Debugger.scriptParsed' + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) assert response['params']['url'] == self.config['file_path']['entry_ability'] assert response['params']['endLine'] == 0 ################################################################################################################ # main thread: Debugger.paused ################################################################################################################ - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - response = json.loads(response) - assert response['method'] == 'Debugger.paused' + response = await self.debugger_impl.recv("Debugger.paused", main_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['entry_ability'] assert response['params']['reason'] == 'Break on start' ################################################################################################################ # main thread: Debugger.resume ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.resume(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.resume", main_thread) ################################################################################################################ # main thread: Debugger.scriptParsed ################################################################################################################ - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - response = json.loads(response) - assert response['method'] == 'Debugger.scriptParsed' + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) assert response['params']['url'] == self.config['file_path']['index'] assert response['params']['endLine'] == 0 ################################################################################################################ # main thread: Debugger.paused ################################################################################################################ - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - response = json.loads(response) - assert response['method'] == 'Debugger.paused' + response = await self.debugger_impl.recv("Debugger.paused", main_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['reason'] == 'Break on start' ################################################################################################################ # main thread: Debugger.resume ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.resume(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.resume", main_thread) ################################################################################################################ # worker thread: connect the debugger server ################################################################################################################ - response = await websocket.recv_msg_of_connect_server() - response = json.loads(response) - assert response['type'] == 'addInstance' - assert response['instanceId'] != 0 - assert response['tid'] != self.config['pid'] - assert 'workerThread_' in response['name'] - worker_instance_id = await websocket.get_instance() - worker_thread_to_send_queue = websocket.to_send_msg_queues[worker_instance_id] - worker_thread_received_queue = websocket.received_msg_queues[worker_instance_id] - logging.info(f'Connect to the debugger server of instance: {worker_instance_id}') + worker_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread.instance_id}') ################################################################################################################ # worker thread: Runtime.enable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instance_id, - worker_thread_to_send_queue, - worker_thread_received_queue, - runtime.enable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {"protocols": []}} + await self.runtime_impl.send("Runtime.enable", worker_thread) ################################################################################################################ # worker thread: Debugger.enable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instance_id, - worker_thread_to_send_queue, - worker_thread_received_queue, - debugger.enable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {"debuggerId": "0", - "protocols": Utils.get_custom_protocols()}} + await self.debugger_impl.send("Debugger.enable", worker_thread) ################################################################################################################ # worker thread: Runtime.runIfWaitingForDebugger ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instance_id, - worker_thread_to_send_queue, - worker_thread_received_queue, - runtime.run_if_waiting_for_debugger(), message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", worker_thread) ################################################################################################################ # main thread: Debugger.removeBreakpointsByUrl ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.remove_breakpoints_by_url( - self.config['file_path']['index']), - message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + params = debugger.RemoveBreakpointsUrl(self.config['file_path']['index']) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", main_thread, params) ################################################################################################################ # main thread: Debugger.getPossibleAndSetBreakpointByUrl ################################################################################################################ - message_id = next(self.id_generator) locations = [debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=10), debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=17), debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=25)] - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.get_possible_and_set_breakpoint_by_url(locations), - message_id) - response = json.loads(response) - assert response['id'] == message_id + params = debugger.SetBreakpointsLocations(locations) + response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", + main_thread, params) assert response['result']['locations'][0]['id'] == 'id:10:0:' + self.config['file_path']['index'] assert response['result']['locations'][1]['id'] == 'id:17:0:' + self.config['file_path']['index'] assert response['result']['locations'][2]['id'] == 'id:25:0:' + self.config['file_path']['index'] @@ -270,118 +183,61 @@ class TestDebug03: ################################################################################################################ # worker thread: Debugger.scriptParsed ################################################################################################################ - response = await websocket.recv_msg_of_debugger_server(worker_instance_id, - worker_thread_received_queue) - response = json.loads(response) - assert response['method'] == 'Debugger.scriptParsed' + response = await self.debugger_impl.recv("Debugger.scriptParsed", worker_thread) assert response['params']['url'] == self.config['file_path']['index'] assert response['params']['endLine'] == 0 # worker thread: Debugger.paused - response = await websocket.recv_msg_of_debugger_server(worker_instance_id, - worker_thread_received_queue) - response = json.loads(response) - assert response['method'] == 'Debugger.paused' + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['reason'] == 'Break on start' ################################################################################################################ # worker thread: Debugger.removeBreakpointsByUrl ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instance_id, - worker_thread_to_send_queue, - worker_thread_received_queue, - debugger.remove_breakpoints_by_url( - self.config['file_path']['index']), - message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + params = debugger.RemoveBreakpointsUrl(self.config['file_path']['index']) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", worker_thread, params) ################################################################################################################ # worker thread: Debugger.getPossibleAndSetBreakpointByUrl ################################################################################################################ - message_id = next(self.id_generator) locations = [debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=10), debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=17), debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=25)] - response = await communicate_with_debugger_server(worker_instance_id, - worker_thread_to_send_queue, - worker_thread_received_queue, - debugger.get_possible_and_set_breakpoint_by_url(locations), - message_id) - response = json.loads(response) - assert response['id'] == message_id + params = debugger.SetBreakpointsLocations(locations) + response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", + worker_thread, params) assert response['result']['locations'][0]['id'] == 'id:10:0:' + self.config['file_path']['index'] assert response['result']['locations'][1]['id'] == 'id:17:0:' + self.config['file_path']['index'] assert response['result']['locations'][2]['id'] == 'id:25:0:' + self.config['file_path']['index'] ################################################################################################################ # worker thread: Debugger.resume ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instance_id, - worker_thread_to_send_queue, - worker_thread_received_queue, - debugger.resume(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(worker_instance_id, - worker_thread_received_queue) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.resume", worker_thread) # worker thread: Debugger.paused - response = await websocket.recv_msg_of_debugger_server(worker_instance_id, - worker_thread_received_queue) - response = json.loads(response) - assert response['method'] == 'Debugger.paused' + await self.debugger_impl.send("Debugger.paused", worker_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['reason'] == 'other' assert response['params']['hitBreakpoints'] == ['id:10:14:' + self.config['file_path']['index']] ################################################################################################################ # worker thread: Runtime.getProperties ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instance_id, - worker_thread_to_send_queue, - worker_thread_received_queue, - runtime.get_properties(object_id='0', - own_properties=True, - accessor_properties_only=False, - generate_preview=True), - message_id) - response = json.loads(response) - assert response['id'] == message_id + params = runtime.GetPropertiesParams('0') + response = await self.runtime_impl.send("Runtime.getProperties", worker_thread, params) assert response['result']['result'][0]['name'] == 'add' assert response['result']['result'][0]['value']['type'] == 'function' ################################################################################################################ # worker thread: Debugger.stepOut ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instance_id, - worker_thread_to_send_queue, - worker_thread_received_queue, - debugger.step_out(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(worker_instance_id, - worker_thread_received_queue) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.stepOut", worker_thread) ################################################################################################################ # main thread: Debugger.paused, hit breakpoint ################################################################################################################ - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - response = json.loads(response) - assert response['method'] == 'Debugger.paused' + response = await self.debugger_impl.recv("Debugger.paused", main_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['hitBreakpoints'] == ['id:25:4:' + self.config['file_path']['index']] ################################################################################################################ # main thread: Runtime.getProperties ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - runtime.get_properties(object_id='0', - own_properties=True, - accessor_properties_only=False, - generate_preview=True), - message_id) - response = json.loads(response) - assert response['id'] == message_id + params = runtime.GetPropertiesParams('0') + response = await self.runtime_impl.send("Runtime.getProperties", main_thread, params) assert response['result']['result'][0]['name'] == 'taskpoolTest' assert response['result']['result'][0]['value']['type'] == 'function' assert response['result']['result'][1]['name'] == 'valueSub' @@ -392,40 +248,29 @@ class TestDebug03: ################################################################################################################ # main thread: Debugger.resume ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.resume(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.resume", main_thread) ################################################################################################################ # worker thread: Debugger.paused ################################################################################################################ - response = await websocket.recv_msg_of_debugger_server(worker_instance_id, - worker_thread_received_queue) - response = json.loads(response) - assert response['method'] == 'Debugger.paused' + response = await self.debugger_impl.recv("Debugger.paused", main_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['hitBreakpoints'] == ['id:17:14:' + self.config['file_path']['index']] ################################################################################################################ # worker thread: Debugger.resume ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instance_id, - worker_thread_to_send_queue, - worker_thread_received_queue, - debugger.resume(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(worker_instance_id, - worker_thread_received_queue) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.resume", worker_thread) + ################################################################################################################ + # worker thread: Debugger.disable + ################################################################################################################ + await self.debugger_impl.send("Debugger.disable", worker_thread) + ################################################################################################################ + # main thread: Debugger.disable + ################################################################################################################ + await self.debugger_impl.send("Debugger.disable", main_thread) ################################################################################################################ # close the websocket connections ################################################################################################################ - await websocket.send_msg_to_debugger_server(worker_instance_id, worker_thread_to_send_queue, 'close') - await websocket.send_msg_to_debugger_server(main_thread_instance_id, main_thread_to_send_queue, 'close') + await websocket.send_msg_to_debugger_server(worker_thread.instance_id, worker_thread.send_msg_queue, 'close') + await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') await websocket.send_msg_to_connect_server('close') ################################################################################################################ \ No newline at end of file diff --git a/test/autotest/scenario_test/test_debug_04.py b/test/autotest/scenario_test/test_debug_04.py index 60b68fcbfe99cd8687a9b17cab1e823c4a1ac7d1..43976b72a0c00231097be8fcd23cd70ca23a6be2 100644 --- a/test/autotest/scenario_test/test_debug_04.py +++ b/test/autotest/scenario_test/test_debug_04.py @@ -17,7 +17,6 @@ limitations under the License. Description: Scenario test case. """ -import json import logging import os import time @@ -26,8 +25,8 @@ import pytest from aw import Application from aw import Utils -from aw import communicate_with_debugger_server from aw import debugger, runtime +from aw.api import debugger_api, runtime_api @pytest.mark.debug @@ -83,9 +82,11 @@ class TestDebug04: websocket = self.config['websocket'] taskpool = self.config['taskpool'] pid = self.config['pid'] + self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) + self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) Application.attach(self.config['bundle_name']) - taskpool.submit(websocket.main_task(taskpool, websocket, self.procedure, pid)) + taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) taskpool.await_taskpool() taskpool.task_join() if taskpool.task_exception: @@ -95,124 +96,51 @@ class TestDebug04: ################################################################################################################ # main thread: connect the debugger server ################################################################################################################ - send_msg = {"type": "connected"} - await websocket.send_msg_to_connect_server(send_msg) - response = await websocket.recv_msg_of_connect_server() - response = json.loads(response) - assert response['type'] == 'addInstance' - assert response['instanceId'] == 0, logging.error('instance id of the main thread not equal to 0') - assert response['tid'] == self.config['pid'] - main_thread_instance_id = await websocket.get_instance() - main_thread_to_send_queue = websocket.to_send_msg_queues[main_thread_instance_id] - main_thread_received_queue = websocket.received_msg_queues[main_thread_instance_id] - logging.info(f'Connect to the debugger server of instance: {main_thread_instance_id}') + main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) + logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') ################################################################################################################ # worker thread: connect the debugger server ################################################################################################################ - response = await websocket.recv_msg_of_connect_server() - response = json.loads(response) - assert response['type'] == 'addInstance' - assert response['instanceId'] != 0 - assert response['tid'] != self.config['pid'] - assert 'workerThread_' in response['name'] - worker_instance_id = await websocket.get_instance() - worker_thread_to_send_queue = websocket.to_send_msg_queues[worker_instance_id] - worker_thread_received_queue = websocket.received_msg_queues[worker_instance_id] - logging.info(f'Connect to the debugger server of instance: {worker_instance_id}') + worker_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread.instance_id}') ################################################################################################################ # main thread: Runtime.enable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - runtime.enable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {"protocols": []}} + await self.runtime_impl.send("Runtime.enable", main_thread) ################################################################################################################ # worker thread: Runtime.enable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instance_id, - worker_thread_to_send_queue, - worker_thread_received_queue, - runtime.enable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {"protocols": []}} + await self.runtime_impl.send("Runtime.enable", worker_thread) ################################################################################################################ # main thread: Debugger.enable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.enable(), message_id) - # main thread: Debugger.scriptParsed - response = json.loads(response) - assert response['method'] == 'Debugger.scriptParsed' - assert response['params']['url'] == self.config['file_path']['index'] - assert response['params']['endLine'] == 0 - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - response = json.loads(response) - assert response['method'] == 'Debugger.scriptParsed' - assert response['params']['url'] == self.config['file_path']['entry_ability'] - assert response['params']['endLine'] == 0 - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - assert json.loads(response) == {"id": message_id, "result": {"debuggerId": "0", - "protocols": Utils.get_custom_protocols()}} + await self.debugger_impl.send("Debugger.enable", main_thread) ################################################################################################################ # main thread: Runtime.runIfWaitingForDebugger ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - runtime.run_if_waiting_for_debugger(), message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", main_thread) ################################################################################################################ # worker thread: Debugger.enable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instance_id, - worker_thread_to_send_queue, - worker_thread_received_queue, - debugger.enable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {"debuggerId": "0", - "protocols": Utils.get_custom_protocols()}} + await self.debugger_impl.send("Debugger.enable", worker_thread) ################################################################################################################ # worker thread: Runtime.runIfWaitingForDebugger ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instance_id, - worker_thread_to_send_queue, - worker_thread_received_queue, - runtime.run_if_waiting_for_debugger(), message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", worker_thread) ################################################################################################################ # main thread: Debugger.removeBreakpointsByUrl ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.remove_breakpoints_by_url( - self.config['file_path']['index']), - message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + params = debugger.RemoveBreakpointsUrl(self.config['file_path']['index']) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", main_thread, params) ################################################################################################################ # main thread: Debugger.getPossibleAndSetBreakpointByUrl ################################################################################################################ - message_id = next(self.id_generator) locations = [debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=10), debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=17), debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=25)] - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.get_possible_and_set_breakpoint_by_url(locations), - message_id) - response = json.loads(response) - assert response['id'] == message_id + params = debugger.SetBreakpointsLocations(locations) + response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", + main_thread, params) assert response['result']['locations'][0]['id'] == 'id:10:0:' + self.config['file_path']['index'] assert response['result']['locations'][1]['id'] == 'id:17:0:' + self.config['file_path']['index'] assert response['result']['locations'][2]['id'] == 'id:25:0:' + self.config['file_path']['index'] @@ -223,118 +151,61 @@ class TestDebug04: ################################################################################################################ # worker thread: Debugger.scriptParsed ################################################################################################################ - response = await websocket.recv_msg_of_debugger_server(worker_instance_id, - worker_thread_received_queue) - response = json.loads(response) - assert response['method'] == 'Debugger.scriptParsed' + response = await self.debugger_impl.recv("Debugger.scriptParsed", worker_thread) assert response['params']['url'] == self.config['file_path']['index'] assert response['params']['endLine'] == 0 # worker thread: Debugger.paused - response = await websocket.recv_msg_of_debugger_server(worker_instance_id, - worker_thread_received_queue) - response = json.loads(response) - assert response['method'] == 'Debugger.paused' + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['reason'] == 'Break on start' ################################################################################################################ # worker thread: Debugger.removeBreakpointsByUrl ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instance_id, - worker_thread_to_send_queue, - worker_thread_received_queue, - debugger.remove_breakpoints_by_url( - self.config['file_path']['index']), - message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + params = debugger.RemoveBreakpointsUrl(self.config['file_path']['index']) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", worker_thread, params) ################################################################################################################ # worker thread: Debugger.getPossibleAndSetBreakpointByUrl ################################################################################################################ - message_id = next(self.id_generator) locations = [debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=10), debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=17), debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=25)] - response = await communicate_with_debugger_server(worker_instance_id, - worker_thread_to_send_queue, - worker_thread_received_queue, - debugger.get_possible_and_set_breakpoint_by_url(locations), - message_id) - response = json.loads(response) - assert response['id'] == message_id + params = debugger.SetBreakpointsLocations(locations) + response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", + worker_thread, params) assert response['result']['locations'][0]['id'] == 'id:10:0:' + self.config['file_path']['index'] assert response['result']['locations'][1]['id'] == 'id:17:0:' + self.config['file_path']['index'] assert response['result']['locations'][2]['id'] == 'id:25:0:' + self.config['file_path']['index'] ################################################################################################################ # worker thread: Debugger.resume ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instance_id, - worker_thread_to_send_queue, - worker_thread_received_queue, - debugger.resume(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(worker_instance_id, - worker_thread_received_queue) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.resume", worker_thread) # worker thread: Debugger.paused - response = await websocket.recv_msg_of_debugger_server(worker_instance_id, - worker_thread_received_queue) - response = json.loads(response) - assert response['method'] == 'Debugger.paused' + await self.debugger_impl.send("Debugger.paused", worker_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['reason'] == 'other' assert response['params']['hitBreakpoints'] == ['id:10:14:' + self.config['file_path']['index']] ################################################################################################################ # worker thread: Runtime.getProperties ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instance_id, - worker_thread_to_send_queue, - worker_thread_received_queue, - runtime.get_properties(object_id='0', - own_properties=True, - accessor_properties_only=False, - generate_preview=True), - message_id) - response = json.loads(response) - assert response['id'] == message_id + params = runtime.GetPropertiesParams('0') + response = await self.runtime_impl.send("Runtime.getProperties", worker_thread, params) assert response['result']['result'][0]['name'] == 'add' assert response['result']['result'][0]['value']['type'] == 'function' ################################################################################################################ # worker thread: Debugger.stepOut ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instance_id, - worker_thread_to_send_queue, - worker_thread_received_queue, - debugger.step_out(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(worker_instance_id, - worker_thread_received_queue) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.stepOut", worker_thread) ################################################################################################################ # main thread: Debugger.paused, hit breakpoint ################################################################################################################ - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - response = json.loads(response) - assert response['method'] == 'Debugger.paused' + response = await self.debugger_impl.recv("Debugger.paused", main_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['hitBreakpoints'] == ['id:25:4:' + self.config['file_path']['index']] ################################################################################################################ # main thread: Runtime.getProperties ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - runtime.get_properties(object_id='0', - own_properties=True, - accessor_properties_only=False, - generate_preview=True), - message_id) - response = json.loads(response) - assert response['id'] == message_id + params = runtime.GetPropertiesParams('0') + response = await self.runtime_impl.send("Runtime.getProperties", main_thread, params) assert response['result']['result'][0]['name'] == 'taskpoolTest' assert response['result']['result'][0]['value']['type'] == 'function' assert response['result']['result'][1]['name'] == 'valueSub' @@ -345,40 +216,29 @@ class TestDebug04: ################################################################################################################ # main thread: Debugger.resume ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.resume(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.resume", main_thread) ################################################################################################################ # worker thread: Debugger.paused ################################################################################################################ - response = await websocket.recv_msg_of_debugger_server(worker_instance_id, - worker_thread_received_queue) - response = json.loads(response) - assert response['method'] == 'Debugger.paused' + response = await self.debugger_impl.recv("Debugger.paused", main_thread) assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] assert response['params']['hitBreakpoints'] == ['id:17:14:' + self.config['file_path']['index']] ################################################################################################################ # worker thread: Debugger.resume ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instance_id, - worker_thread_to_send_queue, - worker_thread_received_queue, - debugger.resume(), message_id) - assert json.loads(response) == {"method": "Debugger.resumed", "params": {}} - response = await websocket.recv_msg_of_debugger_server(worker_instance_id, - worker_thread_received_queue) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.resume", worker_thread) + ################################################################################################################ + # worker thread: Debugger.disable + ################################################################################################################ + await self.debugger_impl.send("Debugger.disable", worker_thread) + ################################################################################################################ + # main thread: Debugger.disable + ################################################################################################################ + await self.debugger_impl.send("Debugger.disable", main_thread) ################################################################################################################ # close the websocket connections ################################################################################################################ - await websocket.send_msg_to_debugger_server(worker_instance_id, worker_thread_to_send_queue, 'close') - await websocket.send_msg_to_debugger_server(main_thread_instance_id, main_thread_to_send_queue, 'close') + await websocket.send_msg_to_debugger_server(worker_thread.instance_id, worker_thread.send_msg_queue, 'close') + await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') await websocket.send_msg_to_connect_server('close') ################################################################################################################ \ No newline at end of file diff --git a/test/autotest/scenario_test/test_debug_05.py b/test/autotest/scenario_test/test_debug_05.py new file mode 100644 index 0000000000000000000000000000000000000000..a9078daec1d4705051e99a4ce3fada6e109c6666 --- /dev/null +++ b/test/autotest/scenario_test/test_debug_05.py @@ -0,0 +1,275 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Copyright (c) 2024 Huawei Device Co., Ltd. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Description: Scenario test case. +""" + +import logging +import os +import time + +import pytest + +from aw import Application +from aw import Utils +from aw import debugger +from aw.api import debugger_api, runtime_api + + +@pytest.mark.debug +@pytest.mark.timeout(30) +class TestDebug05: + """ + 测试用例:多实例异常断点调试 + """ + + def setup_method(self): + logging.info('Start running TestDebug05: setup') + + self.log_path = rf'{os.path.dirname(__file__)}\..\log' + self.hilog_file_name = 'test_debug_05.hilog.txt' + self.id_generator = Utils.message_id_generator() + + # receive the hilog before the test start + Utils.clear_fault_log() + self.hilog_process, self.write_thread = Utils.save_hilog(log_path=self.log_path, + file_name=self.hilog_file_name, + debug_on=True) + + def teardown_method(self): + Application.uninstall(self.config['bundle_name']) + + # terminate the hilog receive process after the test done + time.sleep(3) + self.hilog_process.stdout.close() + self.hilog_process.terminate() + self.hilog_process.wait() + self.write_thread.join() + + Utils.save_fault_log(log_path=self.log_path) + logging.info('TestDebug05 done') + + def test(self, test_suite_worker_03_debug): + logging.info('Start running TestDebug05: test') + self.config = test_suite_worker_03_debug + websocket = self.config['websocket'] + taskpool = self.config['taskpool'] + pid = self.config['pid'] + self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) + self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) + + taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) + taskpool.await_taskpool() + taskpool.task_join() + if taskpool.task_exception: + raise taskpool.task_exception + + async def procedure(self, websocket): + ################################################################################################################ + # main thread: connect the debugger server + ################################################################################################################ + main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) + logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') + ################################################################################################################ + # main thread: Runtime.enable + ################################################################################################################ + await self.runtime_impl.send("Runtime.enable", main_thread) + ################################################################################################################ + # main thread: Debugger.enable + ################################################################################################################ + await self.debugger_impl.send("Debugger.enable", main_thread) + ################################################################################################################ + # main thread: Runtime.runIfWaitingForDebugger + ################################################################################################################ + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", main_thread) + ################################################################################################################ + # main thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) + assert response['params']['url'] == self.config['file_path']['entry_ability'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # main thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['entry_ability'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # main thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) + assert response['params']['url'] == self.config['file_path']['index'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # main thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # main thread: Debugger.removeBreakpointsByUrl + ################################################################################################################ + params = debugger.RemoveBreakpointsUrl(self.config['file_path']['index']) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", main_thread, params) + ################################################################################################################ + # main thread: Debugger.getPossibleAndSetBreakpointByUrl + ################################################################################################################ + locations = [debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=15)] + params = debugger.SetBreakpointsLocations(locations) + response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", + main_thread, params) + assert response['result']['locations'][0]['id'] == 'id:15:0:' + self.config['file_path']['index'] + ################################################################################################################ + # main thread: Debugger.setPauseOnExceptions + ################################################################################################################ + params = debugger.PauseOnExceptionsState.ALL + await self.debugger_impl.send("Debugger.setPauseOnExceptions", main_thread, params) + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # main thread: Debugger.paused, hit breakpoint + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] + assert response['params']['hitBreakpoints'] == ['id:15:4:' + self.config['file_path']['index']] + ################################################################################################################ + # worker thread: connect the debugger server + ################################################################################################################ + worker_thread_1 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread_1.instance_id}') + worker_thread_2 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread_2.instance_id}') + if worker_thread_1.instance_id > worker_thread_2.instance_id: + worker_thread_1, worker_thread_2 = worker_thread_2, worker_thread_1 + ################################################################################################################ + # worker thread: Runtime.enable + ################################################################################################################ + await self.runtime_impl.send("Runtime.enable", worker_thread_1) + await self.runtime_impl.send("Runtime.enable", worker_thread_2) + ################################################################################################################ + # worker thread: Debugger.enable + ################################################################################################################ + await self.debugger_impl.send("Debugger.enable", worker_thread_1) + await self.debugger_impl.send("Debugger.enable", worker_thread_2) + ################################################################################################################ + # worker thread: Runtime.runIfWaitingForDebugger + ################################################################################################################ + # worker thread 1: Runtime.runIfWaitingForDebugger + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", worker_thread_1) + # worker thread 1: Debugger.scriptParsed + response = await self.debugger_impl.recv("Debugger.scriptParsed", worker_thread_1) + assert response['params']['url'] == self.config['file_path']['worker'] + assert response['params']['endLine'] == 0 + # worker thread 1: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread_1) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'Break on start' + # worker thread 2: Runtime.runIfWaitingForDebugger + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", worker_thread_2) + # worker thread 2: Debugger.scriptParsed + response = await self.debugger_impl.recv("Debugger.scriptParsed", worker_thread_2) + assert response['params']['url'] == self.config['file_path']['worker'] + assert response['params']['endLine'] == 0 + # worker thread 2: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread_2) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread_1) + await self.debugger_impl.send("Debugger.resume", worker_thread_2) + ################################################################################################################ + # worker thread: Debugger.setPauseOnExceptions + ################################################################################################################ + params = debugger.PauseOnExceptionsState.ALL + await self.debugger_impl.send("Debugger.setPauseOnExceptions", worker_thread_1, params) + await self.debugger_impl.send("Debugger.setPauseOnExceptions", worker_thread_2, params) + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + # main thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] + assert response['params']['reason'] == 'other' + assert response['params']['hitBreakpoints'] == ['id:15:4:' + self.config['file_path']['index']] + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread_1) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'exception' + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread_1) + ################################################################################################################ + # worker thread: Debugger.setPauseOnExceptions + ################################################################################################################ + params = debugger.PauseOnExceptionsState.NONE + await self.debugger_impl.send("Debugger.setPauseOnExceptions", worker_thread_2, params) + ################################################################################################################ + # worker thread: Debugger.removeBreakpointsByUrl + ################################################################################################################ + params = debugger.RemoveBreakpointsUrl(self.config['file_path']['worker']) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", worker_thread_2, params) + ################################################################################################################ + # worker thread: Debugger.getPossibleAndSetBreakpointByUrl + ################################################################################################################ + locations = [debugger.BreakLocationUrl(url=self.config['file_path']['worker'], line_number=17)] + params = debugger.SetBreakpointsLocations(locations) + response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", + worker_thread_2, params) + assert response['result']['locations'][0]['id'] == 'id:17:0:' + self.config['file_path']['worker'] + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread_1) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'other' + assert response['params']['hitBreakpoints'] == ['id:17:8:' + self.config['file_path']['worker']] + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread_2) + ################################################################################################################ + # main thread: click on the screen + ################################################################################################################ + Application.click_on_middle() + ################################################################################################################ + # worker thread: destroy instance + ################################################################################################################ + response = await self.debugger_impl.destroy_instance() + assert response['instanceId'] != self.config['pid'] + response = await self.debugger_impl.destroy_instance() + assert response['instanceId'] != self.config['pid'] + ################################################################################################################ + # main thread: Debugger.disable + ################################################################################################################ + await self.debugger_impl.send("Debugger.disable", main_thread) + ################################################################################################################ + # close the websocket connections + ################################################################################################################ + await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') + await websocket.send_msg_to_connect_server('close') + ################################################################################################################ \ No newline at end of file diff --git a/test/autotest/scenario_test/test_debug_06.py b/test/autotest/scenario_test/test_debug_06.py new file mode 100644 index 0000000000000000000000000000000000000000..3befbc5635f4855fdf5ead36e10814aa395a39cd --- /dev/null +++ b/test/autotest/scenario_test/test_debug_06.py @@ -0,0 +1,300 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Copyright (c) 2024 Huawei Device Co., Ltd. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Description: Scenario test case. +""" + +import logging +import os +import time + +import pytest + +from aw import Application +from aw import Utils +from aw import debugger, runtime +from aw.api import debugger_api, runtime_api + + +@pytest.mark.debug +@pytest.mark.timeout(30) +class TestDebug06: + """ + 测试用例:多实例 getProperties 调试 + """ + + def setup_method(self): + logging.info('Start running TestDebug06: setup') + + self.log_path = rf'{os.path.dirname(__file__)}\..\log' + self.hilog_file_name = 'test_debug_06.hilog.txt' + self.id_generator = Utils.message_id_generator() + + # receive the hilog before the test start + Utils.clear_fault_log() + self.hilog_process, self.write_thread = Utils.save_hilog(log_path=self.log_path, + file_name=self.hilog_file_name, + debug_on=True) + + def teardown_method(self): + Application.uninstall(self.config['bundle_name']) + + # terminate the hilog receive process after the test done + time.sleep(3) + self.hilog_process.stdout.close() + self.hilog_process.terminate() + self.hilog_process.wait() + self.write_thread.join() + + Utils.save_fault_log(log_path=self.log_path) + logging.info('TestDebug06 done') + + def test(self, test_suite_worker_04_debug): + logging.info('Start running TestDebug06: test') + self.config = test_suite_worker_04_debug + websocket = self.config['websocket'] + taskpool = self.config['taskpool'] + pid = self.config['pid'] + self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) + self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) + + taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) + taskpool.await_taskpool() + taskpool.task_join() + if taskpool.task_exception: + raise taskpool.task_exception + + async def procedure(self, websocket): + ################################################################################################################ + # main thread: connect the debugger server + ################################################################################################################ + main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) + logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') + ################################################################################################################ + # main thread: Runtime.enable + ################################################################################################################ + await self.runtime_impl.send("Runtime.enable", main_thread) + ################################################################################################################ + # main thread: Debugger.enable + ################################################################################################################ + await self.debugger_impl.send("Debugger.enable", main_thread) + ################################################################################################################ + # main thread: Runtime.runIfWaitingForDebugger + ################################################################################################################ + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", main_thread) + ################################################################################################################ + # main thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) + assert response['params']['url'] == self.config['file_path']['entry_ability'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # main thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['entry_ability'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # main thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) + assert response['params']['url'] == self.config['file_path']['index'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # main thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # main thread: Debugger.removeBreakpointsByUrl + ################################################################################################################ + params = debugger.RemoveBreakpointsUrl(self.config['file_path']['index']) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", main_thread, params) + ################################################################################################################ + # main thread: Debugger.getPossibleAndSetBreakpointByUrl + ################################################################################################################ + locations = [debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=12)] + params = debugger.SetBreakpointsLocations(locations) + response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", + main_thread, params) + assert response['result']['locations'][0]['id'] == 'id:12:0:' + self.config['file_path']['index'] + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # main thread: Debugger.paused, hit breakpoint + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] + assert response['params']['hitBreakpoints'] == ['id:12:0:' + self.config['file_path']['index']] + ################################################################################################################ + # worker thread: connect the debugger server + ################################################################################################################ + worker_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread.instance_id}') + ################################################################################################################ + # worker thread: Runtime.enable + ################################################################################################################ + await self.runtime_impl.send("Runtime.enable", worker_thread) + ################################################################################################################ + # worker thread: Debugger.enable + ################################################################################################################ + await self.debugger_impl.send("Debugger.enable", worker_thread) + ################################################################################################################ + # worker thread: Runtime.runIfWaitingForDebugger + ################################################################################################################ + # worker thread 1: Runtime.runIfWaitingForDebugger + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", worker_thread) + # worker thread 1: Debugger.scriptParsed + response = await self.debugger_impl.recv("Debugger.scriptParsed", worker_thread) + assert response['params']['url'] == self.config['file_path']['worker'] + assert response['params']['endLine'] == 0 + # worker thread 1: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # worker thread: Debugger.removeBreakpointsByUrl + ################################################################################################################ + params = debugger.RemoveBreakpointsUrl(self.config['file_path']['worker']) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", worker_thread, params) + ################################################################################################################ + # worker thread: Debugger.getPossibleAndSetBreakpointByUrl + ################################################################################################################ + locations = [debugger.BreakLocationUrl(url=self.config['file_path']['worker'], line_number=116)] + params = debugger.SetBreakpointsLocations(locations) + response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", + worker_thread, params) + assert response['result']['locations'][0]['id'] == 'id:116:0:' + self.config['file_path']['worker'] + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread) + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'other' + assert response['params']['hitBreakpoints'] == ['id:116:4:' + self.config['file_path']['worker']] + ################################################################################################################ + # worker thread: Runtime.getProperties + ################################################################################################################ + params = runtime.GetPropertiesParams('0') + response = await self.runtime_impl.send("Runtime.getProperties", worker_thread, params) + variables = self.get_variables_from_result(response['result']['result'], 'local') + assert variables == {'localArrayList': 'ArrayList', 'localBigInt64Array': 'BigInt64Array', + 'localBigUint64Array': 'BigUint64Array', 'localDataView': 'DataView(20)', + 'localDeque': 'Deque', 'localFloat32Array': 'Float32Array', + 'localFloat64Array': 'Float64Array', 'localHashMap': 'HashMap', 'localHashSet': 'HashSet', + 'localInt16Array': 'Int16Array(0)', 'localInt32Array': 'Int32Array(0)', + 'localInt8Array': 'Int8Array(0)', 'localLightWeightMap': 'LightWeightMap', + 'localLightWeightSet': 'LightWeightSet', 'localLinkedList': 'LinkedList', + 'localList': 'List', 'localMapIter': 'function entries( { [native code] }', + 'localNull': 'null', 'localPerson': 'Person', 'localPlainArray': 'PlainArray', + 'localPromise': 'Promise', 'localProxy': 'Proxy', 'localQueue': 'Queue', + 'localSendableClass': 'SendableClass [Sendable]', + 'localSharedArrayBuffer': 'SharedArrayBuffer(32)', 'localStack': 'Stack', + 'localTreeMap': 'TreeMap', 'localTreeSet': 'TreeSet', 'localUint16Array': 'Uint16Array', + 'localUint32Array': 'Uint32Array', 'localUint8Array': 'Uint8Array(3)', + 'localUint8ClampedArray': 'Uint8ClampedArray', 'localUndefined': 'undefined', + 'localWeakMap': 'WeakMap(0)', 'localWeakRef': 'WeakRef {}', 'localWeakSet': 'WeakSet(0)'} + ################################################################################################################ + # worker thread: Runtime.getProperties + ################################################################################################################ + params = runtime.GetPropertiesParams('1') + response = await self.runtime_impl.send("Runtime.getProperties", worker_thread, params) + variables = self.get_variables_from_result(response['result']['result'], 'closure') + assert variables == {'closureArray': 'Array(3)', 'closureArrayBuffer': 'Arraybuffer(20)', + 'closureMap': 'Map(0)', 'closureNum': '20', 'closureRegExp': '/^ab+c/g', + 'closureSet': "Set(1) {'closure'}", 'closureString': 'closure'} + ################################################################################################################ + # worker thread: Runtime.getProperties + ################################################################################################################ + params = runtime.GetPropertiesParams('2') + response = await self.runtime_impl.send("Runtime.getProperties", worker_thread, params) + variables = self.get_variables_from_result(response['result']['result'], '') + assert variables == {'ArrayList': 'function ArrayList( { [native code] }', + 'Deque': 'function Deque( { [native code] }', + 'HashMap': 'function HashMap( { [native code] }', + 'HashSet': 'function HashSet( { [native code] }', + 'LightWeightMap': 'function LightWeightMap( { [native code] }', + 'LightWeightSet': 'function LightWeightSet( { [native code] }', + 'LinkedList': 'function LinkedList( { [native code] }', + 'List': 'function List( { [native code] }', + 'PlainArray': 'function PlainArray( { [native code] }', + 'Queue': 'function Queue( { [native code] }', 'Stack': 'function Stack( { [native code] }', + 'TreeMap': 'function TreeMap( { [native code] }', + 'TreeSet': 'function TreeSet( { [native code] }', 'worker': 'Object'} + ################################################################################################################ + # worker thread: Runtime.getProperties + ################################################################################################################ + params = runtime.GetPropertiesParams('3') + response = await self.runtime_impl.send("Runtime.getProperties", worker_thread, params) + variables = self.get_variables_from_result(response['result']['result'], 'global') + assert variables == {'globalArray': 'Array(3)', 'globalBigInt': '9007199254740991n', + 'globalBool': 'Boolean{[[PrimitiveValue]]: false}', + 'globalDate': 'Wed Aug 28 2024 02:41:00 GMT+0800', + 'globalNum': 'Number{[[PrimitiveValue]]: 20}', + 'globalObject': 'String{[[PrimitiveValue]]: globalObject}', + 'globalStr': 'String{[[PrimitiveValue]]: globalStr}', 'globalThis': 'Object'} + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread) + ################################################################################################################ + # main thread: click on the screen + ################################################################################################################ + Application.click_on_middle() + ################################################################################################################ + # worker thread: destroy instance + ################################################################################################################ + response = await self.debugger_impl.destroy_instance() + assert response['instanceId'] == worker_thread.instance_id + ################################################################################################################ + # main thread: Debugger.disable + ################################################################################################################ + await self.debugger_impl.send("Debugger.disable", main_thread) + ################################################################################################################ + # close the websocket connections + ################################################################################################################ + await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') + await websocket.send_msg_to_connect_server('close') + ################################################################################################################ + + def get_variables_from_result(self, result, prefix_name): + variables = {} + for var in result: + if var['name'].startswith(prefix_name): + name = var['name'] + value = var['value'] + description = value.get('description') + if description is not None: + index_of_at = description.find('@') + variables[name] = description if index_of_at == -1 else \ + (description[:index_of_at] + description[index_of_at + 9:]) + else: + subtype = value.get('subtype') + variables[name] = subtype if subtype is not None else value.get('type') + return variables \ No newline at end of file diff --git a/test/autotest/scenario_test/test_debug_07.py b/test/autotest/scenario_test/test_debug_07.py new file mode 100644 index 0000000000000000000000000000000000000000..98e757d2fa9e92f62e2f87fadaff36f993fc1603 --- /dev/null +++ b/test/autotest/scenario_test/test_debug_07.py @@ -0,0 +1,338 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Copyright (c) 2024 Huawei Device Co., Ltd. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Description: Scenario test case. +""" + +import logging +import os +import time + +import pytest + +from aw import Application +from aw import Utils +from aw import debugger +from aw.api import debugger_api, runtime_api + + +@pytest.mark.debug +@pytest.mark.timeout(30) +class TestDebug07: + """ + 测试用例:多实例 set value 调试 + """ + + def setup_method(self): + logging.info('Start running TestDebug07: setup') + + self.log_path = rf'{os.path.dirname(__file__)}\..\log' + self.hilog_file_name = 'test_debug_07.hilog.txt' + self.id_generator = Utils.message_id_generator() + + # receive the hilog before the test start + Utils.clear_fault_log() + self.hilog_process, self.write_thread = Utils.save_hilog(log_path=self.log_path, + file_name=self.hilog_file_name, + debug_on=True) + + def teardown_method(self): + Application.uninstall(self.config['bundle_name']) + + # terminate the hilog receive process after the test done + time.sleep(3) + self.hilog_process.stdout.close() + self.hilog_process.terminate() + self.hilog_process.wait() + self.write_thread.join() + + Utils.save_fault_log(log_path=self.log_path) + logging.info('TestDebug07 done') + + def test(self, test_suite_worker_04_debug): + logging.info('Start running TestDebug07: test') + self.config = test_suite_worker_04_debug + websocket = self.config['websocket'] + taskpool = self.config['taskpool'] + pid = self.config['pid'] + self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) + self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) + + taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) + taskpool.await_taskpool() + taskpool.task_join() + if taskpool.task_exception: + raise taskpool.task_exception + + async def procedure(self, websocket): + ################################################################################################################ + # main thread: connect the debugger server + ################################################################################################################ + main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) + logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') + ################################################################################################################ + # main thread: Runtime.enable + ################################################################################################################ + await self.runtime_impl.send("Runtime.enable", main_thread) + ################################################################################################################ + # main thread: Debugger.enable + ################################################################################################################ + await self.debugger_impl.send("Debugger.enable", main_thread) + ################################################################################################################ + # main thread: Runtime.runIfWaitingForDebugger + ################################################################################################################ + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", main_thread) + ################################################################################################################ + # main thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) + assert response['params']['url'] == self.config['file_path']['entry_ability'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # main thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['entry_ability'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # main thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) + assert response['params']['url'] == self.config['file_path']['index'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # main thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # main thread: Debugger.removeBreakpointsByUrl + ################################################################################################################ + params = debugger.RemoveBreakpointsUrl(self.config['file_path']['index']) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", main_thread, params) + ################################################################################################################ + # main thread: Debugger.getPossibleAndSetBreakpointByUrl + ################################################################################################################ + locations = [debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=12)] + params = debugger.SetBreakpointsLocations(locations) + response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", + main_thread, params) + assert response['result']['locations'][0]['id'] == 'id:12:0:' + self.config['file_path']['index'] + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # main thread: Debugger.paused, hit breakpoint + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] + assert response['params']['hitBreakpoints'] == ['id:12:0:' + self.config['file_path']['index']] + ################################################################################################################ + # worker thread: connect the debugger server + ################################################################################################################ + worker_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread.instance_id}') + ################################################################################################################ + # worker thread: Runtime.enable + ################################################################################################################ + await self.runtime_impl.send("Runtime.enable", worker_thread) + ################################################################################################################ + # worker thread: Debugger.enable + ################################################################################################################ + await self.debugger_impl.send("Debugger.enable", worker_thread) + ################################################################################################################ + # worker thread: Runtime.runIfWaitingForDebugger + ################################################################################################################ + # worker thread 1: Runtime.runIfWaitingForDebugger + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", worker_thread) + # worker thread 1: Debugger.scriptParsed + response = await self.debugger_impl.recv("Debugger.scriptParsed", worker_thread) + assert response['params']['url'] == self.config['file_path']['worker'] + assert response['params']['endLine'] == 0 + # worker thread 1: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # worker thread: Debugger.removeBreakpointsByUrl + ################################################################################################################ + params = debugger.RemoveBreakpointsUrl(self.config['file_path']['worker']) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", worker_thread, params) + ################################################################################################################ + # worker thread: Debugger.getPossibleAndSetBreakpointByUrl + ################################################################################################################ + locations = [debugger.BreakLocationUrl(url=self.config['file_path']['worker'], line_number=116)] + params = debugger.SetBreakpointsLocations(locations) + response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", + worker_thread, params) + assert response['result']['locations'][0]['id'] == 'id:116:0:' + self.config['file_path']['worker'] + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread) + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'other' + assert response['params']['hitBreakpoints'] == ['id:116:4:' + self.config['file_path']['worker']] + ################################################################################################################ + # worker thread: Debugger.evaluateOnCallFrame + ################################################################################################################ + # watch localPerson.age + params = debugger.EvaluateOnCallFrameParams('UEFOREEAAAAAAAAADAACAGABAAAAAAAAAAAAAAIAAAA8AAAAAQAAAFwBAAAAAAAARA' + 'AAAAEAAABEAAAApAAAANcAAACAAAAAYAEAAAIAAABsAAAAAwAAAHQAAAD/////////' + '////////////pAAAANcAAACAAAAAhQAAAJcAAAAHYWdlACFkZWJ1Z2dlckdldFZhbH' + 'VlABdsb2NhbFBlcnNvbgAzTF9FU1Nsb3ROdW1iZXJBbm5vdGF0aW9uOwAAAAAAgUAA' + 'AAIAABdmdW5jX21haW5fMAATTF9HTE9CQUw7AAAAAAABAAECAAABAP//ygAAAIgCAS' + 'EBAAACAAVSAQAABhQBAAAAFVNsb3ROdW1iZXIAAAABAAgBAAAGAAAANwkDKABEkESh' + 'RLJtYQVgBUIAAQBhBj4CAGEHAmEIYAYrAgcIYQRgBEIEAABkC2sBDwD/////DwACAC' + 'EATQEAAA==') + response = await self.debugger_impl.send("Debugger.evaluateOnCallFrame", worker_thread, params) + assert response['result']['result'] == {"type": "number", "unserializableValue": "20", "description": "20"} + # set localPerson.age = 22 + params = debugger.EvaluateOnCallFrameParams('UEFOREEAAAAAAAAADAACAGQBAAAAAAAAAAAAAAIAAAA8AAAAAQAAAGABAAAAAAAARA' + 'AAAAEAAABEAAAApAAAANcAAACAAAAAZAEAAAIAAABsAAAAAwAAAHQAAAD/////////' + '////////////pAAAANcAAACAAAAAhQAAAJcAAAAHYWdlACFkZWJ1Z2dlckdldFZhbH' + 'VlABdsb2NhbFBlcnNvbgAzTF9FU1Nsb3ROdW1iZXJBbm5vdGF0aW9uOwAAAAAAgUAA' + 'AAIAABdmdW5jX21haW5fMAATTF9HTE9CQUw7AAAAAAABAAECAAABAP//ygAAAIgCAS' + 'EBAAACAAVWAQAABhQBAAAAFVNsb3ROdW1iZXIAAAABAAgBAAAGAAAANwkDLABEkESh' + 'RLJtYQVgBUIAAQBhBj4CAGEHAmEIYAYrAgcIYQRiFgAAAEMEAAAEZAtrAQ8A/////w' + '8AAgAlAFEBAAA=') + response = await self.debugger_impl.send("Debugger.evaluateOnCallFrame", worker_thread, params) + assert response['result']['result'] == {"type": "number", "unserializableValue": "22", "description": "22"} + # watch localPerson.age + 10 + params = debugger.EvaluateOnCallFrameParams('UEFOREEAAAAAAAAADAACAGwBAAAAAAAAAAAAAAIAAAA8AAAAAQAAAGgBAAAAAAAARA' + 'AAAAEAAABEAAAApAAAANcAAACAAAAAbAEAAAIAAABsAAAAAwAAAHQAAAD/////////' + '////////////pAAAANcAAACAAAAAhQAAAJcAAAAHYWdlACFkZWJ1Z2dlckdldFZhbH' + 'VlABdsb2NhbFBlcnNvbgAzTF9FU1Nsb3ROdW1iZXJBbm5vdGF0aW9uOwAAAAAAgUAA' + 'AAIAABdmdW5jX21haW5fMAATTF9HTE9CQUw7AAAAAAABAAECAAABAP//ygAAAIgCAS' + 'EBAAACAAVcAQAABhQBAAAAFVNsb3ROdW1iZXIAAAABAAgBAAAHAAAANwoDMgBEoESx' + 'RMJtYQZgBkIAAQBhBz4CAGEIAmEJYAcrAggJYQVgBUIEAABhBGIKAAAACgYEZAtrAQ' + '8A/////w8AAgArAAAAVwEAAA==') + response = await self.debugger_impl.send("Debugger.evaluateOnCallFrame", worker_thread, params) + assert response['result']['result'] == {"type": "number", "unserializableValue": "32", "description": "32"} + ################################################################################################################ + # worker thread: Debugger.evaluateOnCallFrame + ################################################################################################################ + params = debugger.EvaluateOnCallFrameParams('closureString') + response = await self.debugger_impl.send("Debugger.evaluateOnCallFrame", worker_thread, params) + assert response['result']['result'] == {"type": "string", "unserializableValue": "closure", + "description": "closure"} + params = debugger.EvaluateOnCallFrameParams('closureString = "modified"') + response = await self.debugger_impl.send("Debugger.evaluateOnCallFrame", worker_thread, params) + assert response['result']['result'] == {"type": "string", "unserializableValue": "modified", + "description": "modified"} + params = debugger.EvaluateOnCallFrameParams('closureString') + response = await self.debugger_impl.send("Debugger.evaluateOnCallFrame", worker_thread, params) + assert response['result']['result'] == {"type": "string", "unserializableValue": "modified", + "description": "modified"} + ################################################################################################################ + # worker thread: Debugger.evaluateOnCallFrame + ################################################################################################################ + params = debugger.EvaluateOnCallFrameParams('globalArray') + response = await self.debugger_impl.send("Debugger.evaluateOnCallFrame", worker_thread, params) + assert response['result']['result']['description'].startswith('Array(3)') + # globalArray.push(999) + params = debugger.EvaluateOnCallFrameParams('UEFOREEAAAAAAAAADAACAHABAAAAAAAAAAAAAAIAAAA8AAAAAQAAAGwBAAAAAAAARA' + 'AAAAEAAABEAAAApQAAANgAAACAAAAAcAEAAAIAAABsAAAAAwAAAHQAAAD/////////' + '////////////pQAAANgAAACAAAAAkgAAAJ8AAAAhZGVidWdnZXJHZXRWYWx1ZQAXZ2' + 'xvYmFsQXJyYXkACXB1c2gAM0xfRVNTbG90TnVtYmVyQW5ub3RhdGlvbjsAAAAAAIFA' + 'AAACAAAXZnVuY19tYWluXzAAE0xfR0xPQkFMOwAAAAAAAQABAgAAAQD//8sAAACIAg' + 'EiAQAAAgAFYgEAAAYVAQAAABVTbG90TnVtYmVyAAAAAQAJAQAACAAAADcKAzcARKBE' + 'sUTCbWEGYAZCAAAAYQc+AQBhCAJhCWAHKwIICWEFYAVCBAIAYQRi5wMAAGEGYAQuBg' + 'UGZAtrAQ8A/////w8AAgAwAF0BAAA=') + response = await self.debugger_impl.send("Debugger.evaluateOnCallFrame", worker_thread, params) + assert response['result']['result'] == {"type": "number", "unserializableValue": "4", "description": "4"} + # watch globalArray[3] + params = debugger.EvaluateOnCallFrameParams('UEFOREEAAAAAAAAADAACAGABAAAAAAAAAAAAAAIAAAA8AAAAAQAAAFwBAAAAAAAARA' + 'AAAAEAAABEAAAAmwAAAM4AAAB8AAAAYAEAAAIAAABsAAAAAgAAAHQAAAD/////////' + '////////////mwAAAM4AAAB8AAAAjgAAACFkZWJ1Z2dlckdldFZhbHVlABdnbG9iYW' + 'xBcnJheQAzTF9FU1Nsb3ROdW1iZXJBbm5vdGF0aW9uOwAAAAAAgUAAAAIAABdmdW5j' + 'X21haW5fMAATTF9HTE9CQUw7AAAAAAABAAECAAABAP//wQAAAIgCARgBAAACAAVPAQ' + 'AABgsBAAAAFVNsb3ROdW1iZXIAAAABAP8AAAAGAAAANwkDLgBEkEShRLJtYQVgBUIA' + 'AABhBj4BAGEHAmEIYAYrAgcIYQRiAwAAAGEFYAU3BARkC2sBDwD/////DwACACcAAA' + 'AASgEAAA==') + response = await self.debugger_impl.send("Debugger.evaluateOnCallFrame", worker_thread, params) + assert response['result']['result'] == {"type": "number", "unserializableValue": "999", "description": "999"} + ################################################################################################################ + # worker thread: Debugger.evaluateOnCallFrame + ################################################################################################################ + # watch worker.workerPort + params = debugger.EvaluateOnCallFrameParams('UEFOREEAAAAAAAAADAACAGQBAAAAAAAAAAAAAAIAAAA8AAAAAQAAAGABAAAAAAAARA' + 'AAAAEAAABEAAAApgAAANkAAACAAAAAZAEAAAIAAABsAAAAAwAAAHQAAAD/////////' + '////////////pgAAANkAAACAAAAAkgAAAJoAAAAhZGVidWdnZXJHZXRWYWx1ZQANd2' + '9ya2VyABV3b3JrZXJQb3J0ADNMX0VTU2xvdE51bWJlckFubm90YXRpb247AAAAAACB' + 'QAAAAgAAF2Z1bmNfbWFpbl8wABNMX0dMT0JBTDsAAAAAAAEAAQIAAAEA///MAAAAiA' + 'IBIwEAAAIABVQBAAAGFgEAAAAVU2xvdE51bWJlcgAAAAEACgEAAAYAAAA3CQMoAESQ' + 'RKFEsm1hBWAFQgAAAGEGPgEAYQcCYQhgBisCBwhhBGAEQgQCAGQLawEPAP////8PAA' + 'IAIQAAAE8BAAA=') + response = await self.debugger_impl.send("Debugger.evaluateOnCallFrame", worker_thread, params) + assert response['result']['result']['type'] == 'object' + # set worker.workerPort = undefined + params = debugger.EvaluateOnCallFrameParams('UEFOREEAAAAAAAAADAACAGQBAAAAAAAAAAAAAAIAAAA8AAAAAQAAAGABAAAAAAAARA' + 'AAAAEAAABEAAAApgAAANkAAACAAAAAZAEAAAIAAABsAAAAAwAAAHQAAAD/////////' + '////////////pgAAANkAAACAAAAAkgAAAJoAAAAhZGVidWdnZXJHZXRWYWx1ZQANd2' + '9ya2VyABV3b3JrZXJQb3J0ADNMX0VTU2xvdE51bWJlckFubm90YXRpb247AAAAAACB' + 'QAAAAgAAF2Z1bmNfbWFpbl8wABNMX0dMT0JBTDsAAAAAAAEAAQIAAAEA///MAAAAiA' + 'IBIwEAAAIABVQBAAAGFgEAAAAVU2xvdE51bWJlcgAAAAEACgEAAAYAAAA3CQMoAESQ' + 'RKFEsm1hBWAFQgAAAGEGPgEAYQcCYQhgBisCBwhhBABDBAIABGQLawEPAP////8PAA' + 'IAIQAAAE8BAAA=') + response = await self.debugger_impl.send("Debugger.evaluateOnCallFrame", worker_thread, params) + assert response['result']['result']['type'] == 'undefined' + # watch worker.workerPort + params = debugger.EvaluateOnCallFrameParams('UEFOREEAAAAAAAAADAACAGQBAAAAAAAAAAAAAAIAAAA8AAAAAQAAAGABAAAAAAAARA' + 'AAAAEAAABEAAAApgAAANkAAACAAAAAZAEAAAIAAABsAAAAAwAAAHQAAAD/////////' + '////////////pgAAANkAAACAAAAAkgAAAJoAAAAhZGVidWdnZXJHZXRWYWx1ZQANd2' + '9ya2VyABV3b3JrZXJQb3J0ADNMX0VTU2xvdE51bWJlckFubm90YXRpb247AAAAAACB' + 'QAAAAgAAF2Z1bmNfbWFpbl8wABNMX0dMT0JBTDsAAAAAAAEAAQIAAAEA///MAAAAiA' + 'IBIwEAAAIABVQBAAAGFgEAAAAVU2xvdE51bWJlcgAAAAEACgEAAAYAAAA3CQMoAESQ' + 'RKFEsm1hBWAFQgAAAGEGPgEAYQcCYQhgBisCBwhhBGAEQgQCAGQLawEPAP////8PAA' + 'IAIQAAAE8BAAA=') + response = await self.debugger_impl.send("Debugger.evaluateOnCallFrame", worker_thread, params) + assert response['result']['result']['type'] == 'undefined' + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread) + ################################################################################################################ + # main thread: click on the screen + ################################################################################################################ + Application.click_on_middle() + ################################################################################################################ + # worker thread: destroy instance + ################################################################################################################ + response = await self.debugger_impl.destroy_instance() + assert response['instanceId'] == worker_thread.instance_id + ################################################################################################################ + # main thread: Debugger.disable + ################################################################################################################ + await self.debugger_impl.send("Debugger.disable", main_thread) + ################################################################################################################ + # close the websocket connections + ################################################################################################################ + await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') + await websocket.send_msg_to_connect_server('close') + ################################################################################################################ \ No newline at end of file diff --git a/test/autotest/scenario_test/test_debug_08.py b/test/autotest/scenario_test/test_debug_08.py new file mode 100644 index 0000000000000000000000000000000000000000..7e6f2656c40754dc2076e420a7f173a7d713f7e9 --- /dev/null +++ b/test/autotest/scenario_test/test_debug_08.py @@ -0,0 +1,215 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Copyright (c) 2024 Huawei Device Co., Ltd. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Description: Scenario test case. +""" + +import logging +import os +import time + +import pytest + +from aw import Application +from aw import Utils +from aw.api import debugger_api, runtime_api + + +@pytest.mark.debug +@pytest.mark.timeout(30) +class TestDebug08: + """ + 测试用例:多实例 debug 暂停恢复调试 + """ + + def setup_method(self): + logging.info('Start running TestDebug08: setup') + + self.log_path = rf'{os.path.dirname(__file__)}\..\log' + self.hilog_file_name = 'test_debug_08.hilog.txt' + self.id_generator = Utils.message_id_generator() + + # receive the hilog before the test start + Utils.clear_fault_log() + self.hilog_process, self.write_thread = Utils.save_hilog(log_path=self.log_path, + file_name=self.hilog_file_name, + debug_on=True) + + def teardown_method(self): + Application.uninstall(self.config['bundle_name']) + + # terminate the hilog receive process after the test done + time.sleep(3) + self.hilog_process.stdout.close() + self.hilog_process.terminate() + self.hilog_process.wait() + self.write_thread.join() + + Utils.save_fault_log(log_path=self.log_path) + logging.info('TestDebug08 done') + + def test(self, test_suite_worker_05_debug): + logging.info('Start running TestDebug08: test') + self.config = test_suite_worker_05_debug + websocket = self.config['websocket'] + taskpool = self.config['taskpool'] + pid = self.config['pid'] + self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) + self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) + + taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) + taskpool.await_taskpool() + taskpool.task_join() + if taskpool.task_exception: + raise taskpool.task_exception + + async def procedure(self, websocket): + ################################################################################################################ + # main thread: connect the debugger server + ################################################################################################################ + main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) + logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') + ################################################################################################################ + # main thread: Runtime.enable + ################################################################################################################ + await self.runtime_impl.send("Runtime.enable", main_thread) + ################################################################################################################ + # main thread: Debugger.enable + ################################################################################################################ + await self.debugger_impl.send("Debugger.enable", main_thread) + ################################################################################################################ + # main thread: Runtime.runIfWaitingForDebugger + ################################################################################################################ + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", main_thread) + ################################################################################################################ + # main thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) + assert response['params']['url'] == self.config['file_path']['entry_ability'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # main thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['entry_ability'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # main thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) + assert response['params']['url'] == self.config['file_path']['index'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # main thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # main thread: click on the screen + ################################################################################################################ + Application.click_on_middle() + ################################################################################################################ + # worker thread: connect the debugger server + ################################################################################################################ + worker_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread.instance_id}') + ################################################################################################################ + # worker thread: Runtime.enable + ################################################################################################################ + await self.runtime_impl.send("Runtime.enable", worker_thread) + ################################################################################################################ + # worker thread: Debugger.enable + ################################################################################################################ + await self.debugger_impl.send("Debugger.enable", worker_thread) + ################################################################################################################ + # worker thread: Runtime.runIfWaitingForDebugger + ################################################################################################################ + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", worker_thread) + # worker thread: Debugger.scriptParsed + response = await self.debugger_impl.recv("Debugger.scriptParsed", worker_thread) + assert response['params']['url'] == self.config['file_path']['worker'] + assert response['params']['endLine'] == 0 + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread) + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'other' + assert response['params']['hitBreakpoints'] == ['id:0:0:'] + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread) + ################################################################################################################ + # all thread: sleep 2 seconds + ################################################################################################################ + time.sleep(2) + ################################################################################################################ + # worker thread: Debugger.pause + ################################################################################################################ + await self.debugger_impl.send("Debugger.pause", worker_thread) + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'other' + assert response['params']['hitBreakpoints'] == [] + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread) + ################################################################################################################ + # worker thread: Debugger.disable + ################################################################################################################ + await self.debugger_impl.send("Debugger.disable", worker_thread) + ################################################################################################################ + # main thread: Debugger.pause + ################################################################################################################ + await self.debugger_impl.send("Debugger.pause", main_thread) + # main thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] + assert response['params']['reason'] == 'other' + assert response['params']['hitBreakpoints'] == [] + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # main thread: Debugger.disable + ################################################################################################################ + await self.debugger_impl.send("Debugger.disable", main_thread) + ################################################################################################################ + # close the websocket connections + ################################################################################################################ + await websocket.send_msg_to_debugger_server(worker_thread.instance_id, worker_thread.send_msg_queue, 'close') + await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') + await websocket.send_msg_to_connect_server('close') + ################################################################################################################ \ No newline at end of file diff --git a/test/autotest/scenario_test/test_debug_09.py b/test/autotest/scenario_test/test_debug_09.py new file mode 100644 index 0000000000000000000000000000000000000000..fea015d9a61e3e1e24fdbb5b8691a4c13212e398 --- /dev/null +++ b/test/autotest/scenario_test/test_debug_09.py @@ -0,0 +1,266 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Copyright (c) 2024 Huawei Device Co., Ltd. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Description: Scenario test case. +""" + +import logging +import os +import time + +import pytest + +from aw import Application +from aw import Utils +from aw import debugger +from aw.api import debugger_api, runtime_api + + +@pytest.mark.debug +@pytest.mark.timeout(30) +class TestDebug09: + """ + 测试用例:多实例 debug smartStepInto 调试 + """ + + def setup_method(self): + logging.info('Start running TestDebug09: setup') + + self.log_path = rf'{os.path.dirname(__file__)}\..\log' + self.hilog_file_name = 'test_debug_09.hilog.txt' + self.id_generator = Utils.message_id_generator() + + # receive the hilog before the test start + Utils.clear_fault_log() + self.hilog_process, self.write_thread = Utils.save_hilog(log_path=self.log_path, + file_name=self.hilog_file_name, + debug_on=True) + + def teardown_method(self): + Application.uninstall(self.config['bundle_name']) + + # terminate the hilog receive process after the test done + time.sleep(3) + self.hilog_process.stdout.close() + self.hilog_process.terminate() + self.hilog_process.wait() + self.write_thread.join() + + Utils.save_fault_log(log_path=self.log_path) + logging.info('TestDebug09 done') + + def test(self, test_suite_worker_06_debug): + logging.info('Start running TestDebug09: test') + self.config = test_suite_worker_06_debug + websocket = self.config['websocket'] + taskpool = self.config['taskpool'] + pid = self.config['pid'] + self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) + self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) + + taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) + taskpool.await_taskpool() + taskpool.task_join() + if taskpool.task_exception: + raise taskpool.task_exception + + async def procedure(self, websocket): + ################################################################################################################ + # main thread: connect the debugger server + ################################################################################################################ + main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) + logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') + ################################################################################################################ + # main thread: Runtime.enable + ################################################################################################################ + await self.runtime_impl.send("Runtime.enable", main_thread) + ################################################################################################################ + # main thread: Debugger.enable + ################################################################################################################ + await self.debugger_impl.send("Debugger.enable", main_thread) + ################################################################################################################ + # main thread: Runtime.runIfWaitingForDebugger + ################################################################################################################ + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", main_thread) + ################################################################################################################ + # main thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) + assert response['params']['url'] == self.config['file_path']['entry_ability'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # main thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['entry_ability'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # main thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) + assert response['params']['url'] == self.config['file_path']['index'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # main thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # main thread: click on the screen + ################################################################################################################ + Application.click_on_middle() + ################################################################################################################ + # worker thread: connect the debugger server + ################################################################################################################ + worker_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread.instance_id}') + ################################################################################################################ + # worker thread: Runtime.enable + ################################################################################################################ + await self.runtime_impl.send("Runtime.enable", worker_thread) + ################################################################################################################ + # worker thread: Debugger.enable + ################################################################################################################ + await self.debugger_impl.send("Debugger.enable", worker_thread) + ################################################################################################################ + # worker thread: Runtime.runIfWaitingForDebugger + ################################################################################################################ + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", worker_thread) + ################################################################################################################ + # worker thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", worker_thread) + assert response['params']['url'] == self.config['file_path']['worker'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # worker thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # worker thread: Debugger.removeBreakpointsByUrl + ################################################################################################################ + params = debugger.RemoveBreakpointsUrl(self.config['file_path']['worker']) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", worker_thread, params) + ################################################################################################################ + # worker thread: Debugger.getPossibleAndSetBreakpointByUrl + ################################################################################################################ + locations = [debugger.BreakLocationUrl(url=self.config['file_path']['worker'], line_number=12)] + params = debugger.SetBreakpointsLocations(locations) + response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", + worker_thread, params) + assert response['result']['locations'][0]['id'] == 'id:12:0:' + self.config['file_path']['worker'] + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread) + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'other' + assert response['params']['hitBreakpoints'] == ['id:12:4:' + self.config['file_path']['worker']] + ################################################################################################################ + # worker thread: Debugger.smartStepInto + ################################################################################################################ + params = debugger.SmartStepIntoParams(url=self.config['file_path']['worker'], line_number=62) + await self.debugger_impl.send("Debugger.smartStepInto", worker_thread, params) + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread) + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['callFrames'][0]['functionName'] == 'introduce' + assert response['params']['reason'] == 'other' + assert response['params']['hitBreakpoints'] == ['id:62:8:' + self.config['file_path']['worker']] + ################################################################################################################ + # worker thread: Debugger.stepOut + ################################################################################################################ + await self.debugger_impl.send("Debugger.stepOut", worker_thread) + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'other' + assert response['params']['hitBreakpoints'] == [] + ################################################################################################################ + # worker thread: Debugger.smartStepInto + ################################################################################################################ + params = debugger.SmartStepIntoParams(url=self.config['file_path']['worker'], line_number=41) + await self.debugger_impl.send("Debugger.smartStepInto", worker_thread, params) + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread) + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['callFrames'][0]['functionName'] == 'add' + assert response['params']['reason'] == 'other' + assert response['params']['hitBreakpoints'] == ['id:41:17:' + self.config['file_path']['worker']] + ################################################################################################################ + # worker thread: Debugger.stepOut + ################################################################################################################ + await self.debugger_impl.send("Debugger.stepOut", worker_thread) + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'other' + assert response['params']['hitBreakpoints'] == [] + ################################################################################################################ + # worker thread: Debugger.smartStepInto + ################################################################################################################ + params = debugger.SmartStepIntoParams(url=self.config['file_path']['worker'], line_number=45) + await self.debugger_impl.send("Debugger.smartStepInto", worker_thread, params) + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread) + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['callFrames'][0]['functionName'] == 'sub' + assert response['params']['reason'] == 'other' + assert response['params']['hitBreakpoints'] == ['id:45:17:' + self.config['file_path']['worker']] + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread) + ################################################################################################################ + # worker thread: Debugger.disable + ################################################################################################################ + await self.debugger_impl.send("Debugger.disable", worker_thread) + ################################################################################################################ + # main thread: Debugger.disable + ################################################################################################################ + await self.debugger_impl.send("Debugger.disable", main_thread) + ################################################################################################################ + # close the websocket connections + ################################################################################################################ + await websocket.send_msg_to_debugger_server(worker_thread.instance_id, worker_thread.send_msg_queue, 'close') + await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') + await websocket.send_msg_to_connect_server('close') + ################################################################################################################ \ No newline at end of file diff --git a/test/autotest/scenario_test/test_debug_10.py b/test/autotest/scenario_test/test_debug_10.py new file mode 100644 index 0000000000000000000000000000000000000000..bbba515fd69700eb348e057506919556896e8f72 --- /dev/null +++ b/test/autotest/scenario_test/test_debug_10.py @@ -0,0 +1,199 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Copyright (c) 2024 Huawei Device Co., Ltd. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Description: Scenario test case. +""" + +import logging +import os +import time + +import pytest + +from aw import Application +from aw import Utils +from aw import debugger +from aw.api import debugger_api, runtime_api + + +@pytest.mark.debug +@pytest.mark.timeout(30) +class TestDebug10: + """ + 测试用例:主线程 debug 混合调试 + """ + + def setup_method(self): + logging.info('Start running TestDebug10: setup') + + self.log_path = rf'{os.path.dirname(__file__)}\..\log' + self.hilog_file_name = 'test_debug_10.hilog.txt' + self.id_generator = Utils.message_id_generator() + + # receive the hilog before the test start + Utils.clear_fault_log() + self.hilog_process, self.write_thread = Utils.save_hilog(log_path=self.log_path, + file_name=self.hilog_file_name, + debug_on=True) + + def teardown_method(self): + Application.uninstall(self.config['bundle_name']) + + # terminate the hilog receive process after the test done + time.sleep(3) + self.hilog_process.stdout.close() + self.hilog_process.terminate() + self.hilog_process.wait() + self.write_thread.join() + + Utils.save_fault_log(log_path=self.log_path) + logging.info('TestDebug10 done') + + def test(self, test_suite_native_01_debug): + logging.info('Start running TestDebug10: test') + self.config = test_suite_native_01_debug + websocket = self.config['websocket'] + taskpool = self.config['taskpool'] + pid = self.config['pid'] + self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) + self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) + + taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) + taskpool.await_taskpool() + taskpool.task_join() + if taskpool.task_exception: + raise taskpool.task_exception + + async def procedure(self, websocket): + ################################################################################################################ + # main thread: connect the debugger server + ################################################################################################################ + main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) + logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') + ################################################################################################################ + # main thread: Runtime.enable + ################################################################################################################ + await self.runtime_impl.send("Runtime.enable", main_thread) + ################################################################################################################ + # main thread: Debugger.enable + ################################################################################################################ + await self.debugger_impl.send("Debugger.enable", main_thread) + ################################################################################################################ + # main thread: Debugger.setMixedDebugEnabled + ################################################################################################################ + params = debugger.SetMixedDebugEnabledParams(enabled=True, mixed_stack_enabled=False) + await self.runtime_impl.send("Debugger.setMixedDebugEnabled", main_thread, params) + ################################################################################################################ + # main thread: Runtime.runIfWaitingForDebugger + ################################################################################################################ + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", main_thread) + ################################################################################################################ + # main thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) + assert response['params']['url'] == self.config['file_path']['entry_ability'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # main thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['entry_ability'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # main thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) + assert response['params']['url'] == self.config['file_path']['index'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # main thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # main thread: Debugger.removeBreakpointsByUrl + ################################################################################################################ + params = debugger.RemoveBreakpointsUrl(self.config['file_path']['index']) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", main_thread, params) + ################################################################################################################ + # main thread: Debugger.getPossibleAndSetBreakpointByUrl + ################################################################################################################ + locations = [debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=53), + debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=54)] + params = debugger.SetBreakpointsLocations(locations) + response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", + main_thread, params) + assert response['result']['locations'][0]['id'] == 'id:53:0:' + self.config['file_path']['index'] + assert response['result']['locations'][1]['id'] == 'id:54:0:' + self.config['file_path']['index'] + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # main thread: click on the screen + ################################################################################################################ + Application.click_on_middle() + ################################################################################################################ + # main thread: Debugger.paused, hit breakpoint + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] + assert response['params']['hitBreakpoints'] == ['id:53:26:' + self.config['file_path']['index']] + ################################################################################################################ + # main thread: Debugger.stepInto + ################################################################################################################ + await self.debugger_impl.send("Debugger.stepInto", main_thread) + ################################################################################################################ + # main thread: Debugger.nativeCalling + ################################################################################################################ + response = await self.debugger_impl.send("Debugger.nativeCalling", main_thread) + assert response['params']['isStepInto'] is True + ################################################################################################################ + # main thread: Debugger.replyNativeCalling + ################################################################################################################ + params = debugger.ReplyNativeCallingParams() + await self.debugger_impl.send("Debugger.replyNativeCalling", main_thread, params) + # main thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] + assert response['params']['hitBreakpoints'] == ['id:54:26:' + self.config['file_path']['index']] + ################################################################################################################ + # main thread: Debugger.stepOver + ################################################################################################################ + await self.debugger_impl.send("Debugger.stepOver", main_thread) + # main thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] + assert response['params']['hitBreakpoints'] == [] + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # main thread: Debugger.disable + ################################################################################################################ + await self.debugger_impl.send("Debugger.disable", main_thread) + ################################################################################################################ + # close the websocket connections + ################################################################################################################ + await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') + await websocket.send_msg_to_connect_server('close') + ################################################################################################################ \ No newline at end of file diff --git a/test/autotest/scenario_test/test_debug_11.py b/test/autotest/scenario_test/test_debug_11.py new file mode 100644 index 0000000000000000000000000000000000000000..a98ccfd3977cc7867f0a0dcc185c3fe805c5c263 --- /dev/null +++ b/test/autotest/scenario_test/test_debug_11.py @@ -0,0 +1,237 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Copyright (c) 2024 Huawei Device Co., Ltd. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Description: Scenario test case. +""" + +import logging +import os +import time + +import pytest + +from aw import Application +from aw import Utils +from aw import debugger +from aw.api import debugger_api, runtime_api + + +@pytest.mark.debug +@pytest.mark.timeout(60) +class TestDebug11: + """ + 测试用例:worker 线程 debug 混合调试 + """ + + def setup_method(self): + logging.info('Start running TestDebug11: setup') + + self.log_path = rf'{os.path.dirname(__file__)}\..\log' + self.hilog_file_name = 'test_debug_11.hilog.txt' + self.id_generator = Utils.message_id_generator() + + # receive the hilog before the test start + Utils.clear_fault_log() + self.hilog_process, self.write_thread = Utils.save_hilog(log_path=self.log_path, + file_name=self.hilog_file_name, + debug_on=True) + + def teardown_method(self): + Application.uninstall(self.config['bundle_name']) + + # terminate the hilog receive process after the test done + time.sleep(3) + self.hilog_process.stdout.close() + self.hilog_process.terminate() + self.hilog_process.wait() + self.write_thread.join() + + Utils.save_fault_log(log_path=self.log_path) + logging.info('TestDebug11 done') + + def test(self, test_suite_worker_06_debug): + logging.info('Start running TestDebug11: test') + self.config = test_suite_worker_06_debug + websocket = self.config['websocket'] + taskpool = self.config['taskpool'] + pid = self.config['pid'] + self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) + self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) + + taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) + taskpool.await_taskpool() + taskpool.task_join() + if taskpool.task_exception: + raise taskpool.task_exception + + async def procedure(self, websocket): + ################################################################################################################ + # main thread: connect the debugger server + ################################################################################################################ + main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) + logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') + ################################################################################################################ + # main thread: Runtime.enable + ################################################################################################################ + await self.runtime_impl.send("Runtime.enable", main_thread) + ################################################################################################################ + # main thread: Debugger.enable + ################################################################################################################ + await self.debugger_impl.send("Debugger.enable", main_thread) + ################################################################################################################ + # main thread: Debugger.setMixedDebugEnabled + ################################################################################################################ + params = debugger.SetMixedDebugEnabledParams(enabled=True, mixed_stack_enabled=False) + await self.runtime_impl.send("Debugger.setMixedDebugEnabled", main_thread, params) + ################################################################################################################ + # main thread: Runtime.runIfWaitingForDebugger + ################################################################################################################ + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", main_thread) + ################################################################################################################ + # main thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) + assert response['params']['url'] == self.config['file_path']['entry_ability'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # main thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['entry_ability'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # main thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) + assert response['params']['url'] == self.config['file_path']['index'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # main thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # main thread: click on the screen + ################################################################################################################ + Application.click_on_middle() + ################################################################################################################ + # worker thread: connect the debugger server + ################################################################################################################ + worker_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread.instance_id}') + ################################################################################################################ + # worker thread: Runtime.enable + ################################################################################################################ + await self.runtime_impl.send("Runtime.enable", worker_thread) + ################################################################################################################ + # worker thread: Debugger.enable + ################################################################################################################ + await self.debugger_impl.send("Debugger.enable", worker_thread) + ################################################################################################################ + # worker thread: Debugger.setMixedDebugEnabled + ################################################################################################################ + params = debugger.SetMixedDebugEnabledParams(enabled=True, mixed_stack_enabled=False) + await self.runtime_impl.send("Debugger.setMixedDebugEnabled", worker_thread, params) + ################################################################################################################ + # worker thread: Runtime.runIfWaitingForDebugger + ################################################################################################################ + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", worker_thread) + ################################################################################################################ + # worker thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", worker_thread) + assert response['params']['url'] == self.config['file_path']['worker'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # worker thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # worker thread: Debugger.removeBreakpointsByUrl + ################################################################################################################ + params = debugger.RemoveBreakpointsUrl(self.config['file_path']['worker']) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", worker_thread, params) + ################################################################################################################ + # worker thread: Debugger.getPossibleAndSetBreakpointByUrl + ################################################################################################################ + locations = [debugger.BreakLocationUrl(url=self.config['file_path']['worker'], line_number=12), + debugger.BreakLocationUrl(url=self.config['file_path']['worker'], line_number=13)] + params = debugger.SetBreakpointsLocations(locations) + response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", + worker_thread, params) + assert response['result']['locations'][0]['id'] == 'id:12:0:' + self.config['file_path']['worker'] + assert response['result']['locations'][1]['id'] == 'id:13:0:' + self.config['file_path']['worker'] + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread) + # worker thread: Debugger.paused + await self.debugger_impl.send("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'other' + assert response['params']['hitBreakpoints'] == ['id:12:14:' + self.config['file_path']['worker']] + ################################################################################################################ + # worker thread: Debugger.stepInto + ################################################################################################################ + await self.debugger_impl.send("Debugger.stepInto", worker_thread) + ################################################################################################################ + # worker thread: Debugger.nativeCalling + ################################################################################################################ + response = await self.debugger_impl.send("Debugger.nativeCalling", worker_thread) + assert response['params']['isStepInto'] is True + ################################################################################################################ + # worker thread: Debugger.replyNativeCalling + ################################################################################################################ + params = debugger.ReplyNativeCallingParams() + await self.debugger_impl.send("Debugger.replyNativeCalling", worker_thread, params) + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['hitBreakpoints'] == ['id:13:14:' + self.config['file_path']['worker']] + ################################################################################################################ + # worker thread: Debugger.stepOver + ################################################################################################################ + await self.debugger_impl.send("Debugger.stepOver", worker_thread) + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['hitBreakpoints'] == [] + ################################################################################################################ + # worker thread: Debugger.disable + ################################################################################################################ + await self.debugger_impl.send("Debugger.disable", worker_thread) + ################################################################################################################ + # main thread: Debugger.disable + ################################################################################################################ + await self.debugger_impl.send("Debugger.disable", main_thread) + ################################################################################################################ + # close the websocket connections + ################################################################################################################ + await websocket.send_msg_to_debugger_server(worker_thread.instance_id, worker_thread.send_msg_queue, 'close') + await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') + await websocket.send_msg_to_connect_server('close') + ################################################################################################################ \ No newline at end of file diff --git a/test/autotest/scenario_test/test_debug_12.py b/test/autotest/scenario_test/test_debug_12.py new file mode 100644 index 0000000000000000000000000000000000000000..2b10544788acbc3ab09f3d2fea0f21b9a39a99ee --- /dev/null +++ b/test/autotest/scenario_test/test_debug_12.py @@ -0,0 +1,235 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Copyright (c) 2024 Huawei Device Co., Ltd. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Description: Scenario test case. +""" + +import logging +import os +import time + +import pytest + +from aw import Application +from aw import Utils +from aw import debugger +from aw.api import debugger_api, runtime_api + + +@pytest.mark.debug +@pytest.mark.timeout(30) +class TestDebug12: + """ + 测试用例:多实例 debug 调试之设置断点 + """ + + def setup_method(self): + logging.info('Start running TestDebug12: setup') + + self.log_path = rf'{os.path.dirname(__file__)}\..\log' + self.hilog_file_name = 'test_debug_12.hilog.txt' + self.id_generator = Utils.message_id_generator() + + # receive the hilog before the test start + Utils.clear_fault_log() + self.hilog_process, self.write_thread = Utils.save_hilog(log_path=self.log_path, + file_name=self.hilog_file_name, + debug_on=True) + + def teardown_method(self): + Application.uninstall(self.config['bundle_name']) + + # terminate the hilog receive process after the test done + time.sleep(3) + self.hilog_process.stdout.close() + self.hilog_process.terminate() + self.hilog_process.wait() + self.write_thread.join() + + Utils.save_fault_log(log_path=self.log_path) + logging.info('TestDebug12 done') + + def test(self, test_suite_worker_06_debug): + logging.info('Start running TestDebug12: test') + self.config = test_suite_worker_06_debug + websocket = self.config['websocket'] + taskpool = self.config['taskpool'] + pid = self.config['pid'] + self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) + self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) + + taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) + taskpool.await_taskpool() + taskpool.task_join() + if taskpool.task_exception: + raise taskpool.task_exception + + async def procedure(self, websocket): + ################################################################################################################ + # main thread: connect the debugger server + ################################################################################################################ + main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) + logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') + ################################################################################################################ + # main thread: Runtime.enable + ################################################################################################################ + await self.runtime_impl.send("Runtime.enable", main_thread) + ################################################################################################################ + # main thread: Debugger.enable + ################################################################################################################ + await self.debugger_impl.send("Debugger.enable", main_thread) + ################################################################################################################ + # main thread: Runtime.runIfWaitingForDebugger + ################################################################################################################ + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", main_thread) + ################################################################################################################ + # main thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) + assert response['params']['url'] == self.config['file_path']['entry_ability'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # main thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['entry_ability'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # main thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) + assert response['params']['url'] == self.config['file_path']['index'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # main thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # main thread: click on the screen + ################################################################################################################ + Application.click_on_middle() + ################################################################################################################ + # worker thread: connect the debugger server + ################################################################################################################ + worker_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread.instance_id}') + ################################################################################################################ + # worker thread: Runtime.enable + ################################################################################################################ + await self.runtime_impl.send("Runtime.enable", worker_thread) + ################################################################################################################ + # worker thread: Debugger.enable + ################################################################################################################ + await self.debugger_impl.send("Debugger.enable", worker_thread) + ################################################################################################################ + # worker thread: Runtime.runIfWaitingForDebugger + ################################################################################################################ + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", worker_thread) + ################################################################################################################ + # worker thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", worker_thread) + assert response['params']['url'] == self.config['file_path']['worker'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # worker thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # worker thread: Debugger.removeBreakpointsByUrl + ################################################################################################################ + params = debugger.RemoveBreakpointsUrl(self.config['file_path']['worker']) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", worker_thread, params) + ################################################################################################################ + # worker thread: Debugger.getPossibleAndSetBreakpointByUrl + ################################################################################################################ + locations = [debugger.BreakLocationUrl(url=self.config['file_path']['worker'], line_number=23), + debugger.BreakLocationUrl(url=self.config['file_path']['worker'], line_number=62), + debugger.BreakLocationUrl(url=self.config['file_path']['worker'], line_number=42, + condition='UEFOREEAAAAAAAAADAACAFQBAAAAAAAAAAAAAAIAAAA8AAAAAQAAAFABAAAAA' + 'AAARAAAAAEAAABEAAAAlgAAAMkAAAB8AAAAVAEAAAIAAABsAAAAAgAAAHQAAA' + 'D/////////////////////lgAAAMkAAAB8AAAAjgAAACFkZWJ1Z2dlckdldFZ' + 'hbHVlAA1yZXN1bHQAM0xfRVNTbG90TnVtYmVyQW5ub3RhdGlvbjsAAAAAAIFA' + 'AAACAAAXZnVuY19tYWluXzAAE0xfR0xPQkFMOwAAAAAAAQABAgAAAQD//7wAA' + 'ACIAgETAQAAAgAFRgEAAAYGAQAAABVTbG90TnVtYmVyAAAAAQD6AAAABQAAAD' + 'cJAyoARJBEoUSybWEFYAVCAAAAYQY+AQBhBwJhCGAGKwIHCGEEYgkAAAAPBAR' + 'kC2sBDwD/////DwACACMAQQEAAA==')] + params = debugger.SetBreakpointsLocations(locations) + response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", + worker_thread, params) + assert response['result']['locations'][0]['id'] == 'id:23:0:' + self.config['file_path']['worker'] + assert response['result']['locations'][1]['id'] == 'id:62:0:' + self.config['file_path']['worker'] + assert response['result']['locations'][2]['id'] == 'id:42:0:' + self.config['file_path']['worker'] + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread) + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['callFrames'][0]['functionName'] == 'introduce' + assert response['params']['reason'] == 'other' + assert response['params']['hitBreakpoints'] == ['id:62:8:' + self.config['file_path']['worker']] + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread) + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['callFrames'][0]['functionName'] == 'add' + assert response['params']['reason'] == 'other' + assert response['params']['hitBreakpoints'] == ['id:42:11:' + self.config['file_path']['worker']] + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread) + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['worker'] + assert response['params']['reason'] == 'other' + assert response['params']['hitBreakpoints'] == ['id:23:1:' + self.config['file_path']['worker']] + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread) + ################################################################################################################ + # worker thread: Debugger.disable + ################################################################################################################ + await self.debugger_impl.send("Debugger.disable", worker_thread) + ################################################################################################################ + # main thread: Debugger.disable + ################################################################################################################ + await self.debugger_impl.send("Debugger.disable", main_thread) + ################################################################################################################ + # close the websocket connections + ################################################################################################################ + await websocket.send_msg_to_debugger_server(worker_thread.instance_id, worker_thread.send_msg_queue, 'close') + await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') + await websocket.send_msg_to_connect_server('close') + ################################################################################################################ \ No newline at end of file diff --git a/test/autotest/scenario_test/test_debug_13.py b/test/autotest/scenario_test/test_debug_13.py new file mode 100644 index 0000000000000000000000000000000000000000..24c488acff3554d83ab065b23983079aa8331441 --- /dev/null +++ b/test/autotest/scenario_test/test_debug_13.py @@ -0,0 +1,222 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Copyright (c) 2024 Huawei Device Co., Ltd. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Description: Scenario test case. +""" + +import logging +import os +import time + +import pytest + +from aw import Application +from aw import Utils +from aw import debugger, runtime +from aw.api import debugger_api, runtime_api + + +@pytest.mark.debug +@pytest.mark.timeout(30) +class TestDebug13: + """ + 测试用例:跨线程传递 Sendable 对象 + """ + + def setup_method(self): + logging.info('Start running TestDebug13: setup') + + self.log_path = rf'{os.path.dirname(__file__)}\..\log' + self.hilog_file_name = 'test_debug_13.hilog.txt' + self.id_generator = Utils.message_id_generator() + + # receive the hilog before the test start + Utils.clear_fault_log() + self.hilog_process, self.write_thread = Utils.save_hilog(log_path=self.log_path, + file_name=self.hilog_file_name, + debug_on=True) + + def teardown_method(self): + Application.uninstall(self.config['bundle_name']) + + # terminate the hilog receive process after the test done + time.sleep(3) + self.hilog_process.stdout.close() + self.hilog_process.terminate() + self.hilog_process.wait() + self.write_thread.join() + + Utils.save_fault_log(log_path=self.log_path) + logging.info('TestDebug13 done') + + def test(self, test_suite_taskpool_02_debug): + logging.info('Start running TestDebug13: test') + self.config = test_suite_taskpool_02_debug + websocket = self.config['websocket'] + taskpool = self.config['taskpool'] + pid = self.config['pid'] + self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) + self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) + + taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) + taskpool.await_taskpool() + taskpool.task_join() + if taskpool.task_exception: + raise taskpool.task_exception + + async def procedure(self, websocket): + ################################################################################################################ + # main thread: connect the debugger server + ################################################################################################################ + main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) + logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') + ################################################################################################################ + # main thread: Runtime.enable + ################################################################################################################ + await self.runtime_impl.send("Runtime.enable", main_thread) + ################################################################################################################ + # main thread: Debugger.enable + ################################################################################################################ + await self.debugger_impl.send("Debugger.enable", main_thread) + ################################################################################################################ + # main thread: Runtime.runIfWaitingForDebugger + ################################################################################################################ + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", main_thread) + ################################################################################################################ + # main thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) + assert response['params']['url'] == self.config['file_path']['entry_ability'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # main thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['entry_ability'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # main thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) + assert response['params']['url'] == self.config['file_path']['index'] + assert response['params']['endLine'] == 0 + ################################################################################################################ + # main thread: Debugger.paused + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.paused", main_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # main thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", main_thread) + ################################################################################################################ + # worker thread: connect the debugger server + ################################################################################################################ + worker_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread.instance_id}') + ################################################################################################################ + # worker thread: Runtime.enable + ################################################################################################################ + await self.runtime_impl.send("Runtime.enable", worker_thread) + ################################################################################################################ + # worker thread: Debugger.enable + ################################################################################################################ + await self.debugger_impl.send("Debugger.enable", worker_thread) + ################################################################################################################ + # worker thread: Runtime.runIfWaitingForDebugger + ################################################################################################################ + await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", worker_thread) + ################################################################################################################ + # main thread: Debugger.removeBreakpointsByUrl + ################################################################################################################ + params = debugger.RemoveBreakpointsUrl(self.config['file_path']['index']) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", main_thread, params) + ################################################################################################################ + # main thread: Debugger.getPossibleAndSetBreakpointByUrl + ################################################################################################################ + locations = [debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=110)] + params = debugger.SetBreakpointsLocations(locations) + response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", + main_thread, params) + assert response['result']['locations'][0]['id'] == 'id:110:0:' + self.config['file_path']['index'] + ################################################################################################################ + # main thread: click on the screen + ################################################################################################################ + Application.click_on_middle() + ################################################################################################################ + # worker thread: Debugger.scriptParsed + ################################################################################################################ + response = await self.debugger_impl.recv("Debugger.scriptParsed", worker_thread) + assert response['params']['url'] == self.config['file_path']['index'] + assert response['params']['endLine'] == 0 + # worker thread: Debugger.paused + response = await self.debugger_impl.recv("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] + assert response['params']['reason'] == 'Break on start' + ################################################################################################################ + # worker thread: Debugger.removeBreakpointsByUrl + ################################################################################################################ + params = debugger.RemoveBreakpointsUrl(self.config['file_path']['index']) + await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", worker_thread, params) + ################################################################################################################ + # worker thread: Debugger.getPossibleAndSetBreakpointByUrl + ################################################################################################################ + locations = [debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=110)] + params = debugger.SetBreakpointsLocations(locations) + response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", + worker_thread, params) + assert response['result']['locations'][0]['id'] == 'id:110:0:' + self.config['file_path']['index'] + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread) + # worker thread: Debugger.paused + await self.debugger_impl.send("Debugger.paused", worker_thread) + assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] + assert response['params']['callFrames'][0]['functionName'] == 'calculate' + assert response['params']['reason'] == 'other' + assert response['params']['hitBreakpoints'] == ['id:110:21:' + self.config['file_path']['index']] + ################################################################################################################ + # worker thread: Runtime.getProperties + ################################################################################################################ + params = runtime.GetPropertiesParams('0') + response = await self.runtime_impl.send("Runtime.getProperties", worker_thread, params) + assert response['result']['result'][0]['name'] == 'calculate' + assert response['result']['result'][0]['value']['description'].endswith('[Sendable]') + ################################################################################################################ + # worker thread: Debugger.resume + ################################################################################################################ + await self.debugger_impl.send("Debugger.resume", worker_thread) + ################################################################################################################ + # worker thread: Debugger.disable + ################################################################################################################ + await self.debugger_impl.send("Debugger.disable", worker_thread) + ################################################################################################################ + # main thread: Debugger.disable + ################################################################################################################ + await self.debugger_impl.send("Debugger.disable", main_thread) + ################################################################################################################ + # close the websocket connections + ################################################################################################################ + await websocket.send_msg_to_debugger_server(worker_thread.instance_id, worker_thread.send_msg_queue, 'close') + await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') + await websocket.send_msg_to_connect_server('close') + ################################################################################################################ \ No newline at end of file diff --git a/test/autotest/scenario_test/test_debug_14.py b/test/autotest/scenario_test/test_debug_14.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/test/autotest/scenario_test/test_heap_profiler_01.py b/test/autotest/scenario_test/test_heap_profiler_01.py index 438efabfc5a92ed45066422255d7a1ca7b504ec2..e41496db29fd250990ed0dd73beb04747010d736 100644 --- a/test/autotest/scenario_test/test_heap_profiler_01.py +++ b/test/autotest/scenario_test/test_heap_profiler_01.py @@ -17,7 +17,6 @@ limitations under the License. Description: Scenario test case. """ -import json import logging import os import time @@ -26,8 +25,8 @@ import pytest from aw import Application from aw import Utils -from aw import communicate_with_debugger_server -from aw import debugger, runtime, heap_profiler +from aw import heap_profiler +from aw.api import debugger_api, runtime_api, heap_profiler_api @pytest.mark.heap_profiler @@ -80,9 +79,12 @@ class TestHeapProfiler01: websocket = self.config['websocket'] taskpool = self.config['taskpool'] pid = self.config['pid'] + self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) + self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) + self.heap_profiler_impl = heap_profiler_api.HeapProfilerImpl(self.id_generator, websocket) Application.attach(self.config['bundle_name']) - taskpool.submit(websocket.main_task(taskpool, websocket, self.procedure, pid)) + taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) taskpool.await_taskpool() taskpool.task_join() if taskpool.task_exception: @@ -92,124 +94,53 @@ class TestHeapProfiler01: ################################################################################################################ # main thread: connect the debugger server ################################################################################################################ - send_msg = {"type": "connected"} - await websocket.send_msg_to_connect_server(send_msg) - response = await websocket.recv_msg_of_connect_server() - response = json.loads(response) - assert response['type'] == 'addInstance' - assert response['instanceId'] == 0, logging.error('instance id of the main thread not equal to 0') - assert response['tid'] == self.config['pid'] - main_thread_instance_id = await websocket.get_instance() - main_thread_to_send_queue = websocket.to_send_msg_queues[main_thread_instance_id] - main_thread_received_queue = websocket.received_msg_queues[main_thread_instance_id] - logging.info(f'Connect to the debugger server of instance: {main_thread_instance_id}') + main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) + logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') ################################################################################################################ # worker thread: connect the debugger server ################################################################################################################ - workers_num = 2 - worker_instances_id = [] - worker_thread_to_send_queues = [] - worker_thread_received_queues = [] - for i in range(workers_num): - response = await websocket.recv_msg_of_connect_server() - response = json.loads(response) - assert response['type'] == 'addInstance' - assert response['instanceId'] != 0 - assert response['tid'] != self.config['pid'] - assert 'workerThread_' in response['name'] - worker_instance_id = await websocket.get_instance() - worker_instances_id.append(worker_instance_id) - worker_thread_to_send_queues.append(websocket.to_send_msg_queues[worker_instance_id]) - worker_thread_received_queues.append(websocket.received_msg_queues[worker_instance_id]) - logging.info(f'Connect to the debugger server of instance: {worker_instance_id}') + worker_thread_1 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread_1.instance_id}') + worker_thread_2 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread_2.instance_id}') ################################################################################################################ # main thread: Runtime.enable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - runtime.enable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {"protocols": []}} + await self.runtime_impl.send("Runtime.enable", main_thread) ################################################################################################################ # worker thread: Runtime.enable ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - runtime.enable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {"protocols": []}} + await self.runtime_impl.send("Runtime.enable", worker_thread_1) + await self.runtime_impl.send("Runtime.enable", worker_thread_2) ################################################################################################################ # main thread: Runtime.getHeapUsage ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - runtime.get_heap_usage(), message_id) - response = json.loads(response) - assert response['result']['usedSize'] > 0 - assert response['result']['totalSize'] > 0 + await self.runtime_impl.send("Runtime.getHeapUsage", main_thread) ################################################################################################################ # worker thread: Runtime.getHeapUsage ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - runtime.get_heap_usage(), message_id) - response = json.loads(response) - assert response['result']['usedSize'] > 0 - assert response['result']['totalSize'] > 0 + await self.runtime_impl.send("Runtime.getHeapUsage", worker_thread_1) + await self.runtime_impl.send("Runtime.getHeapUsage", worker_thread_2) ################################################################################################################ # main thread: HeapProfiler.startTrackingHeapObjects ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - heap_profiler.start_tracking_heap_objects(False), - message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + params = heap_profiler.TrackingHeapObjectsPrams(False) + await self.heap_profiler_impl.send("HeapProfiler.startTrackingHeapObjects", main_thread, params) ################################################################################################################ # worker thread: HeapProfiler.startTrackingHeapObjects ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - heap_profiler.start_tracking_heap_objects(False), - message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + params = heap_profiler.TrackingHeapObjectsPrams(False) + await self.heap_profiler_impl.send("HeapProfiler.startTrackingHeapObjects", worker_thread_1, params) + await self.heap_profiler_impl.send("HeapProfiler.startTrackingHeapObjects", worker_thread_2, params) ################################################################################################################ # main thread: Debugger.disable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.disable(), message_id) - while not json.loads(response).get('id', None): - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.disable", main_thread) ################################################################################################################ # worker thread: Debugger.disable ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - debugger.disable(), message_id) - while not json.loads(response).get('id', None): - response = await websocket.recv_msg_of_debugger_server(worker_instances_id[i], - worker_thread_received_queues[i]) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.disable", worker_thread_1) + await self.debugger_impl.send("Debugger.disable", worker_thread_2) ################################################################################################################ # all thread: sleep 10 seconds ################################################################################################################ @@ -217,84 +148,26 @@ class TestHeapProfiler01: ################################################################################################################ # main thread: Runtime.getHeapUsage ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - runtime.get_heap_usage(), message_id) - while response.startswith('{"method":"HeapProfiler.lastSeenObjectId"'): - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - response = json.loads(response) - assert response['result']['usedSize'] > 0 - assert response['result']['totalSize'] > 0 + await self.runtime_impl.send("Runtime.getHeapUsage", main_thread) ################################################################################################################ # worker thread: Runtime.getHeapUsage ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - runtime.get_heap_usage(), message_id) - while response.startswith('{"method":"HeapProfiler.lastSeenObjectId"'): - response = await websocket.recv_msg_of_debugger_server(worker_instances_id[i], - worker_thread_received_queues[i]) - response = json.loads(response) - assert response['result']['usedSize'] > 0 - assert response['result']['totalSize'] > 0 + await self.runtime_impl.send("Runtime.getHeapUsage", worker_thread_1) + await self.runtime_impl.send("Runtime.getHeapUsage", worker_thread_2) ################################################################################################################ # main thread: HeapProfiler.stopTrackingHeapObjects ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - heap_profiler.stop_tracking_heap_objects(), message_id) - while response.startswith('{"method":"HeapProfiler.lastSeenObjectId"'): - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - assert r'\"location_fields\":[\"object_index\",\"script_id\",\"line\",\"column\"]' in response - pre_response = response - while response.startswith('{"method":"HeapProfiler.addHeapSnapshotChunk"') or\ - response.startswith('{"method":"HeapProfiler.lastSeenObjectId"'): - if response.startswith('{"method":"HeapProfiler.addHeapSnapshotChunk"'): - pre_response = response - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - assert pre_response.endswith(r'\n]\n}\n"}}') - assert json.loads(response) == {"id": message_id, "result": {}} + await self.heap_profiler_impl.send("HeapProfiler.stopTrackingHeapObjects", main_thread, params) ################################################################################################################ # worker thread: HeapProfiler.stopTrackingHeapObjects ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - heap_profiler.stop_tracking_heap_objects(), message_id) - while response.startswith('{"method":"HeapProfiler.lastSeenObjectId"'): - response = await websocket.recv_msg_of_debugger_server(worker_instances_id[i], - worker_thread_received_queues[i]) - assert r'\"location_fields\":[\"object_index\",\"script_id\",\"line\",\"column\"]' in response - pre_response = response - while response.startswith('{"method":"HeapProfiler.addHeapSnapshotChunk"') or\ - response.startswith('{"method":"HeapProfiler.lastSeenObjectId"'): - if response.startswith('{"method":"HeapProfiler.addHeapSnapshotChunk"'): - pre_response = response - response = await websocket.recv_msg_of_debugger_server(worker_instances_id[i], - worker_thread_received_queues[i]) - assert pre_response.endswith(r'\n]\n}\n"}}') - assert json.loads(response) == {"id": message_id, "result": {}} - ################################################################################################################ - # worker thread: destroy instance - ################################################################################################################ - for i in range(workers_num): - await websocket.send_msg_to_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], 'close') + await self.heap_profiler_impl.send("HeapProfiler.stopTrackingHeapObjects", worker_thread_1, params) + await self.heap_profiler_impl.send("HeapProfiler.stopTrackingHeapObjects", worker_thread_2, params) ################################################################################################################ # close the websocket connections ################################################################################################################ - await websocket.send_msg_to_debugger_server(main_thread_instance_id, main_thread_to_send_queue, 'close') + await websocket.send_msg_to_debugger_server(worker_thread_1.instance_id, worker_thread_1.send_msg_queue, 'close') + await websocket.send_msg_to_debugger_server(worker_thread_2.instance_id, worker_thread_2.send_msg_queue, 'close') + await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') await websocket.send_msg_to_connect_server('close') ################################################################################################################ \ No newline at end of file diff --git a/test/autotest/scenario_test/test_heap_profiler_02.py b/test/autotest/scenario_test/test_heap_profiler_02.py index 77aee0a20132c74dcd34a6f182dae1177e894190..c785f7a85e82eed020077d534621a7be41915a49 100644 --- a/test/autotest/scenario_test/test_heap_profiler_02.py +++ b/test/autotest/scenario_test/test_heap_profiler_02.py @@ -17,7 +17,6 @@ limitations under the License. Description: Scenario test case. """ -import json import logging import os import time @@ -26,8 +25,7 @@ import pytest from aw import Application from aw import Utils -from aw import communicate_with_debugger_server -from aw import debugger, runtime, heap_profiler +from aw.api import debugger_api, runtime_api, heap_profiler_api @pytest.mark.heap_profiler @@ -79,9 +77,12 @@ class TestHeapProfiler02: websocket = self.config['websocket'] taskpool = self.config['taskpool'] pid = self.config['pid'] + self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) + self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) + self.heap_profiler_impl = heap_profiler_api.HeapProfilerImpl(self.id_generator, websocket) Application.attach(self.config['bundle_name']) - taskpool.submit(websocket.main_task(taskpool, websocket, self.procedure, pid)) + taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) taskpool.await_taskpool() taskpool.task_join() if taskpool.task_exception: @@ -91,110 +92,42 @@ class TestHeapProfiler02: ################################################################################################################ # main thread: connect the debugger server ################################################################################################################ - send_msg = {"type": "connected"} - await websocket.send_msg_to_connect_server(send_msg) - response = await websocket.recv_msg_of_connect_server() - response = json.loads(response) - assert response['type'] == 'addInstance' - assert response['instanceId'] == 0, logging.error('instance id of the main thread not equal to 0') - assert response['tid'] == self.config['pid'] - main_thread_instance_id = await websocket.get_instance() - main_thread_to_send_queue = websocket.to_send_msg_queues[main_thread_instance_id] - main_thread_received_queue = websocket.received_msg_queues[main_thread_instance_id] - logging.info(f'Connect to the debugger server of instance: {main_thread_instance_id}') + main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) + logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') ################################################################################################################ # worker thread: connect the debugger server ################################################################################################################ - workers_num = 2 - worker_instances_id = [] - worker_thread_to_send_queues = [] - worker_thread_received_queues = [] - for i in range(workers_num): - response = await websocket.recv_msg_of_connect_server() - response = json.loads(response) - assert response['type'] == 'addInstance' - assert response['instanceId'] != 0 - assert response['tid'] != self.config['pid'] - assert 'workerThread_' in response['name'] - worker_instance_id = await websocket.get_instance() - worker_instances_id.append(worker_instance_id) - worker_thread_to_send_queues.append(websocket.to_send_msg_queues[worker_instance_id]) - worker_thread_received_queues.append(websocket.received_msg_queues[worker_instance_id]) - logging.info(f'Connect to the debugger server of instance: {worker_instance_id}') + worker_thread_1 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread_1.instance_id}') + worker_thread_2 = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], False) + logging.info(f'Connect to the debugger server of instance: {worker_thread_2.instance_id}') ################################################################################################################ # main thread: Runtime.enable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - runtime.enable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {"protocols": []}} + await self.runtime_impl.send("Runtime.enable", main_thread) ################################################################################################################ # worker thread: Runtime.enable ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - runtime.enable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {"protocols": []}} + await self.runtime_impl.send("Runtime.enable", worker_thread_1) + await self.runtime_impl.send("Runtime.enable", worker_thread_2) ################################################################################################################ # main thread: Debugger.disable ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - debugger.disable(), message_id) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.disable", main_thread) ################################################################################################################ # worker thread: Debugger.disable ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - debugger.disable(), message_id) - while not json.loads(response).get('id', None): - response = await websocket.recv_msg_of_debugger_server(worker_instances_id[i], - worker_thread_received_queues[i]) - assert json.loads(response) == {"id": message_id, "result": {}} + await self.debugger_impl.send("Debugger.disable", worker_thread_1) + await self.debugger_impl.send("Debugger.disable", worker_thread_2) ################################################################################################################ # main thread: HeapProfiler.takeHeapSnapshot ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - heap_profiler.take_heap_snapshot(), message_id) - assert r'\"location_fields\":[\"object_index\",\"script_id\",\"line\",\"column\"]' in response - pre_response = response - while response.startswith('{"method":"HeapProfiler.addHeapSnapshotChunk"'): - pre_response = response - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - assert pre_response.endswith(r'\n]\n}\n"}}') - assert json.loads(response) == {"id": message_id, "result": {}} + await self.heap_profiler_impl.send("HeapProfiler.takeHeapSnapshot", main_thread) ################################################################################################################ # worker thread: HeapProfiler.takeHeapSnapshot ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - heap_profiler.take_heap_snapshot(), message_id) - assert r'\"location_fields\":[\"object_index\",\"script_id\",\"line\",\"column\"]' in response - pre_response = response - while response.startswith('{"method":"HeapProfiler.addHeapSnapshotChunk"'): - pre_response = response - response = await websocket.recv_msg_of_debugger_server(worker_instances_id[i], - worker_thread_received_queues[i]) - assert pre_response.endswith(r'\n]\n}\n"}}') - assert json.loads(response) == {"id": message_id, "result": {}} + await self.heap_profiler_impl.send("HeapProfiler.takeHeapSnapshot", worker_thread_1) + await self.heap_profiler_impl.send("HeapProfiler.takeHeapSnapshot", worker_thread_2) ################################################################################################################ # all thread: sleep 10 seconds ################################################################################################################ @@ -202,45 +135,17 @@ class TestHeapProfiler02: ################################################################################################################ # main thread: HeapProfiler.takeHeapSnapshot ################################################################################################################ - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(main_thread_instance_id, - main_thread_to_send_queue, - main_thread_received_queue, - heap_profiler.take_heap_snapshot(), message_id) - assert r'\"location_fields\":[\"object_index\",\"script_id\",\"line\",\"column\"]' in response - pre_response = response - while response.startswith('{"method":"HeapProfiler.addHeapSnapshotChunk"'): - pre_response = response - response = await websocket.recv_msg_of_debugger_server(main_thread_instance_id, - main_thread_received_queue) - assert pre_response.endswith(r'\n]\n}\n"}}') - assert json.loads(response) == {"id": message_id, "result": {}} + await self.heap_profiler_impl.send("HeapProfiler.takeHeapSnapshot", main_thread) ################################################################################################################ # worker thread: HeapProfiler.takeHeapSnapshot ################################################################################################################ - for i in range(workers_num): - message_id = next(self.id_generator) - response = await communicate_with_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], - worker_thread_received_queues[i], - heap_profiler.take_heap_snapshot(), message_id) - assert r'\"location_fields\":[\"object_index\",\"script_id\",\"line\",\"column\"]' in response - pre_response = response - while response.startswith('{"method":"HeapProfiler.addHeapSnapshotChunk"'): - pre_response = response - response = await websocket.recv_msg_of_debugger_server(worker_instances_id[i], - worker_thread_received_queues[i]) - assert pre_response.endswith(r'\n]\n}\n"}}') - assert json.loads(response) == {"id": message_id, "result": {}} - ################################################################################################################ - # worker thread: destroy instance - ################################################################################################################ - for i in range(workers_num): - await websocket.send_msg_to_debugger_server(worker_instances_id[i], - worker_thread_to_send_queues[i], 'close') + await self.heap_profiler_impl.send("HeapProfiler.takeHeapSnapshot", worker_thread_1) + await self.heap_profiler_impl.send("HeapProfiler.takeHeapSnapshot", worker_thread_2) ################################################################################################################ # close the websocket connections ################################################################################################################ - await websocket.send_msg_to_debugger_server(main_thread_instance_id, main_thread_to_send_queue, 'close') + await websocket.send_msg_to_debugger_server(worker_thread_1.instance_id, worker_thread_1.send_msg_queue, 'close') + await websocket.send_msg_to_debugger_server(worker_thread_2.instance_id, worker_thread_2.send_msg_queue, 'close') + await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') await websocket.send_msg_to_connect_server('close') ################################################################################################################ \ No newline at end of file