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

Commit 3169d16

Browse files
committed
Fixes and improvements
Added unit tests Removed getOrBuild methods Set executorProvider in ServiceApiSettings constructor Pre-push hook installed. Change-Id: I193be2e6a6ad2e35b5f1def5182e19d9dc14ab29
1 parent 0d24ef5 commit 3169d16

2 files changed

Lines changed: 154 additions & 16 deletions

File tree

src/main/java/com/google/api/gax/grpc/ServiceApiSettings.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,6 @@ protected ServiceApiSettings(
7575
this.generatorVersion = generatorVersion;
7676
}
7777

78-
/**
79-
* Return the channel to be used to connect to the service, retrieved using the channelProvider.
80-
* If no channel was set, a default channel will be instantiated.
81-
*/
82-
public final ManagedChannel getOrBuildChannel() throws IOException, IllegalStateException {
83-
return getChannelProvider().getChannel(getOrBuildExecutor());
84-
}
85-
8678
/**
8779
* Return the channel provider. If no channel provider was set, the default channel provider will
8880
* be returned.
@@ -91,14 +83,6 @@ public final ChannelProvider getChannelProvider() {
9183
return channelProvider;
9284
}
9385

94-
/**
95-
* The Executor used for channels, retries, and bundling, retrieved using the executorProvider. If
96-
* no executor was set, a default executor will be instantiated.
97-
*/
98-
public final ScheduledExecutorService getOrBuildExecutor() throws IllegalStateException {
99-
return getExecutorProvider().getExecutor();
100-
}
101-
10286
/**
10387
* Return the executor provider. It no executor provider was set, the default executor provider
10488
* will be returned.
@@ -136,6 +120,7 @@ protected Builder(ConnectionSettings connectionSettings) {
136120
protected Builder(ServiceApiSettings settings) {
137121
this();
138122
this.channelProvider = settings.channelProvider;
123+
this.executorProvider = settings.executorProvider;
139124
this.clientLibName = settings.clientLibName;
140125
this.clientLibVersion = settings.clientLibVersion;
141126
this.serviceGeneratorName = settings.generatorName;
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package com.google.api.gax.grpc;
2+
3+
import com.google.api.gax.core.ConnectionSettings;
4+
import com.google.common.collect.ImmutableList;
5+
import com.google.common.truth.Truth;
6+
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
import org.junit.rules.ExpectedException;
10+
import org.junit.runner.RunWith;
11+
import org.junit.runners.JUnit4;
12+
import org.mockito.Mockito;
13+
14+
import io.grpc.ManagedChannel;
15+
16+
import java.io.IOException;
17+
import java.util.concurrent.ScheduledExecutorService;
18+
19+
/**
20+
* Tests for {@link ServiceApiSettings}.
21+
*/
22+
@RunWith(JUnit4.class)
23+
public class ServiceApiSettingsTest {
24+
25+
@Rule public ExpectedException thrown = ExpectedException.none();
26+
27+
private static class FakeSettings extends ServiceApiSettings {
28+
29+
public static final String DEFAULT_SERVICE_ADDRESS = "pubsub-experimental.googleapis.com";
30+
public static final int DEFAULT_SERVICE_PORT = 443;
31+
public static final ImmutableList<String> DEFAULT_SERVICE_SCOPES =
32+
ImmutableList.<String>builder()
33+
.add("https://www.googleapis.com/auth/pubsub")
34+
.add("https://www.googleapis.com/auth/cloud-platform")
35+
.build();
36+
public static final ConnectionSettings DEFAULT_CONNECTION_SETTINGS =
37+
ConnectionSettings.newBuilder()
38+
.setServiceAddress(DEFAULT_SERVICE_ADDRESS)
39+
.setPort(DEFAULT_SERVICE_PORT)
40+
.provideCredentialsWith(DEFAULT_SERVICE_SCOPES)
41+
.build();
42+
43+
public static Builder createBuilder(ConnectionSettings connectionSettings) {
44+
return new Builder(connectionSettings);
45+
}
46+
47+
private FakeSettings(Builder settingsBuilder) throws IOException {
48+
super(
49+
settingsBuilder.getChannelProvider(),
50+
settingsBuilder.getExecutorProvider(),
51+
settingsBuilder.getGeneratorName(),
52+
settingsBuilder.getGeneratorVersion(),
53+
settingsBuilder.getClientLibName(),
54+
settingsBuilder.getClientLibVersion());
55+
}
56+
57+
private static class Builder extends ServiceApiSettings.Builder {
58+
59+
private Builder(ConnectionSettings connectionSettings) {
60+
super(connectionSettings);
61+
}
62+
63+
@Override
64+
public FakeSettings build() throws IOException {
65+
return new FakeSettings(this);
66+
}
67+
68+
@Override
69+
public Builder provideExecutorWith(
70+
final ScheduledExecutorService executor, boolean shouldAutoClose) {
71+
super.provideExecutorWith(executor, shouldAutoClose);
72+
return this;
73+
}
74+
75+
@Override
76+
public Builder provideChannelWith(ManagedChannel channel, boolean shouldAutoClose) {
77+
super.provideChannelWith(channel, shouldAutoClose);
78+
return this;
79+
}
80+
81+
@Override
82+
public Builder provideChannelWith(ConnectionSettings settings) {
83+
super.provideChannelWith(settings);
84+
return this;
85+
}
86+
}
87+
}
88+
89+
@Test
90+
public void fixedChannelAutoClose() throws IOException {
91+
thrown.expect(IllegalStateException.class);
92+
ManagedChannel channel = Mockito.mock(ManagedChannel.class);
93+
FakeSettings settings =
94+
FakeSettings.createBuilder(FakeSettings.DEFAULT_CONNECTION_SETTINGS)
95+
.provideChannelWith(channel, true)
96+
.build();
97+
ChannelProvider channelProvider = settings.getChannelProvider();
98+
ScheduledExecutorService executor = settings.getExecutorProvider().getExecutor();
99+
ManagedChannel channelA = channelProvider.getChannel(executor);
100+
ManagedChannel channelB = channelProvider.getChannel(executor);
101+
}
102+
103+
@Test
104+
public void fixedChannelNoAutoClose() throws IOException {
105+
ManagedChannel channel = Mockito.mock(ManagedChannel.class);
106+
FakeSettings settings =
107+
FakeSettings.createBuilder(FakeSettings.DEFAULT_CONNECTION_SETTINGS)
108+
.provideChannelWith(channel, false)
109+
.build();
110+
ChannelProvider channelProvider = settings.getChannelProvider();
111+
ScheduledExecutorService executor = settings.getExecutorProvider().getExecutor();
112+
ManagedChannel channelA = channelProvider.getChannel(executor);
113+
ManagedChannel channelB = channelProvider.getChannel(executor);
114+
Truth.assertThat(channelA).isEqualTo(channelB);
115+
}
116+
117+
@Test
118+
public void defaultExecutor() throws IOException {
119+
FakeSettings settings =
120+
FakeSettings.createBuilder(FakeSettings.DEFAULT_CONNECTION_SETTINGS).build();
121+
ExecutorProvider executorProvider = settings.getExecutorProvider();
122+
ScheduledExecutorService executorA = executorProvider.getExecutor();
123+
ScheduledExecutorService executorB = executorProvider.getExecutor();
124+
Truth.assertThat(executorA).isNotEqualTo(executorB);
125+
}
126+
127+
@Test
128+
public void fixedExecutorAutoClose() throws IOException {
129+
thrown.expect(IllegalStateException.class);
130+
ScheduledExecutorService executor = Mockito.mock(ScheduledExecutorService.class);
131+
FakeSettings settings =
132+
FakeSettings.createBuilder(FakeSettings.DEFAULT_CONNECTION_SETTINGS)
133+
.provideExecutorWith(executor, true)
134+
.build();
135+
ExecutorProvider executorProvider = settings.getExecutorProvider();
136+
ScheduledExecutorService executorA = executorProvider.getExecutor();
137+
ScheduledExecutorService executorB = executorProvider.getExecutor();
138+
}
139+
140+
@Test
141+
public void fixedExecutorNoAutoClose() throws IOException {
142+
ScheduledExecutorService executor = Mockito.mock(ScheduledExecutorService.class);
143+
FakeSettings settings =
144+
FakeSettings.createBuilder(FakeSettings.DEFAULT_CONNECTION_SETTINGS)
145+
.provideExecutorWith(executor, false)
146+
.build();
147+
ExecutorProvider executorProvider = settings.getExecutorProvider();
148+
ScheduledExecutorService executorA = executorProvider.getExecutor();
149+
ScheduledExecutorService executorB = executorProvider.getExecutor();
150+
Truth.assertThat(executorA).isEqualTo(executorB);
151+
}
152+
}
153+

0 commit comments

Comments
 (0)