BuildEventStreamer: ingore noop-flush()s

The BuildEventStreamer supports a method flush() to report any pending
stdout/stderr in the BEP; in particular, all internal buffers of for
those streams are cleared (and the memory can be reclaimed). If there
are no pending bytes in those streams, however, there is no need to
generate an additional progress event to get rid of the buffered stream
contents. Make flush() a no-op in this case.

Change-Id: Ia8cf8733fdeaf4d1a50488736d2637862e7cb4f5
PiperOrigin-RevId: 181590982
diff --git a/src/test/java/com/google/devtools/build/lib/runtime/BuildEventStreamerTest.java b/src/test/java/com/google/devtools/build/lib/runtime/BuildEventStreamerTest.java
index 541843b..81b4328 100644
--- a/src/test/java/com/google/devtools/build/lib/runtime/BuildEventStreamerTest.java
+++ b/src/test/java/com/google/devtools/build/lib/runtime/BuildEventStreamerTest.java
@@ -699,6 +699,36 @@
   }
 
   @Test
+  public void testNoopFlush() throws Exception {
+    // Verify that the streamer ignores a flush, if neither stream produces any output.
+    RecordingBuildEventTransport transport = new RecordingBuildEventTransport();
+    BuildEventStreamer streamer =
+        new BuildEventStreamer(ImmutableSet.<BuildEventTransport>of(transport), reporter);
+    BuildEventStreamer.OutErrProvider outErr =
+        Mockito.mock(BuildEventStreamer.OutErrProvider.class);
+    String stdoutMsg = "Some text that was written to stdout.";
+    String stderrMsg = "The UI text that bazel wrote to stderr.";
+    when(outErr.getOut()).thenReturn(stdoutMsg).thenReturn("");
+    when(outErr.getErr()).thenReturn(stderrMsg).thenReturn("");
+    BuildEvent startEvent =
+        new GenericBuildEvent(
+            testId("Initial"),
+            ImmutableSet.<BuildEventId>of(ProgressEvent.INITIAL_PROGRESS_UPDATE));
+
+    streamer.registerOutErrProvider(outErr);
+    streamer.buildEvent(startEvent);
+    assertThat(transport.getEvents()).hasSize(1);
+    streamer.flush(); // Output, so a new progress event has to be added
+    assertThat(transport.getEvents()).hasSize(2);
+    streamer.flush(); // No further output, so no additional event should be generated.
+    assertThat(transport.getEvents()).hasSize(2);
+
+    assertThat(transport.getEvents().get(0)).isEqualTo(startEvent);
+    assertThat(transport.getEventProtos().get(1).getProgress().getStdout()).isEqualTo(stdoutMsg);
+    assertThat(transport.getEventProtos().get(1).getProgress().getStderr()).isEqualTo(stderrMsg);
+  }
+
+  @Test
   public void testEarlyFlushBadInitialEvent() throws Exception {
     // Verify that an early flush works correctly with an unusual start event.
     // In this case, we expect 3 events in the stream, in that order: