• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 android.os.AsyncTask;
20 import android.util.Log;
21 import io.grpc.ManagedChannel;
22 import io.grpc.ManagedChannelBuilder;
23 import io.grpc.testing.integration.AbstractInteropTest;
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26 import java.lang.ref.WeakReference;
27 import org.junit.AssumptionViolatedException;
28 
29 /** AsyncTask for interop test cases. */
30 final class InteropTask extends AsyncTask<Void, Void, String> {
31   private static final String LOG_TAG = "GrpcInteropTask";
32 
33   interface Listener {
onComplete(String result)34     void onComplete(String result);
35   }
36 
37   static final String SUCCESS_MESSAGE = "Success!";
38 
39   private final WeakReference<Listener> listenerReference;
40   private final String testCase;
41   private final Tester tester;
42 
InteropTask( Listener listener, ManagedChannel channel, String testCase)43   InteropTask(
44       Listener listener,
45       ManagedChannel channel,
46       String testCase) {
47     this.listenerReference = new WeakReference<Listener>(listener);
48     this.testCase = testCase;
49     this.tester = new Tester(channel);
50   }
51 
52   @Override
onPreExecute()53   protected void onPreExecute() {
54     tester.setUp();
55   }
56 
57   @SuppressWarnings("Finally")
58   @Override
doInBackground(Void... ignored)59   protected String doInBackground(Void... ignored) {
60     try {
61       runTest(testCase);
62       return SUCCESS_MESSAGE;
63     } catch (Throwable t) {
64       // Print the stack trace to logcat.
65       t.printStackTrace();
66       // Then print to the error message.
67       StringWriter sw = new StringWriter();
68       t.printStackTrace(new PrintWriter(sw));
69       return "Failed... : " + t.getMessage() + "\n" + sw;
70     } finally {
71       try {
72         tester.tearDown();
73       } catch (RuntimeException ex) {
74         throw ex;
75       } catch (Exception ex) {
76         throw new RuntimeException(ex);
77       }
78     }
79   }
80 
runTest(String testCase)81   private void runTest(String testCase) throws Exception {
82     Log.i(LOG_TAG, "Running test case: " + testCase);
83     if ("empty_unary".equals(testCase)) {
84       tester.emptyUnary();
85     } else if ("large_unary".equals(testCase)) {
86       try {
87         tester.largeUnary();
88       } catch (AssumptionViolatedException e) {
89         // This test case requires more memory than most Android devices have available
90         Log.w(LOG_TAG, "Skipping " + testCase + " due to assumption violation", e);
91       }
92     } else if ("client_streaming".equals(testCase)) {
93       tester.clientStreaming();
94     } else if ("server_streaming".equals(testCase)) {
95       tester.serverStreaming();
96     } else if ("ping_pong".equals(testCase)) {
97       tester.pingPong();
98     } else if ("empty_stream".equals(testCase)) {
99       tester.emptyStream();
100     } else if ("cancel_after_begin".equals(testCase)) {
101       tester.cancelAfterBegin();
102     } else if ("cancel_after_first_response".equals(testCase)) {
103       tester.cancelAfterFirstResponse();
104     } else if ("full_duplex_call_should_succeed".equals(testCase)) {
105       tester.fullDuplexCallShouldSucceed();
106     } else if ("half_duplex_call_should_succeed".equals(testCase)) {
107       tester.halfDuplexCallShouldSucceed();
108     } else if ("server_streaming_should_be_flow_controlled".equals(testCase)) {
109       tester.serverStreamingShouldBeFlowControlled();
110     } else if ("very_large_request".equals(testCase)) {
111       try {
112         tester.veryLargeRequest();
113       } catch (AssumptionViolatedException e) {
114         // This test case requires more memory than most Android devices have available
115         Log.w(LOG_TAG, "Skipping " + testCase + " due to assumption violation", e);
116       }
117     } else if ("very_large_response".equals(testCase)) {
118       try {
119         tester.veryLargeResponse();
120       } catch (AssumptionViolatedException e) {
121         // This test case requires more memory than most Android devices have available
122         Log.w(LOG_TAG, "Skipping " + testCase + " due to assumption violation", e);
123       }
124     } else if ("deadline_not_exceeded".equals(testCase)) {
125       tester.deadlineNotExceeded();
126     } else if ("deadline_exceeded".equals(testCase)) {
127       tester.deadlineExceeded();
128     } else if ("deadline_exceeded_server_streaming".equals(testCase)) {
129       tester.deadlineExceededServerStreaming();
130     } else if ("unimplemented_method".equals(testCase)) {
131       tester.unimplementedMethod();
132     } else if ("timeout_on_sleeping_server".equals(testCase)) {
133       tester.timeoutOnSleepingServer();
134     } else if ("graceful_shutdown".equals(testCase)) {
135       tester.gracefulShutdown();
136     } else {
137       throw new IllegalArgumentException("Unimplemented/Unknown test case: " + testCase);
138     }
139   }
140 
141   @Override
onPostExecute(String result)142   protected void onPostExecute(String result) {
143     Listener listener = listenerReference.get();
144     if (listener != null) {
145       listener.onComplete(result);
146     }
147   }
148 
149   private static class Tester extends AbstractInteropTest {
150 
Tester(ManagedChannel channel)151     private Tester(ManagedChannel channel) {
152       this.channel = channel;
153     }
154 
155     @Override
createChannel()156     protected ManagedChannel createChannel() {
157       return channel;
158     }
159 
160     @Override
createChannelBuilder()161     protected ManagedChannelBuilder<?> createChannelBuilder() {
162       throw new UnsupportedOperationException();
163     }
164 
165     @Override
metricsExpected()166     protected boolean metricsExpected() {
167       return false;
168     }
169   }
170 }
171