style: fix styles and error-prones (#2560)

"Because of spurious wakeups, wait() must always be called in a loop".
Got rid of wait().

"If you return or throw from a finally, then values returned or thrown from the try-catch block will be ignored. Consider using try-with-resources instead."
Ignored the exception thrown from finally.
This commit is contained in:
Kun Zhang 2016-12-29 11:20:49 -08:00 committed by GitHub
parent 1aaf1a989c
commit 4693492fda
3 changed files with 31 additions and 32 deletions

View File

@ -72,7 +72,7 @@ public class PickFirstLoadBalancer2Test {
private List<ResolvedServerInfoGroup> servers = Lists.newArrayList(); private List<ResolvedServerInfoGroup> servers = Lists.newArrayList();
private List<SocketAddress> socketAddresses = Lists.newArrayList(); private List<SocketAddress> socketAddresses = Lists.newArrayList();
private static Attributes.Key<String> FOO = Attributes.Key.of("foo"); private static final Attributes.Key<String> FOO = Attributes.Key.of("foo");
private Attributes affinity = Attributes.newBuilder().set(FOO, "bar").build(); private Attributes affinity = Attributes.newBuilder().set(FOO, "bar").build();
@Captor @Captor

View File

@ -88,15 +88,13 @@ public class RoundRobinLoadBalancer2Test {
private RoundRobinLoadBalancer loadBalancer; private RoundRobinLoadBalancer loadBalancer;
private Map<ResolvedServerInfoGroup, EquivalentAddressGroup> servers = Maps.newHashMap(); private Map<ResolvedServerInfoGroup, EquivalentAddressGroup> servers = Maps.newHashMap();
private Map<EquivalentAddressGroup, Subchannel> subchannels = Maps.newLinkedHashMap(); private Map<EquivalentAddressGroup, Subchannel> subchannels = Maps.newLinkedHashMap();
private static Attributes.Key<String> MAJOR_KEY = Attributes.Key.of("major-key"); private static final Attributes.Key<String> MAJOR_KEY = Attributes.Key.of("major-key");
private Attributes affinity = Attributes.newBuilder().set(MAJOR_KEY, "I got the keys").build(); private Attributes affinity = Attributes.newBuilder().set(MAJOR_KEY, "I got the keys").build();
@Captor @Captor
private ArgumentCaptor<Picker> pickerCaptor; private ArgumentCaptor<Picker> pickerCaptor;
@Captor @Captor
private ArgumentCaptor<EquivalentAddressGroup> eagCaptor; private ArgumentCaptor<EquivalentAddressGroup> eagCaptor;
@Captor
private ArgumentCaptor<Attributes> attrsCaptor;
@Mock @Mock
private Helper mockHelper; private Helper mockHelper;
@Mock @Mock

View File

@ -112,6 +112,7 @@ import java.util.List;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -1450,10 +1451,15 @@ public class OkHttpClientTransportTest {
} }
private static class MockFrameReader implements FrameReader { private static class MockFrameReader implements FrameReader {
CountDownLatch closed = new CountDownLatch(1); final CountDownLatch closed = new CountDownLatch(1);
boolean throwExceptionForNextFrame;
boolean returnFalseInNextFrame; enum Result {
boolean throwErrorForNextFrame; THROW_EXCEPTION,
RETURN_FALSE,
THROW_ERROR
}
final LinkedBlockingQueue<Result> nextResults = new LinkedBlockingQueue<Result>();
@Override @Override
public void close() throws IOException { public void close() throws IOException {
@ -1472,41 +1478,36 @@ public class OkHttpClientTransportTest {
} }
@Override @Override
public synchronized boolean nextFrame(Handler handler) throws IOException { public boolean nextFrame(Handler handler) throws IOException {
if (throwErrorForNextFrame) { Result result;
throw new Error(ERROR_MESSAGE);
}
if (throwExceptionForNextFrame) {
throw new IOException(NETWORK_ISSUE_MESSAGE);
}
if (returnFalseInNextFrame) {
return false;
}
try { try {
wait(); result = nextResults.take();
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
throw new IOException(e); throw new IOException(e);
} }
if (throwExceptionForNextFrame) { switch (result) {
throw new IOException(NETWORK_ISSUE_MESSAGE); case THROW_EXCEPTION:
throw new IOException(NETWORK_ISSUE_MESSAGE);
case RETURN_FALSE:
return false;
case THROW_ERROR:
throw new Error(ERROR_MESSAGE);
default:
throw new UnsupportedOperationException("unimplemented: " + result);
} }
return !returnFalseInNextFrame;
} }
synchronized void throwIoExceptionForNextFrame() { void throwIoExceptionForNextFrame() {
throwExceptionForNextFrame = true; nextResults.add(Result.THROW_EXCEPTION);
notifyAll();
} }
synchronized void throwErrorForNextFrame() { void throwErrorForNextFrame() {
throwErrorForNextFrame = true; nextResults.add(Result.THROW_ERROR);
notifyAll();
} }
synchronized void nextFrameAtEndOfStream() { void nextFrameAtEndOfStream() {
returnFalseInNextFrame = true; nextResults.add(Result.RETURN_FALSE);
notifyAll();
} }
@Override @Override
@ -1574,7 +1575,7 @@ public class OkHttpClientTransportTest {
try { try {
message.close(); message.close();
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); // Ignore
} }
} }
} }