Skip to content

Commit dbf5b56

Browse files
motiz88facebook-github-bot
authored andcommitted
Add tests for multi-session Tracing domain support (first to trace wins) (facebook#55287)
Summary: Changelog: [Internal] Adds tests (and a doc comment) codifying the existing, correct behavior of the CDP `Tracing.start` command when there is already a tracing session in progress. Reviewed By: hoxyq Differential Revision: D90888857
1 parent 4506222 commit dbf5b56

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

packages/react-native/ReactCommon/jsinspector-modern/TracingAgent.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ bool TracingAgent::handleRequest(const cdp::PreparsedRequest& req) {
9090
bool didNotHaveAlreadyRunningRecording = hostTargetController_.startTracing(
9191
tracing::Mode::CDP, std::move(enabledCategories));
9292
if (!didNotHaveAlreadyRunningRecording) {
93+
// @cdp Tracing.start fails if there is a tracing session already running
94+
// in the current target. This matches Chrome's behavior.
9395
frontendChannel_(
9496
cdp::jsonError(
9597
req.id,

packages/react-native/ReactCommon/jsinspector-modern/tests/TracingTest.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,76 @@ TEST_F(TracingTest, EmitsScreenshotEventWhenScreenshotValuePassed) {
113113
EXPECT_THAT(allTraceEvents, Contains(AtJsonPtr("/name", "Screenshot")));
114114
}
115115

116+
TEST_F(
117+
TracingTest,
118+
SecondSessionTracingStartIsRejectedWhileFirstSessionIsTracing) {
119+
auto secondary = connectSecondary();
120+
InSequence s;
121+
122+
// Session 1 starts tracing successfully
123+
startTracing();
124+
125+
// Session 2 tries to start tracing - should get error
126+
EXPECT_CALL(
127+
secondary.fromPage(),
128+
onMessage(JsonParsed(AllOf(
129+
AtJsonPtr("/id", 2),
130+
AtJsonPtr("/error/message", "Tracing has already been started")))));
131+
secondary.toPage().sendMessage(R"({"id": 2, "method": "Tracing.start"})");
132+
133+
// Session 1 ends tracing normally
134+
endTracingAndCollectEvents();
135+
136+
// Now Session 2 can start tracing
137+
EXPECT_CALL(
138+
secondary.fromPage(), onMessage(JsonEq(R"({"id": 3, "result": {}})")));
139+
secondary.toPage().sendMessage(R"({"id": 3, "method": "Tracing.start"})");
140+
141+
// Clean up - end secondary's tracing
142+
EXPECT_CALL(
143+
secondary.fromPage(), onMessage(JsonEq(R"({"id": 4, "result": {}})")));
144+
EXPECT_CALL(
145+
secondary.fromPage(),
146+
onMessage(JsonParsed(AtJsonPtr("/method", "Tracing.dataCollected"))))
147+
.Times(AtLeast(1));
148+
EXPECT_CALL(
149+
secondary.fromPage(),
150+
onMessage(JsonParsed(AtJsonPtr("/method", "Tracing.tracingComplete"))));
151+
secondary.toPage().sendMessage(R"({"id": 4, "method": "Tracing.end"})");
152+
}
153+
154+
TEST_F(TracingTest, CDPTracingPreemptsBackgroundTracing) {
155+
InSequence s;
156+
157+
// Start background tracing directly
158+
page_->startTracing(tracing::Mode::Background, {});
159+
160+
// CDP Tracing.start should preempt background (succeed, not fail)
161+
startTracing();
162+
163+
// End tracing normally
164+
endTracingAndCollectEvents();
165+
}
166+
167+
TEST_F(TracingTest, BackgroundTracingIsRejectedWhileCDPTracingIsRunning) {
168+
InSequence s;
169+
170+
// Start CDP tracing
171+
startTracing();
172+
173+
// Background tracing should be rejected
174+
bool started = page_->startTracing(tracing::Mode::Background, {});
175+
EXPECT_FALSE(started);
176+
177+
// End CDP tracing
178+
endTracingAndCollectEvents();
179+
180+
// Now background tracing should succeed
181+
started = page_->startTracing(tracing::Mode::Background, {});
182+
EXPECT_TRUE(started);
183+
184+
// Clean up
185+
page_->stopTracing();
186+
}
187+
116188
} // namespace facebook::react::jsinspector_modern

0 commit comments

Comments
 (0)