@Test
public void testGetTraceInfoFromSession() {

  Assume.assumeTrue(myUnifiedPipeline);
  Common.Session session = myProfilers.getSession();
  // Insert a completed CpuTraceInfo.
  Cpu.CpuTraceInfo info1 = Cpu.CpuTraceInfo.newBuilder()
    .setTraceId(1).setFromTimestamp(1).setToTimestamp(2)
    .setStartStatus(Cpu.TraceStartStatus.newBuilder().setStatus(Cpu.TraceStartStatus.Status.SUCCESS))
    .setStopStatus(Cpu.TraceStopStatus.newBuilder().setStatus(Cpu.TraceStopStatus.Status.SUCCESS))
    .build();
  myTransportService.addEventToStream(
    session.getStreamId(),
    Common.Event.newBuilder().setGroupId(1).setPid(session.getPid())
      .setIsEnded(true).setKind(Common.Event.Kind.CPU_TRACE).setTimestamp(1)
      .setCpuTrace(Cpu.CpuTraceData.newBuilder().setTraceEnded(Cpu.CpuTraceData.TraceEnded.newBuilder().setTraceInfo(info1))).build());
  List<Cpu.CpuTraceInfo> infos = CpuProfiler.getTraceInfoFromSession(myProfilers.getClient(), session, myUnifiedPipeline);
  assertThat(infos).containsExactly(info1);
  // Insert a not yet completed info followed up by a generic end event.
  Cpu.CpuTraceInfo info2 = Cpu.CpuTraceInfo.newBuilder()
    .setTraceId(5).setFromTimestamp(5).setToTimestamp(-1)
    .setStartStatus(Cpu.TraceStartStatus.newBuilder().setStatus(Cpu.TraceStartStatus.Status.SUCCESS))
    .build();
  myTransportService.addEventToStream(
    session.getStreamId(),
    Common.Event.newBuilder().setGroupId(5).setPid(session.getPid()).setKind(Common.Event.Kind.CPU_TRACE).setTimestamp(5)
      .setCpuTrace(Cpu.CpuTraceData.newBuilder().setTraceStarted(Cpu.CpuTraceData.TraceStarted.newBuilder().setTraceInfo(info2)))
      .build());
  myTransportService.addEventToStream(
    session.getStreamId(),
    Common.Event.newBuilder()
      .setTimestamp(10).setGroupId(5).setKind(Common.Event.Kind.CPU_TRACE).setPid(session.getPid()).setIsEnded(true).build());
  infos = CpuProfiler.getTraceInfoFromSession(myProfilers.getClient(), session, myUnifiedPipeline);
  assertThat(infos)
    .containsExactly(info1, info2.toBuilder().setToTimestamp(session.getEndTimestamp())
      .setStopStatus(Cpu.TraceStopStatus.newBuilder().setStatus(Cpu.TraceStopStatus.Status.APP_PROCESS_DIED)).build());
}