-
Notifications
You must be signed in to change notification settings - Fork 0
/
BIOTest.java
61 lines (54 loc) · 1.5 KB
/
BIOTest.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
package com.lawsssscat.learn;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.net.Socket;
import org.junit.Test;
public class BIOTest {
private Integer count = 0;
@Test
public void testSingleLine() throws IOException, InterruptedException {
int port = 9999;
String msg = "hello \nworld! 服务器端,你好呀!";
new BIOServer().start(port, new Callback() {
@Override
public void run(Object... args) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String receiveMsg = (String) args[0];
Socket socket = (Socket) args[1];
synchronized (count) {
count++;
System.out.println(String.format("%s: %s [%s]", socket, receiveMsg, count));
}
assertTrue(receiveMsg.indexOf(msg) >= 0);
}
});
Integer num = 16;
BIOClient client = new BIOClient();
for (int i = 0; i < num; i++) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("客户端,发送信息...");
client.submitSingleLine("127.0.0.1", port, msg + Thread.currentThread().getName());
System.out.println("客户端,发送信息完成!");
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.setName("thread-" + i);
thread.start();
}
Thread.sleep(1000);
synchronized (count) {
assertEquals(num, count);
}
}
}