diff options
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/buildeventstream/transports')
2 files changed, 84 insertions, 6 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/buildeventstream/transports/BinaryFormatFileTransportTest.java b/src/test/java/com/google/devtools/build/lib/buildeventstream/transports/BinaryFormatFileTransportTest.java index 359f59ed93..1697bb9d1f 100644 --- a/src/test/java/com/google/devtools/build/lib/buildeventstream/transports/BinaryFormatFileTransportTest.java +++ b/src/test/java/com/google/devtools/build/lib/buildeventstream/transports/BinaryFormatFileTransportTest.java @@ -15,6 +15,8 @@ package com.google.devtools.build.lib.buildeventstream.transports; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; import com.google.devtools.build.lib.buildeventstream.BuildEvent; @@ -26,8 +28,8 @@ import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.Tar import com.google.devtools.build.lib.buildeventstream.PathConverter; import java.io.File; import java.io.FileInputStream; -import java.io.IOException; import java.io.InputStream; +import java.util.concurrent.Future; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -61,7 +63,7 @@ public class BinaryFormatFileTransportTest { } @Test - public void testCreatesFileAndWritesProtoTextFormat() throws IOException { + public void testCreatesFileAndWritesProtoBinaryFormat() throws Exception { File output = tmp.newFile(); BuildEventStreamProtos.BuildEvent started = @@ -85,7 +87,7 @@ public class BinaryFormatFileTransportTest { when(buildEvent.asStreamProto(Matchers.<BuildEventConverters>any())).thenReturn(completed); transport.sendBuildEvent(buildEvent); - transport.close(); + transport.close().get(); try (InputStream in = new FileInputStream(output)) { assertThat(BuildEventStreamProtos.BuildEvent.parseDelimitedFrom(in)).isEqualTo(started); assertThat(BuildEventStreamProtos.BuildEvent.parseDelimitedFrom(in)).isEqualTo(progress); @@ -93,4 +95,81 @@ public class BinaryFormatFileTransportTest { assertThat(in.available()).isEqualTo(0); } } + + @Test + public void testFileDoesNotExist() throws Exception { + // Get a file that doesn't exist by creating a new file and immediately deleting it. + File output = tmp.newFile(); + String path = output.getAbsolutePath(); + assertTrue(output.delete()); + + BuildEventStreamProtos.BuildEvent started = + BuildEventStreamProtos.BuildEvent.newBuilder() + .setStarted(BuildStarted.newBuilder().setCommand("build")) + .build(); + when(buildEvent.asStreamProto(Matchers.<BuildEventConverters>any())).thenReturn(started); + BinaryFormatFileTransport transport = new BinaryFormatFileTransport(path, pathConverter); + transport.sendBuildEvent(buildEvent); + + transport.close().get(); + try (InputStream in = new FileInputStream(output)) { + assertThat(BuildEventStreamProtos.BuildEvent.parseDelimitedFrom(in)).isEqualTo(started); + assertThat(in.available()).isEqualTo(0); + } + } + + @Test + public void testWriteWhenFileClosed() throws Exception { + File output = tmp.newFile(); + + BuildEventStreamProtos.BuildEvent started = + BuildEventStreamProtos.BuildEvent.newBuilder() + .setStarted(BuildStarted.newBuilder().setCommand("build")) + .build(); + when(buildEvent.asStreamProto(Matchers.<BuildEventConverters>any())).thenReturn(started); + + BinaryFormatFileTransport transport = + new BinaryFormatFileTransport(output.getAbsolutePath(), pathConverter); + + // Close the file. + transport.ch.close(); + assertFalse(transport.ch.isOpen()); + + // This should not throw an exception. + transport.sendBuildEvent(buildEvent); + transport.close().get(); + + // Also, nothing should have been written to the file + try (InputStream in = new FileInputStream(output)) { + assertThat(in.available()).isEqualTo(0); + } + } + + @Test + public void testWriteWhenTransportClosed() throws Exception { + File output = tmp.newFile(); + + BuildEventStreamProtos.BuildEvent started = + BuildEventStreamProtos.BuildEvent.newBuilder() + .setStarted(BuildStarted.newBuilder().setCommand("build")) + .build(); + when(buildEvent.asStreamProto(Matchers.<BuildEventConverters>any())).thenReturn(started); + + BinaryFormatFileTransport transport = + new BinaryFormatFileTransport(output.getAbsolutePath(), pathConverter); + + transport.sendBuildEvent(buildEvent); + Future<Void> closeFuture = transport.close(); + // This should not throw an exception, but also not perform any write. + transport.sendBuildEvent(buildEvent); + + closeFuture.get(); + assertFalse(transport.ch.isOpen()); + + // There should have only been one write. + try (InputStream in = new FileInputStream(output)) { + assertThat(BuildEventStreamProtos.BuildEvent.parseDelimitedFrom(in)).isEqualTo(started); + assertThat(in.available()).isEqualTo(0); + } + } } diff --git a/src/test/java/com/google/devtools/build/lib/buildeventstream/transports/TextFormatFileTransportTest.java b/src/test/java/com/google/devtools/build/lib/buildeventstream/transports/TextFormatFileTransportTest.java index ac62c7476c..7a008fabb0 100644 --- a/src/test/java/com/google/devtools/build/lib/buildeventstream/transports/TextFormatFileTransportTest.java +++ b/src/test/java/com/google/devtools/build/lib/buildeventstream/transports/TextFormatFileTransportTest.java @@ -28,7 +28,6 @@ import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.Tar import com.google.devtools.build.lib.buildeventstream.PathConverter; import com.google.protobuf.TextFormat; import java.io.File; -import java.io.IOException; import java.nio.charset.StandardCharsets; import org.junit.After; import org.junit.Before; @@ -63,7 +62,7 @@ public class TextFormatFileTransportTest { } @Test - public void testCreatesFileAndWritesProtoTextFormat() throws IOException { + public void testCreatesFileAndWritesProtoTextFormat() throws Exception { File output = tmp.newFile(); BuildEventStreamProtos.BuildEvent started = @@ -87,7 +86,7 @@ public class TextFormatFileTransportTest { when(buildEvent.asStreamProto(Matchers.<BuildEventConverters>any())).thenReturn(completed); transport.sendBuildEvent(buildEvent); - transport.close(); + transport.close().get(); String contents = trimLines( Joiner.on(System.lineSeparator()) |