forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNioTest.java
More file actions
213 lines (175 loc) · 6.24 KB
/
NioTest.java
File metadata and controls
213 lines (175 loc) · 6.24 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
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
// 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
// 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.cloud.utils.testcase;
import java.nio.channels.ClosedChannelException;
import java.util.Random;
import junit.framework.TestCase;
import org.apache.log4j.Logger;
import org.junit.Assert;
import com.cloud.utils.nio.HandlerFactory;
import com.cloud.utils.nio.Link;
import com.cloud.utils.nio.NioClient;
import com.cloud.utils.nio.NioServer;
import com.cloud.utils.nio.Task;
import com.cloud.utils.nio.Task.Type;
/**
*
*
*
*
*/
public class NioTest extends TestCase {
private static final Logger s_logger = Logger.getLogger(NioTest.class);
private NioServer _server;
private NioClient _client;
private Link _clientLink;
private int _testCount;
private int _completedCount;
private boolean isTestsDone() {
boolean result;
synchronized (this) {
result = (_testCount == _completedCount);
}
return result;
}
private void getOneMoreTest() {
synchronized (this) {
_testCount++;
}
}
private void oneMoreTestDone() {
synchronized (this) {
_completedCount++;
}
}
@Override
public void setUp() {
s_logger.info("Test");
_testCount = 0;
_completedCount = 0;
_server = new NioServer("NioTestServer", 7777, 5, new NioTestServer());
_server.start();
_client = new NioClient("NioTestServer", "127.0.0.1", 7777, 5, new NioTestClient());
_client.start();
while (_clientLink == null) {
try {
s_logger.debug("Link is not up! Waiting ...");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void tearDown() {
while (!isTestsDone()) {
try {
s_logger.debug(this._completedCount + "/" + this._testCount + " tests done. Waiting for completion");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
stopClient();
stopServer();
}
protected void stopClient() {
_client.stop();
s_logger.info("Client stopped.");
}
protected void stopServer() {
_server.stop();
s_logger.info("Server stopped.");
}
protected void setClientLink(Link link) {
_clientLink = link;
}
Random randomGenerator = new Random();
byte[] _testBytes;
public void testConnection() {
_testBytes = new byte[1000000];
randomGenerator.nextBytes(_testBytes);
try {
getOneMoreTest();
_clientLink.send(_testBytes);
s_logger.info("Client: Data sent");
getOneMoreTest();
_clientLink.send(_testBytes);
s_logger.info("Client: Data sent");
} catch (ClosedChannelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doServerProcess(byte[] data) {
oneMoreTestDone();
Assert.assertArrayEquals(_testBytes, data);
s_logger.info("Verify done.");
}
public class NioTestClient implements HandlerFactory {
@Override
public Task create(Type type, Link link, byte[] data) {
return new NioTestClientHandler(type, link, data);
}
public class NioTestClientHandler extends Task {
public NioTestClientHandler(Type type, Link link, byte[] data) {
super(type, link, data);
}
@Override
public void doTask(final Task task) {
if (task.getType() == Task.Type.CONNECT) {
s_logger.info("Client: Received CONNECT task");
setClientLink(task.getLink());
} else if (task.getType() == Task.Type.DATA) {
s_logger.info("Client: Received DATA task");
} else if (task.getType() == Task.Type.DISCONNECT) {
s_logger.info("Client: Received DISCONNECT task");
stopClient();
} else if (task.getType() == Task.Type.OTHER) {
s_logger.info("Client: Received OTHER task");
}
}
}
}
public class NioTestServer implements HandlerFactory {
@Override
public Task create(Type type, Link link, byte[] data) {
return new NioTestServerHandler(type, link, data);
}
public class NioTestServerHandler extends Task {
public NioTestServerHandler(Type type, Link link, byte[] data) {
super(type, link, data);
}
@Override
public void doTask(final Task task) {
if (task.getType() == Task.Type.CONNECT) {
s_logger.info("Server: Received CONNECT task");
} else if (task.getType() == Task.Type.DATA) {
s_logger.info("Server: Received DATA task");
doServerProcess(task.getData());
} else if (task.getType() == Task.Type.DISCONNECT) {
s_logger.info("Server: Received DISCONNECT task");
stopServer();
} else if (task.getType() == Task.Type.OTHER) {
s_logger.info("Server: Received OTHER task");
}
}
}
}
}