Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deps: update to Guava 29.0-jre #837

Merged
merged 5 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<organization>
<name>Google LLC</name>
<url>http://www.google.com</url>
<url>https://www.google.com</url>
</organization>

<licenses>
Expand Down Expand Up @@ -67,7 +67,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.1-jre</version>
<version>28.2-jre</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ class AsyncByteConsumer implements AsyncStreamSaver {
@Override
public void handleStream(final InputStream inputStream) {
if (executorService.isShutdown()) {
throw new IllegalStateException("Cannot re-use " + this.getClass().getName());
throw new IllegalStateException("Cannot reuse " + this.getClass().getName());
}
result.setFuture(executorService.submit(() -> consumeBytes(inputStream)));
ListenableFuture<String> submit = executorService.submit(() -> consumeBytes(inputStream));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to expand this out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No semantic difference, but it makes refactoring, debugging, and updating easier. The more operations get crammed into a single statement the harder it becomes to read. In this case, it was difficult to demock without introducing local variables.

result.setFuture(submit);
executorService.shutdown();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
package com.google.cloud.tools.managedcloudsdk.command;

import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.SettableFuture;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -37,42 +38,50 @@ public class AsyncByteConsumerTest {
private final InputStream fakeInputStream =
new ByteArrayInputStream(TEST_STRING.getBytes(StandardCharsets.UTF_8));

@Mock private ListeningExecutorService mockExecutorService;
@Mock private ExecutorService executorService;
@Mock private ByteHandler mockByteHandler;
@Mock private InputStream mockInputStream;
@Mock private SettableFuture<String> mockFuture;

private SettableFuture<String> future = SettableFuture.create();

@Test
public void testHandleStream() {
Mockito.when(mockExecutorService.isShutdown()).thenReturn(false);
Mockito.when(executorService.isShutdown()).thenReturn(false);

ListeningExecutorService listeningExecutorService =
MoreExecutors.listeningDecorator(executorService);

new AsyncByteConsumer(mockByteHandler, mockExecutorService, mockFuture)
.handleStream(mockInputStream);
AsyncByteConsumer consumer =
new AsyncByteConsumer(mockByteHandler, listeningExecutorService, future);
consumer.handleStream(mockInputStream);

Mockito.verify(mockExecutorService).isShutdown();
Mockito.verify(mockExecutorService).submit(Mockito.<Callable<Object>>any());
Mockito.verify(mockExecutorService).shutdown();
Mockito.verifyNoMoreInteractions(mockExecutorService);
Mockito.verify(executorService).isShutdown();
Mockito.verify(executorService).execute(Mockito.<Runnable>any());
Mockito.verify(executorService).shutdown();
Mockito.verifyNoMoreInteractions(executorService);
}

@Test
public void testHandleStream_failIfReused() {
Mockito.when(mockExecutorService.isShutdown()).thenReturn(true);
Mockito.when(executorService.isShutdown()).thenReturn(true);
ListeningExecutorService listeningExecutorService =
MoreExecutors.listeningDecorator(executorService);

try {
new AsyncByteConsumer(mockByteHandler, mockExecutorService, mockFuture)
new AsyncByteConsumer(mockByteHandler, listeningExecutorService, future)
.handleStream(mockInputStream);
Assert.fail("IllegalStateException expected but not thrown");
} catch (IllegalStateException ex) {
// pass
Assert.assertEquals("Cannot re-use " + AsyncByteConsumer.class.getName(), ex.getMessage());
Assert.assertEquals("Cannot reuse " + AsyncByteConsumer.class.getName(), ex.getMessage());
}
}

@Test
public void testConsumeBytes() throws Exception {

new AsyncByteConsumer(mockByteHandler, mockExecutorService, mockFuture)
ListeningExecutorService listeningExecutorService =
MoreExecutors.listeningDecorator(executorService);
new AsyncByteConsumer(mockByteHandler, listeningExecutorService, future)
.consumeBytes(fakeInputStream);

ArgumentCaptor<byte[]> bytes = ArgumentCaptor.forClass(byte[].class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

import com.google.cloud.tools.managedcloudsdk.process.ProcessExecutor;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.AbstractFuture;
import com.google.common.util.concurrent.SettableFuture;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
Expand All @@ -45,9 +46,9 @@ public class CommandCallerTest {
@Mock private AsyncStreamSaver mockStdoutSaver;
@Mock private AsyncStreamSaver mockStderrSaver;
@Mock private AsyncStreamSaverFactory mockStreamSaverFactory;
@Mock private ListenableFuture<String> mockStdout;
@Mock private ListenableFuture<String> mockStderr;

private final SettableFuture<String> mockStdout = SettableFuture.create();
private final SettableFuture<String> mockStderr = SettableFuture.create();
private List<String> fakeCommand;
private Path fakeWorkingDirectory;
private Map<String, String> fakeEnvironment;
Expand All @@ -73,8 +74,9 @@ public void setUp() throws IOException, InterruptedException, ExecutionException
.thenReturn(0);
Mockito.when(mockStdoutSaver.getResult()).thenReturn(mockStdout);
Mockito.when(mockStderrSaver.getResult()).thenReturn(mockStderr);
Mockito.when(mockStdout.get()).thenReturn("stdout");
Mockito.when(mockStderr.get()).thenReturn("stderr");

mockStdout.set("stdout");
mockStderr.set("stderr");

testCommandCaller = new CommandCaller(() -> mockProcessExecutor, mockStreamSaverFactory);
}
Expand Down Expand Up @@ -143,7 +145,15 @@ public void testCall_ioException()
public void testCall_interruptedExceptionPassthrough()
throws CommandExecutionException, CommandExitException, ExecutionException,
InterruptedException, IOException {
Mockito.when(mockStdout.get()).thenThrow(InterruptedException.class);

AbstractFuture<String> future =
new AbstractFuture<String>() {
@Override
public String get() throws InterruptedException {
throw new InterruptedException();
}
};
Mockito.when(mockStdoutSaver.getResult()).thenReturn(future);

try {
testCommandCaller.call(fakeCommand, fakeWorkingDirectory, fakeEnvironment);
Expand Down