1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_HEAPSAMPLING_LOOP_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_HEAPSAMPLING_LOOP_TEST_H
18 
19 #include "tooling/test/client_utils/test_util.h"
20 
21 namespace panda::ecmascript::tooling::test {
22 class JsHeapSamplingLoopTest : public TestActions {
23 public:
JsHeapSamplingLoopTest()24     JsHeapSamplingLoopTest()
25     {
26         testAction = {
27             {SocketAction::SEND, "enable"},
28             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
29             {SocketAction::SEND, "runtime-enable"},
30             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
31             {SocketAction::SEND, "run"},
32             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
33             // load common_func.js
34             {SocketAction::RECV, "Debugger.scriptParsed", ActionRule::STRING_CONTAIN},
35             // break on start
36             {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
37             {SocketAction::SEND, "heapprofiler-enable"},
38             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
39             {SocketAction::SEND, "sampling"},
40             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
41             // set breakpoint
42             {SocketAction::SEND, "b " DEBUGGER_JS_DIR "common_func.js 64"},
43             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
44             {SocketAction::SEND, "b " DEBUGGER_JS_DIR "common_func.js 65"},
45             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
46             // hit breakpoint after resume first time
47             {SocketAction::SEND, "resume"},
48             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
49             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
50             {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
51             {SocketAction::SEND, "sampling-stop"},
52             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
53                 [this](auto recv, auto, auto) -> bool { return RecvHeapsamlingInfo(recv); }},
54 
55             {SocketAction::SEND, "sampling"},
56             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
57             // hit breakpoint after resume second time
58             {SocketAction::SEND, "resume"},
59             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
60             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
61             {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
62             {SocketAction::SEND, "sampling-stop"},
63             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
64                 [this](auto recv, auto, auto) -> bool { return RecvHeapsamlingInfo(recv); }},
65 
66             {SocketAction::SEND, "resume"},
67             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
68             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
69             {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
70 
71             {SocketAction::SEND, "heapprofiler-disable"},
72             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
73             // reply success and run
74             {SocketAction::SEND, "success"},
75             {SocketAction::SEND, "resume"},
76             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
77             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
78         };
79     }
80 
RecvHeapsamlingInfo(std::string recv)81     bool RecvHeapsamlingInfo(std::string recv)
82     {
83         std::unique_ptr<PtJson> json = PtJson::Parse(recv);
84         int id = 0;
85         Result ret = json->GetInt("id", &id);
86         if (ret != Result::SUCCESS) {
87             return false;
88         }
89 
90         std::unique_ptr<PtJson> result = nullptr;
91         ret = json->GetObject("result", &result);
92         if (ret != Result::SUCCESS) {
93             return false;
94         }
95 
96         std::unique_ptr<PtJson> profile = nullptr;
97         ret = result->GetObject("profile", &profile);
98         if (ret != Result::SUCCESS) {
99             return false;
100         }
101         return true;
102     }
103 
104     std::pair<std::string, std::string> GetEntryPoint() override
105     {
106         return {pandaFile_, entryPoint_};
107     }
108     ~JsHeapSamplingLoopTest() = default;
109 
110 private:
111     std::string pandaFile_ = DEBUGGER_ABC_DIR "common_func.abc";
112     std::string sourceFile_ = DEBUGGER_JS_DIR "common_func.js";
113     std::string entryPoint_ = "_GLOBAL::func_main_0";
114 };
115 
GetJsHeapSamplingLoopTest()116 std::unique_ptr<TestActions> GetJsHeapSamplingLoopTest()
117 {
118     return std::make_unique<JsHeapSamplingLoopTest>();
119 }
120 } // namespace panda::ecmascript::tooling::test
121 
122 #endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_HEAPSAMPLING_LOOP_TEST_H
123