forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUdsChannelInteropTest.java
More file actions
129 lines (115 loc) · 4.44 KB
/
UdsChannelInteropTest.java
File metadata and controls
129 lines (115 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright 2021 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.grpc.android.integrationtest;
import static org.junit.Assert.assertEquals;
import android.net.LocalSocketAddress.Namespace;
import androidx.test.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;
import io.grpc.Grpc;
import io.grpc.InsecureServerCredentials;
import io.grpc.Server;
import io.grpc.android.UdsChannelBuilder;
import io.grpc.testing.integration.TestServiceImpl;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* Tests for channels created with {@link UdsChannelBuilder}. The UDS Channel is only meant to talk
* to Unix Domain Socket endpoints on servers that are on-device, so a {@link LocalTestServer} is
* set up to expose a UDS endpoint.
*/
@RunWith(AndroidJUnit4.class)
public class UdsChannelInteropTest {
private static final int TIMEOUT_SECONDS = 150;
private static final String UDS_PATH = "udspath";
private String testCase;
private Server server;
private UdsTcpEndpointConnector endpointConnector;
private ScheduledExecutorService serverExecutor = Executors.newScheduledThreadPool(2);
private ExecutorService testExecutor = Executors.newSingleThreadExecutor();
// Ensures Looper is initialized for tests running on API level 15. Otherwise instantiating an
// AsyncTask throws an exception.
@Rule
public ActivityTestRule<TesterActivity> activityRule =
new ActivityTestRule<TesterActivity>(TesterActivity.class);
@Before
public void setUp() throws IOException {
testCase = InstrumentationRegistry.getArguments().getString("test_case", "all");
// Start local server.
server =
Grpc.newServerBuilderForPort(0, InsecureServerCredentials.create())
.maxInboundMessageSize(16 * 1024 * 1024)
.addService(new TestServiceImpl(serverExecutor))
.build();
server.start();
// Connect uds endpoint to server's endpoint.
endpointConnector = new UdsTcpEndpointConnector(UDS_PATH, "0.0.0.0", server.getPort());
endpointConnector.start();
}
@After
public void teardown() {
server.shutdownNow();
endpointConnector.shutDown();
}
@Test
public void interopTests() throws Exception {
if (testCase.equals("all")) {
runTest("empty_unary");
runTest("large_unary");
runTest("client_streaming");
runTest("server_streaming");
runTest("ping_pong");
runTest("empty_stream");
runTest("cancel_after_begin");
runTest("cancel_after_first_response");
runTest("full_duplex_call_should_succeed");
runTest("half_duplex_call_should_succeed");
runTest("server_streaming_should_be_flow_controlled");
runTest("very_large_request");
runTest("very_large_response");
runTest("deadline_not_exceeded");
runTest("deadline_exceeded");
runTest("deadline_exceeded_server_streaming");
runTest("unimplemented_method");
runTest("timeout_on_sleeping_server");
runTest("graceful_shutdown");
} else {
runTest(testCase);
}
}
private void runTest(String testCase) throws Exception {
String result = null;
try {
result = testExecutor.submit(new TestCallable(
UdsChannelBuilder.forPath(UDS_PATH, Namespace.ABSTRACT)
.maxInboundMessageSize(16 * 1024 * 1024)
.build(),
testCase)).get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
assertEquals(testCase + " failed", TestCallable.SUCCESS_MESSAGE, result);
} catch (ExecutionException | InterruptedException e) {
result = e.getMessage();
}
}
}