-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
InteropTask.java
164 lines (149 loc) · 5.41 KB
/
InteropTask.java
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright 2018 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 android.os.AsyncTask;
import android.util.Log;
import io.grpc.ManagedChannel;
import io.grpc.testing.integration.AbstractInteropTest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.ref.WeakReference;
import org.junit.AssumptionViolatedException;
/** AsyncTask for interop test cases. */
final class InteropTask extends AsyncTask<Void, Void, String> {
private static final String LOG_TAG = "GrpcInteropTask";
interface Listener {
void onComplete(String result);
}
static final String SUCCESS_MESSAGE = "Success!";
private final WeakReference<Listener> listenerReference;
private final String testCase;
private final Tester tester;
InteropTask(
Listener listener,
ManagedChannel channel,
String testCase) {
this.listenerReference = new WeakReference<Listener>(listener);
this.testCase = testCase;
this.tester = new Tester(channel);
}
@Override
protected void onPreExecute() {
tester.setUp();
}
@SuppressWarnings("Finally")
@Override
protected String doInBackground(Void... ignored) {
try {
runTest(testCase);
return SUCCESS_MESSAGE;
} catch (Throwable t) {
// Print the stack trace to logcat.
t.printStackTrace();
// Then print to the error message.
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
return "Failed... : " + t.getMessage() + "\n" + sw;
} finally {
try {
tester.tearDown();
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
private void runTest(String testCase) throws Exception {
Log.i(LOG_TAG, "Running test case: " + testCase);
if ("empty_unary".equals(testCase)) {
tester.emptyUnary();
} else if ("large_unary".equals(testCase)) {
try {
tester.largeUnary();
} catch (AssumptionViolatedException e) {
// This test case requires more memory than most Android devices have available
Log.w(LOG_TAG, "Skipping " + testCase + " due to assumption violation", e);
}
} else if ("client_streaming".equals(testCase)) {
tester.clientStreaming();
} else if ("server_streaming".equals(testCase)) {
tester.serverStreaming();
} else if ("ping_pong".equals(testCase)) {
tester.pingPong();
} else if ("empty_stream".equals(testCase)) {
tester.emptyStream();
} else if ("cancel_after_begin".equals(testCase)) {
tester.cancelAfterBegin();
} else if ("cancel_after_first_response".equals(testCase)) {
tester.cancelAfterFirstResponse();
} else if ("full_duplex_call_should_succeed".equals(testCase)) {
tester.fullDuplexCallShouldSucceed();
} else if ("half_duplex_call_should_succeed".equals(testCase)) {
tester.halfDuplexCallShouldSucceed();
} else if ("server_streaming_should_be_flow_controlled".equals(testCase)) {
tester.serverStreamingShouldBeFlowControlled();
} else if ("very_large_request".equals(testCase)) {
try {
tester.veryLargeRequest();
} catch (AssumptionViolatedException e) {
// This test case requires more memory than most Android devices have available
Log.w(LOG_TAG, "Skipping " + testCase + " due to assumption violation", e);
}
} else if ("very_large_response".equals(testCase)) {
try {
tester.veryLargeResponse();
} catch (AssumptionViolatedException e) {
// This test case requires more memory than most Android devices have available
Log.w(LOG_TAG, "Skipping " + testCase + " due to assumption violation", e);
}
} else if ("deadline_not_exceeded".equals(testCase)) {
tester.deadlineNotExceeded();
} else if ("deadline_exceeded".equals(testCase)) {
tester.deadlineExceeded();
} else if ("deadline_exceeded_server_streaming".equals(testCase)) {
tester.deadlineExceededServerStreaming();
} else if ("unimplemented_method".equals(testCase)) {
tester.unimplementedMethod();
} else if ("timeout_on_sleeping_server".equals(testCase)) {
tester.timeoutOnSleepingServer();
} else if ("graceful_shutdown".equals(testCase)) {
tester.gracefulShutdown();
} else {
throw new IllegalArgumentException("Unimplemented/Unknown test case: " + testCase);
}
}
@Override
protected void onPostExecute(String result) {
Listener listener = listenerReference.get();
if (listener != null) {
listener.onComplete(result);
}
}
private static class Tester extends AbstractInteropTest {
private Tester(ManagedChannel channel) {
this.channel = channel;
}
@Override
protected ManagedChannel createChannel() {
return channel;
}
@Override
protected boolean metricsExpected() {
return false;
}
}
}