Skip to content

Commit

Permalink
Test using 2nd CDPAgent to control debugger state
Browse files Browse the repository at this point in the history
Summary:
Add test to verify DebuggerDomainAgent's behavior when used as part of
multiple CDPAgents.

Reviewed By: mattbfb

Differential Revision: D56161895

fbshipit-source-id: e27dd026ad87447212dd9087a34875a0fd5b44e4
  • Loading branch information
dannysu authored and facebook-github-bot committed Apr 16, 2024
1 parent a87ed7d commit 05a42bd
Showing 1 changed file with 75 additions and 6 deletions.
81 changes: 75 additions & 6 deletions unittests/API/CDPAgentTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,15 @@ class CDPAgentTest : public ::testing::Test {
void sendRequest(
const std::string &method,
int id,
const std::function<void(::hermes::JSONEmitter &)> &setParameters = {});
const std::function<void(::hermes::JSONEmitter &)> &setParameters = {},
CDPAgent *altAgent = nullptr);
void sendParameterlessRequest(const std::string &method, int id);
void sendAndCheckResponse(const std::string &method, int id);
void sendEvalRequest(int id, int callFrameId, const std::string &expression);
void sendEvalRequest(
int id,
int callFrameId,
const std::string &expression,
CDPAgent *altAgent = nullptr);

jsi::Value shouldStop(
jsi::Runtime &runtime,
Expand Down Expand Up @@ -309,7 +314,8 @@ jsi::Value CDPAgentTest::shouldStop(
void CDPAgentTest::sendRequest(
const std::string &method,
int id,
const std::function<void(::hermes::JSONEmitter &)> &setParameters) {
const std::function<void(::hermes::JSONEmitter &)> &setParameters,
CDPAgent *altAgent) {
std::string command;
llvh::raw_string_ostream commandStream{command};
::hermes::JSONEmitter json{commandStream};
Expand All @@ -324,7 +330,11 @@ void CDPAgentTest::sendRequest(
}
json.closeDict();
commandStream.flush();
cdpAgent_->handleCommand(command);
if (altAgent != nullptr) {
altAgent->handleCommand(command);
} else {
cdpAgent_->handleCommand(command);
}
}

void CDPAgentTest::sendParameterlessRequest(const std::string &method, int id) {
Expand Down Expand Up @@ -360,7 +370,8 @@ void CDPAgentTest::waitForTestSignal(std::chrono::milliseconds timeout) {
void CDPAgentTest::sendEvalRequest(
int id,
int callFrameId,
const std::string &expression) {
const std::string &expression,
CDPAgent *altAgent) {
std::string command;
llvh::raw_string_ostream commandStream{command};
::hermes::JSONEmitter json{commandStream};
Expand All @@ -375,7 +386,11 @@ void CDPAgentTest::sendEvalRequest(
json.closeDict();
json.closeDict();
commandStream.flush();
cdpAgent_->handleCommand(command);
if (altAgent != nullptr) {
altAgent->handleCommand(command);
} else {
cdpAgent_->handleCommand(command);
}
}

std::unordered_map<std::string, std::string> CDPAgentTest::getAndEnsureProps(
Expand Down Expand Up @@ -1813,6 +1828,60 @@ TEST_F(CDPAgentTest, DebuggerActivateBreakpoints) {
ensureNotification(waitForMessage(), "Debugger.resumed");
}

TEST_F(CDPAgentTest, DebuggerMultipleCDPAgents) {
// Make a second CDPAgent
auto secondCDPAgent = CDPAgent::create(
kTestExecutionContextId_,
*cdpDebugAPI_,
std::bind(&CDPAgentTest::handleRuntimeTask, this, _1),
std::bind(&CDPAgentTest::handleResponse, this, _1));

int msgId = 1;

sendAndCheckResponse("Debugger.enable", msgId++);

std::array<std::string, 4> secondAgentMethods = {
"Debugger.resume",
"Debugger.stepOver",
"Debugger.stepInto",
"Debugger.stepOut",
};

for (const auto &method : secondAgentMethods) {
scheduleScript(R"(
debugger; // line 1
)");
ensureNotification(waitForMessage(), "Debugger.scriptParsed");
ensurePaused(waitForMessage(), "other", {{"global", 1, 1}});

// Send a command from the second CDPAgent even though we never enabled the
// Debugger domain on the second one.
sendRequest(method, msgId, {}, secondCDPAgent.get());

// Check that the command gets processed successfully
ensureOkResponse(waitForMessage(), msgId++);

// And the debugger resumed
ensureNotification(waitForMessage(), "Debugger.resumed");
}

scheduleScript(R"(
function level1() {
var foo = "bar";
debugger; // line 3
}
level1();
)");
ensureNotification(waitForMessage(), "Debugger.scriptParsed");
ensurePaused(waitForMessage(), "other", {{"level1", 3, 2}, {"global", 5, 1}});

// Send a command from the second CDPAgent for evaluateOnCallFrame even though
// we never enabled the Debugger domain on the second one.
sendEvalRequest(msgId, 0, R"("foo" + foo)", secondCDPAgent.get());

ensureEvalResponse(waitForMessage(), msgId++, "foobar");
}

TEST_F(CDPAgentTest, RuntimeEnableDisable) {
int msgId = 1;

Expand Down

0 comments on commit 05a42bd

Please sign in to comment.