-
Notifications
You must be signed in to change notification settings - Fork 870
Expand file tree
/
Copy pathRpcCommandHandlerTest.java
More file actions
144 lines (117 loc) · 4.45 KB
/
RpcCommandHandlerTest.java
File metadata and controls
144 lines (117 loc) · 4.45 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* 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.protocol;
import com.alipay.remoting.AsyncContext;
import com.alipay.remoting.BizContext;
import com.alipay.remoting.InvokeContext;
import com.alipay.remoting.LifeCycleException;
import com.alipay.remoting.RemotingContext;
import com.alipay.remoting.rpc.RpcCommandFactory;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
/**
* @author Even
* @date 2024/4/29 11:20
*/
public class RpcCommandHandlerTest {
private static RemotingContext remotingContext = null;
private static final List<RemotingContext> remotingContextList = new ArrayList<>();
private static final CountDownLatch countDownLatch = new CountDownLatch(2);
@BeforeClass
public static void beforeClass() {
ConcurrentHashMap<String, UserProcessor<?>> userProcessors = new ConcurrentHashMap<>();
userProcessors.put("testClass", new MockUserProcessors());
remotingContext = new RemotingContext(null, new InvokeContext(),true, userProcessors);
}
@Test
public void testHandleCommand() throws Exception {
List<RpcRequestCommand> msg = new ArrayList<>();
RpcRequestCommand rpcRequestCommand = new RpcRequestCommand();
rpcRequestCommand.setTimeout(1000);
rpcRequestCommand.setRequestClass("testClass");
RpcRequestCommand rpcRequestCommand2 = new RpcRequestCommand();
rpcRequestCommand2.setTimeout(2000);
rpcRequestCommand2.setRequestClass("testClass");
msg.add(rpcRequestCommand);
msg.add(rpcRequestCommand2);
RpcCommandHandler rpcCommandHandler = new RpcCommandHandler(new RpcCommandFactory());
rpcCommandHandler.handleCommand(remotingContext, msg);
countDownLatch.await();
Assert.assertEquals(2, remotingContextList.size());
Assert.assertTrue(remotingContextList.get(0).getTimeout() != remotingContextList.get(1).getTimeout());
}
static class MockUserProcessors implements UserProcessor {
@Override
public void startup() throws LifeCycleException {
}
@Override
public void shutdown() throws LifeCycleException {
}
@Override
public boolean isStarted() {
return false;
}
@Override
public BizContext preHandleRequest(RemotingContext remotingCtx, Object request) {
Assert.assertNotSame(remotingCtx, remotingContext);
remotingContextList.add(remotingCtx);
countDownLatch.countDown();
return null;
}
@Override
public void handleRequest(BizContext bizCtx, AsyncContext asyncCtx, Object request) {
}
@Override
public Object handleRequest(BizContext bizCtx, Object request) throws Exception {
return null;
}
@Override
public String interest() {
return null;
}
@Override
public Executor getExecutor() {
return null;
}
@Override
public ClassLoader getBizClassLoader() {
return null;
}
@Override
public boolean processInIOThread() {
return false;
}
@Override
public boolean timeoutDiscard() {
return false;
}
@Override
public void setExecutorSelector(ExecutorSelector executorSelector) {
}
@Override
public ExecutorSelector getExecutorSelector() {
return null;
}
}
}