1 /*
2  * Copyright 2016 Google LLC
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google LLC nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 package com.google.api.gax.grpc.testing;
31 
32 import com.google.api.core.BetaApi;
33 import com.google.common.annotations.VisibleForTesting;
34 import com.google.common.collect.Lists;
35 import io.grpc.Server;
36 import io.grpc.inprocess.InProcessServerBuilder;
37 import java.io.IOException;
38 import java.util.Arrays;
39 import java.util.List;
40 
41 /** A utility class to control a local service which is used by testing. */
42 @BetaApi
43 public class MockServiceHelper {
44   private static final int FLOW_CONTROL_WINDOW = 65 * 1024;
45 
46   private final String addressString;
47   private final Server server;
48   private final List<MockGrpcService> mockServices;
49 
50   /** Constructs a new MockServiceHelper. The method start() must be called before it is used. */
MockServiceHelper(String addressString, MockGrpcService mockService)51   public MockServiceHelper(String addressString, MockGrpcService mockService) {
52     this(addressString, Arrays.asList(mockService));
53   }
54 
MockServiceHelper(String addressString, List<MockGrpcService> mockServices)55   public MockServiceHelper(String addressString, List<MockGrpcService> mockServices) {
56     this.addressString = addressString;
57     this.mockServices = Lists.newArrayList(mockServices);
58     InProcessServerBuilder builder = InProcessServerBuilder.forName(addressString);
59     for (MockGrpcService mockService : mockServices) {
60       builder.addService(mockService.getServiceDefinition());
61     }
62     this.server = builder.build();
63   }
64 
65   @VisibleForTesting
MockServiceHelper(Server server, String address, MockGrpcService mockService)66   MockServiceHelper(Server server, String address, MockGrpcService mockService) {
67     this(server, address, Arrays.asList(mockService));
68   }
69 
70   @VisibleForTesting
MockServiceHelper(Server server, String addressString, List<MockGrpcService> mockServices)71   MockServiceHelper(Server server, String addressString, List<MockGrpcService> mockServices) {
72     this.server = server;
73     this.addressString = addressString;
74     this.mockServices = mockServices;
75   }
76 
77   /** Starts the local server. */
start()78   public void start() {
79     try {
80       server.start();
81     } catch (IOException ex) {
82       throw new RuntimeException(ex);
83     }
84   }
85 
86   /** Resets the state of the mock service. */
reset()87   public void reset() {
88     for (MockGrpcService mockService : mockServices) {
89       mockService.reset();
90     }
91   }
92 
93   /** Stops the local server. */
stop()94   public void stop() {
95     server.shutdownNow();
96   }
97 
98   /** Returns the mock grpc service. */
getService()99   public MockGrpcService getService() {
100     if (mockServices.size() != 1) {
101       throw new IllegalStateException(
102           "MockServiceHelper.getService() can only be called if "
103               + "there is one service, but there are "
104               + mockServices.size());
105     }
106     return mockServices.get(0);
107   }
108 
109   /** Returns all of the mocked grpc services. */
getServices()110   public List<MockGrpcService> getServices() {
111     return mockServices;
112   }
113 
114   /** Creates a channel for making requests to the mock service. */
createChannelProvider()115   public LocalChannelProvider createChannelProvider() {
116     return LocalChannelProvider.create(addressString);
117   }
118 }
119