Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
Added unit tests, removed getOrBuild methods
Browse files Browse the repository at this point in the history
Pre-push hook installed.

Change-Id: I193be2e6a6ad2e35b5f1def5182e19d9dc14ab29
  • Loading branch information
michaelbausor committed Apr 27, 2016
1 parent 0d24ef5 commit 80a2f14
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 16 deletions.
16 changes: 0 additions & 16 deletions src/main/java/com/google/api/gax/grpc/ServiceApiSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,6 @@ protected ServiceApiSettings(
this.generatorVersion = generatorVersion;
}

/**
* Return the channel to be used to connect to the service, retrieved using the channelProvider.
* If no channel was set, a default channel will be instantiated.
*/
public final ManagedChannel getOrBuildChannel() throws IOException, IllegalStateException {
return getChannelProvider().getChannel(getOrBuildExecutor());
}

/**
* Return the channel provider. If no channel provider was set, the default channel provider will
* be returned.
Expand All @@ -91,14 +83,6 @@ public final ChannelProvider getChannelProvider() {
return channelProvider;
}

/**
* The Executor used for channels, retries, and bundling, retrieved using the executorProvider. If
* no executor was set, a default executor will be instantiated.
*/
public final ScheduledExecutorService getOrBuildExecutor() throws IllegalStateException {
return getExecutorProvider().getExecutor();
}

/**
* Return the executor provider. It no executor provider was set, the default executor provider
* will be returned.
Expand Down
153 changes: 153 additions & 0 deletions src/test/java/com/google/api/gax/grpc/ServiceApiSettingsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package com.google.api.gax.grpc;

import com.google.api.gax.core.ConnectionSettings;
import com.google.common.collect.ImmutableList;
import com.google.common.truth.Truth;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;

import io.grpc.ManagedChannel;

import java.io.IOException;
import java.util.concurrent.ScheduledExecutorService;

/**
* Tests for {@link ServiceApiSettings}.
*/
@RunWith(JUnit4.class)
public class ServiceApiSettingsTest {

@Rule public ExpectedException thrown = ExpectedException.none();

private static class FakeSettings extends ServiceApiSettings {

public static final String DEFAULT_SERVICE_ADDRESS = "pubsub-experimental.googleapis.com";
public static final int DEFAULT_SERVICE_PORT = 443;
public static final ImmutableList<String> DEFAULT_SERVICE_SCOPES =
ImmutableList.<String>builder()
.add("https://www.googleapis.com/auth/pubsub")
.add("https://www.googleapis.com/auth/cloud-platform")
.build();
public static final ConnectionSettings DEFAULT_CONNECTION_SETTINGS =
ConnectionSettings.newBuilder()
.setServiceAddress(DEFAULT_SERVICE_ADDRESS)
.setPort(DEFAULT_SERVICE_PORT)
.provideCredentialsWith(DEFAULT_SERVICE_SCOPES)
.build();

public static Builder createBuilder(ConnectionSettings connectionSettings) {
return new Builder(connectionSettings);
}

private FakeSettings(Builder settingsBuilder) throws IOException {
super(
settingsBuilder.getChannelProvider(),
settingsBuilder.getExecutorProvider(),
settingsBuilder.getGeneratorName(),
settingsBuilder.getGeneratorVersion(),
settingsBuilder.getClientLibName(),
settingsBuilder.getClientLibVersion());
}

private static class Builder extends ServiceApiSettings.Builder {

private Builder(ConnectionSettings connectionSettings) {
super(connectionSettings);
}

@Override
public FakeSettings build() throws IOException {
return new FakeSettings(this);
}

@Override
public Builder provideExecutorWith(
final ScheduledExecutorService executor, boolean shouldAutoClose) {
super.provideExecutorWith(executor, shouldAutoClose);
return this;
}

@Override
public Builder provideChannelWith(ManagedChannel channel, boolean shouldAutoClose) {
super.provideChannelWith(channel, shouldAutoClose);
return this;
}

@Override
public Builder provideChannelWith(ConnectionSettings settings) {
super.provideChannelWith(settings);
return this;
}
}
}

@Test
public void fixedChannelAutoClose() throws IOException {
thrown.expect(IllegalStateException.class);
ManagedChannel channel = Mockito.mock(ManagedChannel.class);
FakeSettings settings =
FakeSettings.createBuilder(FakeSettings.DEFAULT_CONNECTION_SETTINGS)
.provideChannelWith(channel, true)
.build();
ChannelProvider channelProvider = settings.getChannelProvider();
ScheduledExecutorService executor = settings.getExecutorProvider().getExecutor();
ManagedChannel channelA = channelProvider.getChannel(executor);
ManagedChannel channelB = channelProvider.getChannel(executor);
}

@Test
public void fixedChannelNoAutoClose() throws IOException {
ManagedChannel channel = Mockito.mock(ManagedChannel.class);
FakeSettings settings =
FakeSettings.createBuilder(FakeSettings.DEFAULT_CONNECTION_SETTINGS)
.provideChannelWith(channel, false)
.build();
ChannelProvider channelProvider = settings.getChannelProvider();
ScheduledExecutorService executor = settings.getExecutorProvider().getExecutor();
ManagedChannel channelA = channelProvider.getChannel(executor);
ManagedChannel channelB = channelProvider.getChannel(executor);
Truth.assertThat(channelA).isEqualTo(channelB);
}

@Test
public void defaultExecutor() throws IOException {
FakeSettings settings =
FakeSettings.createBuilder(FakeSettings.DEFAULT_CONNECTION_SETTINGS).build();
ExecutorProvider executorProvider = settings.getExecutorProvider();
ScheduledExecutorService executorA = executorProvider.getExecutor();
ScheduledExecutorService executorB = executorProvider.getExecutor();
Truth.assertThat(executorA).isNotEqualTo(executorB);
}

@Test
public void fixedExecutorAutoClose() throws IOException {
thrown.expect(IllegalStateException.class);
ScheduledExecutorService executor = Mockito.mock(ScheduledExecutorService.class);
FakeSettings settings =
FakeSettings.createBuilder(FakeSettings.DEFAULT_CONNECTION_SETTINGS)
.provideExecutorWith(executor, true)
.build();
ExecutorProvider executorProvider = settings.getExecutorProvider();
ScheduledExecutorService executorA = executorProvider.getExecutor();
ScheduledExecutorService executorB = executorProvider.getExecutor();
}

@Test
public void fixedExecutorNoAutoClose() throws IOException {
ScheduledExecutorService executor = Mockito.mock(ScheduledExecutorService.class);
FakeSettings settings =
FakeSettings.createBuilder(FakeSettings.DEFAULT_CONNECTION_SETTINGS)
.provideExecutorWith(executor, false)
.build();
ExecutorProvider executorProvider = settings.getExecutorProvider();
ScheduledExecutorService executorA = executorProvider.getExecutor();
ScheduledExecutorService executorB = executorProvider.getExecutor();
Truth.assertThat(executorA).isEqualTo(executorB);
}
}

0 comments on commit 80a2f14

Please sign in to comment.