This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests, removed getOrBuild methods
Pre-push hook installed. Change-Id: I193be2e6a6ad2e35b5f1def5182e19d9dc14ab29
- Loading branch information
1 parent
0d24ef5
commit 80a2f14
Showing
2 changed files
with
153 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
src/test/java/com/google/api/gax/grpc/ServiceApiSettingsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|