• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 The gRPC Authors
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 io.grpc.android.integrationtest;
18 
19 import static java.util.concurrent.TimeUnit.SECONDS;
20 import static org.junit.Assert.assertEquals;
21 
22 import android.net.LocalSocketAddress.Namespace;
23 import androidx.test.InstrumentationRegistry;
24 import androidx.test.ext.junit.runners.AndroidJUnit4;
25 import androidx.test.rule.ActivityTestRule;
26 import com.google.common.util.concurrent.SettableFuture;
27 import io.grpc.Server;
28 import io.grpc.android.UdsChannelBuilder;
29 import io.grpc.android.integrationtest.InteropTask.Listener;
30 import io.grpc.netty.NettyServerBuilder;
31 import io.grpc.testing.integration.TestServiceImpl;
32 import java.io.IOException;
33 import java.util.concurrent.Executors;
34 import java.util.concurrent.ScheduledExecutorService;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 
41 /**
42  * Tests for channels created with {@link UdsChannelBuilder}. The UDS Channel is only meant to talk
43  * to Unix Domain Socket endpoints on servers that are on-device, so a {@link LocalTestServer} is
44  * set up to expose a UDS endpoint.
45  */
46 @RunWith(AndroidJUnit4.class)
47 public class UdsChannelInteropTest {
48   private static final int TIMEOUT_SECONDS = 150;
49 
50   private static final String UDS_PATH = "udspath";
51   private String testCase;
52 
53   private Server server;
54   private UdsTcpEndpointConnector endpointConnector;
55 
56   private ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
57 
58   // Ensures Looper is initialized for tests running on API level 15. Otherwise instantiating an
59   // AsyncTask throws an exception.
60   @Rule
61   public ActivityTestRule<TesterActivity> activityRule =
62       new ActivityTestRule<TesterActivity>(TesterActivity.class);
63 
64   @Before
setUp()65   public void setUp() throws IOException {
66     testCase = InstrumentationRegistry.getArguments().getString("test_case", "all");
67 
68     // Start local server.
69     server =
70         NettyServerBuilder.forPort(0)
71             .maxInboundMessageSize(16 * 1024 * 1024)
72             .addService(new TestServiceImpl(executor))
73             .build();
74     server.start();
75 
76     // Connect uds endpoint to server's endpoint.
77     endpointConnector = new UdsTcpEndpointConnector(UDS_PATH, "0.0.0.0", server.getPort());
78     endpointConnector.start();
79   }
80 
81   @After
teardown()82   public void teardown() {
83     server.shutdownNow();
84     endpointConnector.shutDown();
85   }
86 
87   @Test
interopTests()88   public void interopTests() throws Exception {
89     if (testCase.equals("all")) {
90       runTest("empty_unary");
91       runTest("large_unary");
92       runTest("client_streaming");
93       runTest("server_streaming");
94       runTest("ping_pong");
95       runTest("empty_stream");
96       runTest("cancel_after_begin");
97       runTest("cancel_after_first_response");
98       runTest("full_duplex_call_should_succeed");
99       runTest("half_duplex_call_should_succeed");
100       runTest("server_streaming_should_be_flow_controlled");
101       runTest("very_large_request");
102       runTest("very_large_response");
103       runTest("deadline_not_exceeded");
104       runTest("deadline_exceeded");
105       runTest("deadline_exceeded_server_streaming");
106       runTest("unimplemented_method");
107       runTest("timeout_on_sleeping_server");
108       runTest("graceful_shutdown");
109     } else {
110       runTest(testCase);
111     }
112   }
113 
runTest(String testCase)114   private void runTest(String testCase) throws Exception {
115     final SettableFuture<String> resultFuture = SettableFuture.create();
116     InteropTask.Listener listener =
117         new Listener() {
118           @Override
119           public void onComplete(String result) {
120             resultFuture.set(result);
121           }
122         };
123 
124     new InteropTask(
125             listener,
126             UdsChannelBuilder.forPath(UDS_PATH, Namespace.ABSTRACT)
127                 .maxInboundMessageSize(16 * 1024 * 1024)
128                 .build(),
129             testCase)
130         .execute();
131     String result = resultFuture.get(TIMEOUT_SECONDS, SECONDS);
132     assertEquals(testCase + " failed", InteropTask.SUCCESS_MESSAGE, result);
133   }
134 }
135