|
40 | 40 | import com.google.common.collect.ImmutableList; |
41 | 41 | import com.google.common.collect.ImmutableMap; |
42 | 42 | import com.google.common.collect.Lists; |
| 43 | +import com.google.gson.JsonObject; |
43 | 44 | import java.io.ByteArrayInputStream; |
44 | 45 | import java.nio.charset.StandardCharsets; |
45 | 46 | import java.util.stream.IntStream; |
@@ -349,6 +350,63 @@ public void getActionCountsMixedUsesNew() |
349 | 350 | assertThat(profile.getActionCounts().get().size()).isEqualTo(201); |
350 | 351 | } |
351 | 352 |
|
| 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 | + |
352 | 410 | @Test |
353 | 411 | public void isMainThreadShouldReturnFalseOnUnnamedProfileThread() { |
354 | 412 | ProfileThread thread = new ProfileThread(new ThreadId(0, 0)); |
|
0 commit comments