Skip to content

Commit 5aac8c1

Browse files
committed
Throw an ISE on attempting to modify a bazel profile thread after being sorted
Signed-off-by: Felipe <afrueda97@outlook.com>
1 parent cdb011d commit 5aac8c1

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

analyzer/java/com/engflow/bazel/invocation/analyzer/bazelprofile/ProfileThread.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,14 @@ public Integer getSortIndex() {
112112
/**
113113
* Parses a {@link JsonObject} as a tracing event and adds it to this thread. Returns {@code true}
114114
* if parsing and adding the event was successful and {@code false} otherwise.
115+
*
116+
* @throws IllegalStateException if the thread has already been sorted, and as such cannot be
117+
* modified anymore.
115118
*/
116119
public boolean addEvent(JsonObject event) {
120+
if (sorted) {
121+
throw new IllegalStateException("Cannot add event, Bazel profile thread has been sorted!");
122+
}
117123
try {
118124
switch (event.get(TraceEventFormatConstants.EVENT_PHASE).getAsString()) {
119125
case TraceEventFormatConstants.PHASE_COMPLETE: // Complete events

analyzer/javatests/com/engflow/bazel/invocation/analyzer/bazelprofile/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ java_test(
1414
"//analyzer/java/com/engflow/bazel/invocation/analyzer/time",
1515
"//analyzer/java/com/engflow/bazel/invocation/analyzer/traceeventformat",
1616
"//analyzer/javatests/com/engflow/bazel/invocation/analyzer:test_base",
17+
"//third_party/gson",
1718
"//third_party/guava",
1819
"//third_party/junit",
1920
"//third_party/truth",

analyzer/javatests/com/engflow/bazel/invocation/analyzer/bazelprofile/BazelProfileTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.google.common.collect.ImmutableList;
4141
import com.google.common.collect.ImmutableMap;
4242
import com.google.common.collect.Lists;
43+
import com.google.gson.JsonObject;
4344
import java.io.ByteArrayInputStream;
4445
import java.nio.charset.StandardCharsets;
4546
import java.util.stream.IntStream;
@@ -349,6 +350,63 @@ public void getActionCountsMixedUsesNew()
349350
assertThat(profile.getActionCounts().get().size()).isEqualTo(201);
350351
}
351352

353+
@Test
354+
public void addEventShouldThrowOnSortedProfileThread() throws Exception {
355+
var name = "CPP";
356+
var want =
357+
new ProfileThread(
358+
new ThreadId(1, 1),
359+
BazelProfileConstants.THREAD_CRITICAL_PATH,
360+
0,
361+
ImmutableList.of(),
362+
ImmutableList.of(),
363+
Lists.newArrayList(
364+
new CompleteEvent(
365+
name,
366+
BazelProfileConstants.CAT_CRITICAL_PATH_COMPONENT,
367+
Timestamp.ofMicros(11),
368+
TimeUtil.getDurationForMicros(10),
369+
1,
370+
1,
371+
ImmutableMap.of()),
372+
new CompleteEvent(
373+
name,
374+
BazelProfileConstants.CAT_CRITICAL_PATH_COMPONENT,
375+
Timestamp.ofMicros(21),
376+
TimeUtil.getDurationForMicros(10),
377+
1,
378+
1,
379+
ImmutableMap.of()),
380+
new CompleteEvent(
381+
name,
382+
BazelProfileConstants.CAT_CRITICAL_PATH_COMPONENT,
383+
Timestamp.ofMicros(31),
384+
TimeUtil.getDurationForMicros(10),
385+
1,
386+
1,
387+
ImmutableMap.of()),
388+
new CompleteEvent(
389+
name,
390+
BazelProfileConstants.CAT_CRITICAL_PATH_COMPONENT,
391+
Timestamp.ofMicros(50),
392+
TimeUtil.getDurationForMicros(10),
393+
1,
394+
1,
395+
ImmutableMap.of())),
396+
ImmutableMap.of(),
397+
ImmutableMap.of());
398+
399+
// Getting the completeEvents from the thread will trigger them to be sorted.
400+
var sorted = want.getCompleteEvents();
401+
// At this point, any further attempts at modifying the thread should throw an ISE, as this
402+
// would break the sorting that was previously done.
403+
var exception =
404+
assertThrows(IllegalStateException.class, () -> want.addEvent(new JsonObject()));
405+
assertThat(exception)
406+
.hasMessageThat()
407+
.isEqualTo("Cannot add event, Bazel profile thread has been sorted!");
408+
}
409+
352410
@Test
353411
public void isMainThreadShouldReturnFalseOnUnnamedProfileThread() {
354412
ProfileThread thread = new ProfileThread(new ThreadId(0, 0));

0 commit comments

Comments
 (0)