■ ■ ■ ■ ■ ■
plugin/src/test/java/com/google/tsunami/plugin/PluginServiceClientTest.java
| 1 | + | /* |
| 2 | + | * Copyright 2022 Google LLC |
| 3 | + | * |
| 4 | + | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + | * you may not use this file except in compliance with the License. |
| 6 | + | * You may obtain a copy of the License at |
| 7 | + | * |
| 8 | + | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + | * |
| 10 | + | * Unless required by applicable law or agreed to in writing, software |
| 11 | + | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + | * See the License for the specific language governing permissions and |
| 14 | + | * limitations under the License. |
| 15 | + | */ |
| 16 | + | package com.google.tsunami.plugin; |
| 17 | + | |
| 18 | + | import static com.google.common.truth.Truth.assertThat; |
| 19 | + | |
| 20 | + | import com.google.common.util.concurrent.ListeningScheduledExecutorService; |
| 21 | + | import com.google.inject.Guice; |
| 22 | + | import com.google.inject.Key; |
| 23 | + | import com.google.tsunami.common.concurrent.ScheduledThreadPoolModule; |
| 24 | + | import io.grpc.inprocess.InProcessChannelBuilder; |
| 25 | + | import io.grpc.inprocess.InProcessServerBuilder; |
| 26 | + | import io.grpc.testing.GrpcCleanupRule; |
| 27 | + | import io.grpc.util.MutableHandlerRegistry; |
| 28 | + | import java.lang.annotation.Retention; |
| 29 | + | import java.lang.annotation.RetentionPolicy; |
| 30 | + | import javax.inject.Qualifier; |
| 31 | + | import org.junit.Before; |
| 32 | + | import org.junit.Rule; |
| 33 | + | import org.junit.Test; |
| 34 | + | import org.junit.runner.RunWith; |
| 35 | + | import org.junit.runners.JUnit4; |
| 36 | + | |
| 37 | + | /** Tests for {@link PluginServiceClient}. */ |
| 38 | + | @RunWith(JUnit4.class) |
| 39 | + | public final class PluginServiceClientTest { |
| 40 | + | |
| 41 | + | // Useful test thread pool used for testing grpc handlers |
| 42 | + | @Qualifier |
| 43 | + | @Retention(RetentionPolicy.RUNTIME) |
| 44 | + | @interface TestThreadPool {} |
| 45 | + | |
| 46 | + | @Rule public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule(); |
| 47 | + | |
| 48 | + | private static final int THREAD_POOLS = 1; |
| 49 | + | private static final String THREAD_POOL_NAME = "test"; |
| 50 | + | |
| 51 | + | private PluginServiceClient pluginService; |
| 52 | + | private final MutableHandlerRegistry serviceRegistry = new MutableHandlerRegistry(); |
| 53 | + | |
| 54 | + | @Before |
| 55 | + | public void setUp() throws Exception { |
| 56 | + | String serverName = InProcessServerBuilder.generateName(); |
| 57 | + | grpcCleanup.register( |
| 58 | + | InProcessServerBuilder.forName(serverName) |
| 59 | + | .fallbackHandlerRegistry(serviceRegistry) |
| 60 | + | .directExecutor() |
| 61 | + | .build() |
| 62 | + | .start()); |
| 63 | + | |
| 64 | + | pluginService = |
| 65 | + | new PluginServiceClient( |
| 66 | + | InProcessChannelBuilder.forName(serverName).directExecutor().build(), |
| 67 | + | Guice.createInjector( |
| 68 | + | new ScheduledThreadPoolModule.Builder() |
| 69 | + | .setName(THREAD_POOL_NAME) |
| 70 | + | .setSize(THREAD_POOLS) |
| 71 | + | .setAnnotation(TestThreadPool.class) |
| 72 | + | .build()) |
| 73 | + | .getInstance( |
| 74 | + | Key.get(ListeningScheduledExecutorService.class, TestThreadPool.class))); |
| 75 | + | } |
| 76 | + | |
| 77 | + | @Test |
| 78 | + | public void pluginService_isNotNull() { |
| 79 | + | assertThat(pluginService).isNotNull(); |
| 80 | + | } |
| 81 | + | |
| 82 | + | } |
| 83 | + | |