/
NettyClientInteropServlet.java
171 lines (152 loc) · 5.56 KB
/
NettyClientInteropServlet.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
165
166
167
168
169
170
171
/*
* Copyright 2017 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.testing.integration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import io.grpc.Grpc;
import io.grpc.ManagedChannelBuilder;
import io.grpc.TlsChannelCredentials;
import io.grpc.netty.NettyChannelBuilder;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Ignore;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
/**
* This servlet communicates with {@code grpc-test.sandbox.googleapis.com}, which is a server
* managed by the gRPC team. For more information, see
* <a href="https://github.com/grpc/grpc/blob/master/doc/interop-test-descriptions.md">
* Interoperability Test Case Descriptions</a>.
*/
@SuppressWarnings("serial")
public final class NettyClientInteropServlet extends HttpServlet {
private static final String INTEROP_TEST_ADDRESS = "grpc-test.sandbox.googleapis.com:443";
private static final class LogEntryRecorder extends Handler {
private Queue<LogRecord> loggedMessages = new ConcurrentLinkedQueue<>();
@Override
public void publish(LogRecord logRecord) {
loggedMessages.add(logRecord);
}
@Override
public void flush() {}
@Override
public void close() {}
public String getLogOutput() {
SimpleFormatter formatter = new SimpleFormatter();
StringBuilder sb = new StringBuilder();
for (LogRecord loggedMessage : loggedMessages) {
sb.append(formatter.format(loggedMessage));
}
return sb.toString();
}
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
LogEntryRecorder handler = new LogEntryRecorder();
Logger.getLogger("").addHandler(handler);
try {
doGetHelper(resp);
} finally {
Logger.getLogger("").removeHandler(handler);
}
resp.getWriter().append("=======================================\n")
.append("Server side java.util.logging messages:\n")
.append(handler.getLogOutput());
}
private void doGetHelper(HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
PrintWriter writer = resp.getWriter();
writer.println("Test invoked at: ");
writer.println(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss Z")
.format(Calendar.getInstance().getTime()));
Result result = new JUnitCore().run(Tester.class);
if (result.wasSuccessful()) {
resp.setStatus(200);
writer.println(
String.format(
"PASS! Tests ran %d, tests ignored %d",
result.getRunCount(),
result.getIgnoreCount()));
} else {
resp.setStatus(500);
writer.println(
String.format(
"FAILED! Tests ran %d, tests failed %d, tests ignored %d",
result.getRunCount(),
result.getFailureCount(),
result.getIgnoreCount()));
for (Failure failure : result.getFailures()) {
writer.println("================================");
writer.println(failure.getTestHeader());
Throwable thrown = failure.getException();
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
thrown.printStackTrace(printWriter);
writer.println(stringWriter);
}
}
}
public static final class Tester extends AbstractInteropTest {
@Override
protected ManagedChannelBuilder<?> createChannelBuilder() {
assertEquals(
"jdk8 required",
"1.8",
System.getProperty("java.specification.version"));
ManagedChannelBuilder<?> builder =
Grpc.newChannelBuilder(INTEROP_TEST_ADDRESS, TlsChannelCredentials.create())
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE);
assertTrue(builder instanceof NettyChannelBuilder);
((NettyChannelBuilder) builder)
.flowControlWindow(AbstractInteropTest.TEST_FLOW_CONTROL_WINDOW);
return builder;
}
@Override
protected boolean metricsExpected() {
// Server-side metrics won't be found because the server is running remotely.
return false;
}
// grpc-test.sandbox.googleapis.com does not support these tests
@Ignore
@Override
public void customMetadata() { }
@Ignore
@Override
public void statusCodeAndMessage() { }
@Ignore
@Override
public void exchangeMetadataUnaryCall() { }
@Ignore
@Override
public void exchangeMetadataStreamingCall() { }
@Ignore
@Override
public void specialStatusMessage() {}
}
}