-
Notifications
You must be signed in to change notification settings - Fork 852
/
AddressArgs_CONNECTIONNUM_Test.java
292 lines (264 loc) · 11.7 KB
/
AddressArgs_CONNECTIONNUM_Test.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 com.alipay.remoting.rpc.addressargs;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alipay.remoting.ConnectionEventType;
import com.alipay.remoting.InvokeCallback;
import com.alipay.remoting.Url;
import com.alipay.remoting.exception.RemotingException;
import com.alipay.remoting.rpc.RpcAddressParser;
import com.alipay.remoting.rpc.RpcClient;
import com.alipay.remoting.rpc.RpcResponseFuture;
import com.alipay.remoting.rpc.common.BoltServer;
import com.alipay.remoting.rpc.common.CONNECTEventProcessor;
import com.alipay.remoting.rpc.common.DISCONNECTEventProcessor;
import com.alipay.remoting.rpc.common.PortScan;
import com.alipay.remoting.rpc.common.RequestBody;
import com.alipay.remoting.rpc.common.SimpleClientUserProcessor;
import com.alipay.remoting.rpc.common.SimpleServerUserProcessor;
/**
* address args test [_CONNECTIONNUM]
*
* @author xiaomin.cxm
* @version $Id: AddressArgs_CONNECTIONNUM_Test.java, v 0.1 Feb 17, 2016 2:01:54 PM xiaomin.cxm Exp $
*/
public class AddressArgs_CONNECTIONNUM_Test {
static Logger logger = LoggerFactory
.getLogger(AddressArgs_CONNECTIONNUM_Test.class);
BoltServer server;
RpcClient client;
int port = PortScan.select();
String ip = "127.0.0.1";
String addr = "127.0.0.1:" + port;
int invokeTimes = 5;
SimpleServerUserProcessor serverUserProcessor = new SimpleServerUserProcessor();
SimpleClientUserProcessor clientUserProcessor = new SimpleClientUserProcessor();
CONNECTEventProcessor clientConnectProcessor = new CONNECTEventProcessor();
CONNECTEventProcessor serverConnectProcessor = new CONNECTEventProcessor();
DISCONNECTEventProcessor clientDisConnectProcessor = new DISCONNECTEventProcessor();
DISCONNECTEventProcessor serverDisConnectProcessor = new DISCONNECTEventProcessor();
@Before
public void init() {
server = new BoltServer(port);
server.start();
server.addConnectionEventProcessor(ConnectionEventType.CONNECT, serverConnectProcessor);
server.addConnectionEventProcessor(ConnectionEventType.CLOSE, serverDisConnectProcessor);
server.registerUserProcessor(serverUserProcessor);
client = new RpcClient();
client.addConnectionEventProcessor(ConnectionEventType.CONNECT, clientConnectProcessor);
client.addConnectionEventProcessor(ConnectionEventType.CLOSE, clientDisConnectProcessor);
client.registerUserProcessor(clientUserProcessor);
client.init();
}
@After
public void stop() {
try {
server.stop();
Thread.sleep(100);
} catch (InterruptedException e) {
logger.error("Stop server failed!", e);
}
}
/**
* url: url with different args
* invokeTimes: rpc invoke times
* expectConnTimes: expect connection times for client and server
* expectMaxFirstInvokeTimeDuration: expect first invoke time cost, if assign -1, then will not do check
*
* @param url
* @param invokeTimes
* @param expectConnTimes
* @param expectMaxFirstInvokeTimeDuration
*/
private void doTest(String url, RequestBody.InvokeType type, int invokeTimes,
int expectConnTimes, int expectMaxFirstInvokeTimeDuration) {
try {
RpcAddressParser parser = new RpcAddressParser();
final Url addr = parser.parse(url);
for (int i = 0; i < invokeTimes; i++) {
long start = System.currentTimeMillis();
String ret = (String) doInvoke(type, url);
long end = System.currentTimeMillis();
logger.warn("WITH WARMUP, first invoke cost ->" + (end - start));
if ((end - start) > expectMaxFirstInvokeTimeDuration
&& expectMaxFirstInvokeTimeDuration != -1) {
Assert.fail("Should not reach here, First invoke cost too much time ["
+ (end - start) + "ms], expect limit in ["
+ expectMaxFirstInvokeTimeDuration + "ms]!");
}
if (!type.equals(RequestBody.InvokeType.ONEWAY)) {
Assert.assertEquals(ret, RequestBody.DEFAULT_SERVER_RETURN_STR);
}
}
if (addr.isConnWarmup()) {
Thread.sleep(200);// must wait, to wait event finish
Assert.assertEquals(expectConnTimes, serverConnectProcessor.getConnectTimes());
Assert.assertEquals(expectConnTimes, clientConnectProcessor.getConnectTimes());
client.closeConnection(addr);
Thread.sleep(200);// must wait, to wait event finish
Assert
.assertEquals(expectConnTimes, serverDisConnectProcessor.getDisConnectTimes());
Assert
.assertEquals(expectConnTimes, clientDisConnectProcessor.getDisConnectTimes());
} else {
Thread.sleep(200);// must wait, to wait event finish
Assert.assertTrue(serverConnectProcessor.getConnectTimes() >= expectConnTimes);
Assert.assertTrue(clientConnectProcessor.getConnectTimes() >= expectConnTimes);
client.closeConnection(addr);
Thread.sleep(200);// must wait, to wait event finish
Assert
.assertTrue(serverDisConnectProcessor.getDisConnectTimes() >= expectConnTimes);
Assert
.assertTrue(clientDisConnectProcessor.getDisConnectTimes() >= expectConnTimes);
}
} catch (RemotingException e) {
logger.error("Exception caught in sync!", e);
Assert.fail("Should not reach here!");
} catch (InterruptedException e) {
logger.error("InterruptedException in sync", e);
Assert.fail("Should not reach here!");
}
}
/**
* do invoke
*
* @param type
* @param url
* @return
* @throws RemotingException
* @throws InterruptedException
*/
private Object doInvoke(RequestBody.InvokeType type, String url) throws RemotingException,
InterruptedException {
RequestBody b1 = new RequestBody(1, "hello world");
Object obj = null;
if (type.equals(RequestBody.InvokeType.ONEWAY)) {
client.oneway(url, b1);
} else if (type.equals(RequestBody.InvokeType.SYNC)) {
obj = client.invokeSync(url, b1, 3000);
} else if (type.equals(RequestBody.InvokeType.FUTURE)) {
RpcResponseFuture future = client.invokeWithFuture(url, b1, 3000);
obj = future.get(3000);
} else if (type.equals(RequestBody.InvokeType.CALLBACK)) {
final List<String> rets = new ArrayList<String>(1);
final CountDownLatch latch = new CountDownLatch(1);
client.invokeWithCallback(url, b1, new InvokeCallback() {
Executor executor = Executors.newCachedThreadPool();
@Override
public void onResponse(Object result) {
logger.warn("Result received in callback: " + result);
rets.add((String) result);
latch.countDown();
}
@Override
public void onException(Throwable e) {
logger.error("Process exception in callback.", e);
latch.countDown();
}
@Override
public Executor getExecutor() {
return executor;
}
}, 3000);
try {
latch.await();
} catch (InterruptedException e) {
String errMsg = "InterruptedException caught in callback!";
logger.error(errMsg, e);
Assert.fail(errMsg);
}
if (rets.size() == 0) {
Assert.fail("No result of callback! Maybe exception caught!");
}
obj = rets.get(0);
}
return obj;
}
/**
* reset all times
*/
private void doResetTimes() {
this.clientConnectProcessor.reset();
this.serverConnectProcessor.reset();
this.clientDisConnectProcessor.reset();
this.serverDisConnectProcessor.reset();
}
@Test
public void test_connNum_10_warmup_True_invoke_1times() {
String url = addr
+ "?_CONNECTTIMEOUT=1000&_TIMEOUT=5000&_CONNECTIONNUM=10&_CONNECTIONWARMUP=true";
for (RequestBody.InvokeType type : RequestBody.InvokeType.values()) {
doResetTimes();
doTest(url, type, 1, 10, -1);
}
}
@Test
public void test_connNum_10_warmup_False_invoke_1times() {
String url = addr
+ "?_CONNECTTIMEOUT=1000&_TIMEOUT=5000&_CONNECTIONNUM=10&_CONNECTIONWARMUP=false";
for (RequestBody.InvokeType type : RequestBody.InvokeType.values()) {
doResetTimes();
doTest(url, type, 1, 1, -1);
}
}
@Test
public void test_connNum_1_warmup_True_invoke_1times() {
String url = addr
+ "?_CONNECTTIMEOUT=1000&_TIMEOUT=5000&_CONNECTIONNUM=1&_CONNECTIONWARMUP=true";
for (RequestBody.InvokeType type : RequestBody.InvokeType.values()) {
doResetTimes();
doTest(url, type, 1, 1, -1);
}
}
@Test
public void test_connNum_1_warmup_False_invoke_3times() {
String url = addr
+ "?_CONNECTTIMEOUT=1000&_TIMEOUT=5000&_CONNECTIONNUM=1&_CONNECTIONWARMUP=false";
for (RequestBody.InvokeType type : RequestBody.InvokeType.values()) {
doResetTimes();
doTest(url, type, 1, 1, -1);
}
}
@Test
public void test_connNum_2_warmup_False_invoke_3times() {
String url = addr
+ "?_CONNECTTIMEOUT=1000&_TIMEOUT=5000&_CONNECTIONNUM=2&_CONNECTIONWARMUP=false";
for (RequestBody.InvokeType type : RequestBody.InvokeType.values()) {
doResetTimes();
doTest(url, type, 1, 1, -1);
}
}
@Test
public void test_connNum_2_warmup_True_invoke_3times() {
String url = addr
+ "?_CONNECTTIMEOUT=1000&_TIMEOUT=5000&_CONNECTIONNUM=2&_CONNECTIONWARMUP=true";
for (RequestBody.InvokeType type : RequestBody.InvokeType.values()) {
doResetTimes();
doTest(url, type, 1, 2, -1);
}
}
}