|
| 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