Skip to content

Commit d8b8ab0

Browse files
Googlercopybara-github
authored andcommitted
Add a TestProgress event type to the Build Event Protocol
This event may be used to report information about an executing TestRunner action. PiperOrigin-RevId: 556865690 Change-Id: Ia654d2e228e94008218ca8e7362a9c11d353d4fe
1 parent b54046f commit d8b8ab0

File tree

6 files changed

+243
-0
lines changed

6 files changed

+243
-0
lines changed

src/main/java/com/google/devtools/build/lib/analysis/BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2777,6 +2777,18 @@ java_library(
27772777
],
27782778
)
27792779

2780+
java_library(
2781+
name = "test/test_progress",
2782+
srcs = ["test/TestProgress.java"],
2783+
deps = [
2784+
"//src/main/java/com/google/devtools/build/lib/buildeventstream",
2785+
"//src/main/java/com/google/devtools/build/lib/buildeventstream/proto:build_event_stream_java_proto",
2786+
"//src/main/java/com/google/devtools/build/lib/concurrent",
2787+
"//third_party:guava",
2788+
"//third_party:jsr305",
2789+
],
2790+
)
2791+
27802792
java_library(
27812793
name = "transitive_dependency_state",
27822794
srcs = ["TransitiveDependencyState.java"],
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright 2023 The Bazel Authors. All rights reserved.
2+
//
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+
package com.google.devtools.build.lib.analysis.test;
15+
16+
import com.google.common.base.Objects;
17+
import com.google.common.collect.ImmutableList;
18+
import com.google.devtools.build.lib.buildeventstream.BuildEvent;
19+
import com.google.devtools.build.lib.buildeventstream.BuildEventContext;
20+
import com.google.devtools.build.lib.buildeventstream.BuildEventIdUtil;
21+
import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos;
22+
import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId;
23+
import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId.ConfigurationId;
24+
import com.google.devtools.build.lib.buildeventstream.GenericBuildEvent;
25+
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
26+
import javax.annotation.Nullable;
27+
28+
/** This event may be raised while a test action is executing to report info about its execution. */
29+
@Immutable
30+
public final class TestProgress implements BuildEvent {
31+
/** The label of the target for the action. */
32+
private final String label;
33+
34+
/** The configuration under which the action is running. */
35+
private final BuildEventId.ConfigurationId configId;
36+
37+
/** The run number of the test action (e.g. for runs_per_test > 1). */
38+
private final int run;
39+
40+
/** For sharded tests, the shard number of the test action. */
41+
private final int shard;
42+
43+
/** The execution attempt number which may increase due to retries. */
44+
private final int attempt;
45+
46+
/** A count which may be incremented to differentiate events. */
47+
private final int opaqueCount;
48+
49+
/** Identifies a resource that can provide info about the active test run. */
50+
private final String uri;
51+
52+
public TestProgress(
53+
String label,
54+
ConfigurationId configId,
55+
int run,
56+
int shard,
57+
int attempt,
58+
int opaqueCount,
59+
String uri) {
60+
this.label = label;
61+
this.configId = configId;
62+
this.run = run;
63+
this.shard = shard;
64+
this.attempt = attempt;
65+
this.opaqueCount = opaqueCount;
66+
this.uri = uri;
67+
}
68+
69+
@Override
70+
public BuildEventId getEventId() {
71+
return BuildEventIdUtil.testProgressId(label, configId, run, shard, attempt, opaqueCount);
72+
}
73+
74+
@Override
75+
public ImmutableList<BuildEventId> getChildrenEvents() {
76+
return ImmutableList.of();
77+
}
78+
79+
@Override
80+
public BuildEventStreamProtos.BuildEvent asStreamProto(BuildEventContext converters) {
81+
return GenericBuildEvent.protoChaining(this).setTestProgress(asTestResult()).build();
82+
}
83+
84+
@Override
85+
public int hashCode() {
86+
return Objects.hashCode(label, configId, run, shard, attempt, opaqueCount, uri);
87+
}
88+
89+
@Override
90+
public boolean equals(@Nullable Object object) {
91+
if (!(object instanceof TestProgress)) {
92+
return false;
93+
}
94+
TestProgress other = (TestProgress) object;
95+
return label.equals(other.label)
96+
&& configId.equals(other.configId)
97+
&& run == other.run
98+
&& shard == other.shard
99+
&& attempt == other.attempt
100+
&& opaqueCount == other.opaqueCount
101+
&& uri.equals(other.uri);
102+
}
103+
104+
private BuildEventStreamProtos.TestProgress asTestResult() {
105+
return BuildEventStreamProtos.TestProgress.newBuilder().setUri(uri).build();
106+
}
107+
}

src/main/java/com/google/devtools/build/lib/buildeventstream/BuildEventIdUtil.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,25 @@ public static BuildEventId testResult(
257257
return testResult(target, run, shard, 1, configuration);
258258
}
259259

260+
public static BuildEventId testProgressId(
261+
String label,
262+
BuildEventId.ConfigurationId configId,
263+
int run,
264+
int shard,
265+
int attempt,
266+
int opaqueCount) {
267+
return BuildEventId.newBuilder()
268+
.setTestProgress(
269+
BuildEventId.TestProgressId.newBuilder()
270+
.setLabel(label)
271+
.setConfiguration(configId)
272+
.setRun(run)
273+
.setShard(shard)
274+
.setAttempt(attempt)
275+
.setOpaqueCount(opaqueCount))
276+
.build();
277+
}
278+
260279
public static BuildEventId testSummary(Label target, BuildEventId configuration) {
261280
BuildEventId.ConfigurationId configId = configuration.getConfiguration();
262281
BuildEventId.TestSummaryId summaryId =

src/main/java/com/google/devtools/build/lib/buildeventstream/proto/build_event_stream.proto

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,24 @@ message BuildEventId {
201201
int32 attempt = 4;
202202
}
203203

204+
// Identifier of an event reporting progress of an individual test run.
205+
message TestProgressId {
206+
// The label of the target for the action.
207+
string label = 1;
208+
// The configuration under which the action is running.
209+
ConfigurationId configuration = 2;
210+
// The run number of the test action (e.g. for runs_per_test > 1).
211+
int32 run = 3;
212+
// For sharded tests, the shard number of the test action.
213+
int32 shard = 4;
214+
// The execution attempt number which may increase due to retries (e.g. for
215+
// flaky tests).
216+
int32 attempt = 5;
217+
// An incrementing count used to differentiate TestProgressIds for the same
218+
// test attempt.
219+
int32 opaque_count = 6;
220+
}
221+
204222
// Identifier of an event reporting the summary of a test.
205223
message TestSummaryId {
206224
string label = 1;
@@ -252,6 +270,7 @@ message BuildEventId {
252270
UnconfiguredLabelId unconfigured_label = 19;
253271
ConfiguredLabelId configured_label = 21;
254272
TestResultId test_result = 8;
273+
TestProgressId test_progress = 29;
255274
TestSummaryId test_summary = 7;
256275
TargetSummaryId target_summary = 26;
257276
BuildFinishedId build_finished = 9;
@@ -725,6 +744,15 @@ message TestResult {
725744
ExecutionInfo execution_info = 8;
726745
}
727746

747+
// Event payload providing information about an active, individual test run.
748+
message TestProgress {
749+
// Identifies a resource that may provide information about an active test
750+
// run. The resource is not necessarily a file and may need to be queried
751+
// for information. The URI is not guaranteed to be available after the test
752+
// completes. The string is encoded according to RFC2396.
753+
string uri = 1;
754+
}
755+
728756
// Payload of the event summarizing a test.
729757
message TestSummary {
730758
// Wrapper around BlazeTestStatus to support importing that enum to proto3.
@@ -1250,6 +1278,7 @@ message BuildEvent {
12501278
NamedSetOfFiles named_set_of_files = 15;
12511279
TargetComplete completed = 8;
12521280
TestResult test_result = 10;
1281+
TestProgress test_progress = 30;
12531282
TestSummary test_summary = 9;
12541283
TargetSummary target_summary = 28;
12551284
BuildFinished finished = 14;

src/test/java/com/google/devtools/build/lib/analysis/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ java_library(
119119
"//src/main/java/com/google/devtools/build/lib/analysis:target_and_configuration",
120120
"//src/main/java/com/google/devtools/build/lib/analysis:test/instrumented_files_info",
121121
"//src/main/java/com/google/devtools/build/lib/analysis:test/test_configuration",
122+
"//src/main/java/com/google/devtools/build/lib/analysis:test/test_progress",
122123
"//src/main/java/com/google/devtools/build/lib/analysis:test/test_trimming_transition_factory",
123124
"//src/main/java/com/google/devtools/build/lib/analysis:top_level_artifact_context",
124125
"//src/main/java/com/google/devtools/build/lib/analysis:transitive_dependency_state",
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2023 The Bazel Authors. All rights reserved.
2+
//
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+
package com.google.devtools.build.lib.analysis.test;
15+
16+
import static com.google.common.truth.extensions.proto.ProtoTruth.assertThat;
17+
18+
import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos;
19+
import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEvent;
20+
import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId;
21+
import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId.ConfigurationId;
22+
import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId.TestProgressId;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.junit.runners.JUnit4;
26+
27+
@RunWith(JUnit4.class)
28+
public class TestProgressTest {
29+
@Test
30+
public void testTestProgress_convertsToEventId() {
31+
TestProgress progress =
32+
new TestProgress(
33+
"alabel", ConfigurationId.newBuilder().setId("configid").build(), 1, 2, 3, 4, "auri");
34+
35+
BuildEventId id = progress.getEventId();
36+
37+
assertThat(id)
38+
.isEqualTo(
39+
BuildEventId.newBuilder()
40+
.setTestProgress(
41+
TestProgressId.newBuilder()
42+
.setLabel("alabel")
43+
.setConfiguration(ConfigurationId.newBuilder().setId("configid"))
44+
.setRun(1)
45+
.setShard(2)
46+
.setAttempt(3)
47+
.setOpaqueCount(4))
48+
.build());
49+
}
50+
51+
@Test
52+
public void testTestProgress_convertsToEvent() {
53+
TestProgress progress =
54+
new TestProgress(
55+
"alabel", ConfigurationId.newBuilder().setId("configid").build(), 1, 2, 3, 4, "auri");
56+
57+
BuildEvent event = progress.asStreamProto(null);
58+
59+
assertThat(event)
60+
.isEqualTo(
61+
BuildEvent.newBuilder()
62+
.setId(
63+
BuildEventId.newBuilder()
64+
.setTestProgress(
65+
TestProgressId.newBuilder()
66+
.setLabel("alabel")
67+
.setConfiguration(ConfigurationId.newBuilder().setId("configid"))
68+
.setRun(1)
69+
.setShard(2)
70+
.setAttempt(3)
71+
.setOpaqueCount(4)))
72+
.setTestProgress(BuildEventStreamProtos.TestProgress.newBuilder().setUri("auri"))
73+
.build());
74+
}
75+
}

0 commit comments

Comments
 (0)