shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.incubator.protobuf.services.nb; |
| 18 | |
| 19 | import com.google.common.collect.Sets; |
| 20 | import io.grpc.Server; |
| 21 | import io.grpc.inprocess.InProcessServerBuilder; |
| 22 | import io.grpc.internal.AbstractServerImplBuilder; |
| 23 | |
| 24 | import java.io.IOException; |
| 25 | import java.util.Set; |
| 26 | /** |
| 27 | * InProcessServer that manages startup/shutdown of a service within the same process |
| 28 | * as the client is running. Used for unit testing purposes. |
| 29 | */ |
| 30 | public class InProcessServer<T extends io.grpc.BindableService> { |
| 31 | private Server server; |
| 32 | |
| 33 | Set<T> services = Sets.newHashSet(); |
| 34 | |
| 35 | private Class<T> clazz; |
| 36 | |
| 37 | public InProcessServer(Class<T> clazz) { |
| 38 | this.clazz = clazz; |
| 39 | } |
| 40 | |
| 41 | public void addServiceToBind(T service) { |
| 42 | if (service != null) { |
| 43 | services.add(service); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public void start() throws IOException, InstantiationException, IllegalAccessException { |
| 48 | |
| 49 | AbstractServerImplBuilder builder = InProcessServerBuilder.forName("test").directExecutor(); |
| 50 | services.forEach(service -> builder.addService(service)); |
| 51 | server = builder.build().start(); |
| 52 | Runtime.getRuntime().addShutdownHook(new Thread() { |
| 53 | @Override |
| 54 | public void run() { |
| 55 | // Use stderr here since the logger may have been reset by its JVM shutdown hook. |
| 56 | System.err.println("*** shutting down gRPC server since JVM is shutting down"); |
| 57 | InProcessServer.this.stop(); |
| 58 | System.err.println("*** server shut down"); |
| 59 | } |
| 60 | }); |
| 61 | } |
| 62 | |
Tian Jian | 5e4be91 | 2017-09-19 12:47:55 +0800 | [diff] [blame] | 63 | public void stop() { |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 64 | if (server != null) { |
| 65 | server.shutdown(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Await termination on the main thread since the grpc library uses daemon threads. |
| 71 | * @throws InterruptedException if there is an issue |
| 72 | */ |
| 73 | public void blockUntilShutdown() throws InterruptedException { |
| 74 | if (server != null) { |
| 75 | server.awaitTermination(); |
| 76 | } |
| 77 | } |
| 78 | } |