@Test
public void stopMonitoringStopsOngoingTraces() {

  myCpuProfiler = new CpuProfiler(myProfilers);
  myCpuProfiler.stopProfiling(FAKE_SESSION);
  if (myUnifiedPipeline) {
    StopCpuTrace stopCpuTrace = (StopCpuTrace)myTransportService.getRegisteredCommand(Commands.Command.CommandType.STOP_CPU_TRACE);
    assertThat(stopCpuTrace.getLastTraceInfo()).isEqualTo(Cpu.CpuTraceInfo.getDefaultInstance());
    myTransportService.addEventToStream(
      FAKE_SESSION.getStreamId(),
      Common.Event.newBuilder()
        .setTimestamp(1).setGroupId(1).setPid(FAKE_SESSION.getPid()).setKind(Common.Event.Kind.CPU_TRACE)
        .setCpuTrace(Cpu.CpuTraceData.newBuilder().setTraceStarted(
          Cpu.CpuTraceData.TraceStarted.newBuilder().setTraceInfo(Cpu.CpuTraceInfo.newBuilder().setTraceId(1).setToTimestamp(-1))))
        .build());
    myCpuProfiler.stopProfiling(FAKE_SESSION);
    assertThat(stopCpuTrace.getLastTraceInfo()).isNotEqualTo(Cpu.CpuTraceInfo.getDefaultInstance());
  }
  else {
    // No ongoing so we don't expect stop tracing to be called.
    assertThat(myCpuService.getStartStopCapturingSession()).isNull();
    // There is an ongoing trace so stop tracing should be called.
    myCpuService.addTraceInfo(Cpu.CpuTraceInfo.newBuilder().setTraceId(1).setFromTimestamp(10).setToTimestamp(-1).build());
    myCpuProfiler.stopProfiling(FAKE_SESSION);
    assertThat(myCpuService.getStartStopCapturingSession()).isEqualTo(FAKE_SESSION);
  }
}