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

remove Mockito from pubsub #1741

Merged
merged 1 commit into from
Mar 13, 2017
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
6 changes: 0 additions & 6 deletions google-cloud-pubsub/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@
<version>3.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.google.pubsub.v1.PublishResponse;
import com.google.pubsub.v1.PublisherGrpc.PublisherImplBase;
import io.grpc.stub.StreamObserver;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

/**
Expand All @@ -29,6 +31,7 @@
*/
class FakePublisherServiceImpl extends PublisherImplBase {

private final LinkedBlockingQueue<PublishRequest> requests = new LinkedBlockingQueue<>();
private final LinkedBlockingQueue<Response> publishResponses = new LinkedBlockingQueue<>();

/** Class used to save the state of a possible response. */
Expand Down Expand Up @@ -69,6 +72,7 @@ public String toString() {

@Override
public void publish(PublishRequest request, StreamObserver<PublishResponse> responseObserver) {
requests.add(request);
Response response;
try {
response = publishResponses.take();
Expand Down Expand Up @@ -98,7 +102,7 @@ public FakePublisherServiceImpl addPublishError(Throwable error) {
return this;
}

public void reset() {
publishResponses.clear();
public List<PublishRequest> getCapturedRequests() {
return new ArrayList<PublishRequest>(requests);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,19 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.times;

import com.google.api.gax.bundling.BundlingSettings;
import com.google.api.gax.core.ApiFuture;
import com.google.api.gax.core.FlowControlSettings;
import com.google.api.gax.core.FlowController.LimitExceededBehavior;
import com.google.api.gax.core.ApiFuture;
import com.google.api.gax.grpc.ChannelProvider;
import com.google.api.gax.grpc.ExecutorProvider;
import com.google.api.gax.grpc.FixedExecutorProvider;
import com.google.api.gax.grpc.InstantiatingExecutorProvider;
import com.google.cloud.pubsub.spi.v1.Publisher.Builder;
import com.google.protobuf.ByteString;
import com.google.pubsub.v1.PublishRequest;
import com.google.pubsub.v1.PublishResponse;
import com.google.pubsub.v1.PubsubMessage;
import com.google.pubsub.v1.TopicName;
Expand All @@ -42,7 +40,6 @@
import io.grpc.inprocess.InProcessChannelBuilder;
import io.grpc.inprocess.InProcessServerBuilder;
import io.grpc.internal.ServerImpl;
import io.grpc.stub.StreamObserver;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import org.joda.time.Duration;
Expand All @@ -51,10 +48,6 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

@RunWith(JUnit4.class)
public class PublisherImplTest {
Expand Down Expand Up @@ -87,8 +80,6 @@ public ManagedChannel getChannel(Executor executor) {
}
};

@Captor private ArgumentCaptor<PublishRequest> requestCaptor;

private FakeScheduledExecutorService fakeExecutor;

private FakeCredentials testCredentials;
Expand All @@ -101,16 +92,13 @@ class FakeException extends Exception {}

@Before
public void setUp() throws Exception {
testPublisherServiceImpl = Mockito.spy(new FakePublisherServiceImpl());
testPublisherServiceImpl = new FakePublisherServiceImpl();

InProcessServerBuilder serverBuilder = InProcessServerBuilder.forName("test-server");
serverBuilder.addService(testPublisherServiceImpl);
testServer = serverBuilder.build();
testServer.start();

MockitoAnnotations.initMocks(this);
testPublisherServiceImpl.reset();
Mockito.reset(testPublisherServiceImpl);
fakeExecutor = new FakeScheduledExecutorService();
}

Expand Down Expand Up @@ -146,9 +134,7 @@ public void testPublishByDuration() throws Exception {
assertEquals("1", publishFuture1.get());
assertEquals("2", publishFuture2.get());

Mockito.verify(testPublisherServiceImpl)
.publish(requestCaptor.capture(), Mockito.<StreamObserver<PublishResponse>>any());
assertEquals(2, requestCaptor.getValue().getMessagesCount());
assertEquals(2, testPublisherServiceImpl.getCapturedRequests().get(0).getMessagesCount());
publisher.shutdown();
}

Expand Down Expand Up @@ -185,10 +171,8 @@ public void testPublishByNumBundledMessages() throws Exception {
assertEquals("3", publishFuture3.get());
assertEquals("4", publishFuture4.get());

Mockito.verify(testPublisherServiceImpl, times(2))
.publish(requestCaptor.capture(), Mockito.<StreamObserver<PublishResponse>>any());
assertEquals(2, requestCaptor.getAllValues().get(0).getMessagesCount());
assertEquals(2, requestCaptor.getAllValues().get(1).getMessagesCount());
assertEquals(2, testPublisherServiceImpl.getCapturedRequests().get(0).getMessagesCount());
assertEquals(2, testPublisherServiceImpl.getCapturedRequests().get(1).getMessagesCount());
publisher.shutdown();
}

Expand Down Expand Up @@ -222,8 +206,7 @@ public void testSinglePublishByNumBytes() throws Exception {
assertEquals("3", publishFuture3.get());
assertEquals("4", publishFuture4.get());

Mockito.verify(testPublisherServiceImpl, times(2))
.publish(requestCaptor.capture(), Mockito.<StreamObserver<PublishResponse>>any());
assertEquals(2, testPublisherServiceImpl.getCapturedRequests().size());
publisher.shutdown();
}

Expand Down Expand Up @@ -264,10 +247,8 @@ public void testPublishMixedSizeAndDuration() throws Exception {

assertEquals("3", publishFuture3.get());

Mockito.verify(testPublisherServiceImpl, times(2))
.publish(requestCaptor.capture(), Mockito.<StreamObserver<PublishResponse>>any());
assertEquals(2, requestCaptor.getAllValues().get(0).getMessagesCount());
assertEquals(1, requestCaptor.getAllValues().get(1).getMessagesCount());
assertEquals(2, testPublisherServiceImpl.getCapturedRequests().get(0).getMessagesCount());
assertEquals(1, testPublisherServiceImpl.getCapturedRequests().get(1).getMessagesCount());
publisher.shutdown();
}

Expand Down Expand Up @@ -296,8 +277,7 @@ public void testPublishFailureRetries() throws Exception {

assertEquals("1", publishFuture1.get());

Mockito.verify(testPublisherServiceImpl, times(2))
.publish(Mockito.<PublishRequest>any(), Mockito.<StreamObserver<PublishResponse>>any());
assertEquals(2, testPublisherServiceImpl.getCapturedRequests().size());
publisher.shutdown();
}

Expand Down Expand Up @@ -331,8 +311,7 @@ public void testPublishFailureRetries_exceededsRetryDuration() throws Exception
throw new IllegalStateException("unexpected exception", e);
}
} finally {
Mockito.verify(testPublisherServiceImpl, atLeast(10))
.publish(Mockito.<PublishRequest>any(), Mockito.<StreamObserver<PublishResponse>>any());
assertTrue(testPublisherServiceImpl.getCapturedRequests().size() >= 10);
publisher.shutdown();
}
}
Expand Down Expand Up @@ -361,8 +340,7 @@ public void testPublishFailureRetries_nonRetryableFailsImmediately() throws Exce
try {
publishFuture1.get();
} finally {
Mockito.verify(testPublisherServiceImpl)
.publish(Mockito.<PublishRequest>any(), Mockito.<StreamObserver<PublishResponse>>any());
assertTrue(testPublisherServiceImpl.getCapturedRequests().size() >= 1);
publisher.shutdown();
}
}
Expand Down