Skip to content

Commit 5b80f41

Browse files
committed
added Testthreadsafety.java
1 parent 5c3555d commit 5b80f41

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package org.msgpack;
2+
3+
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.Map.Entry;
10+
import java.util.concurrent.Callable;
11+
import java.util.concurrent.ExecutionException;
12+
import java.util.concurrent.ExecutorService;
13+
import java.util.concurrent.Executors;
14+
import java.util.concurrent.Future;
15+
import java.util.concurrent.TimeUnit;
16+
import java.util.concurrent.TimeoutException;
17+
18+
import org.junit.Before;
19+
import org.junit.Test;
20+
import org.msgpack.MessagePack;
21+
import org.msgpack.packer.BufferPacker;
22+
import org.msgpack.type.ArrayValue;
23+
import org.msgpack.type.MapValue;
24+
import org.msgpack.type.Value;
25+
import org.msgpack.unpacker.BufferUnpacker;
26+
27+
public class TestThreadSafety {
28+
private List<String> list = createList(1000);
29+
private Map<String, String> map = createMap(1000);
30+
private static final String EXAMPLE_STRING;
31+
static {
32+
StringBuilder buf = new StringBuilder();
33+
for (int i = 0; i < 10; i++) {
34+
buf.append("0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999");
35+
}
36+
EXAMPLE_STRING = buf.toString();
37+
}
38+
39+
private List<String> createList(int n) {
40+
List<String> src = new ArrayList<String>();
41+
for (int i = 0; i < n; i++) {
42+
src.add(EXAMPLE_STRING);
43+
}
44+
return src;
45+
}
46+
47+
private Map<String, String> createMap(int n) {
48+
Map<String, String> src = new HashMap<String, String>();
49+
for (int i = 0; i < n; i++) {
50+
src.put(String.valueOf(i), EXAMPLE_STRING);
51+
}
52+
return src;
53+
}
54+
55+
private void testMsgpackDynamicString(int n) throws IOException {
56+
MessagePack msgpack = new MessagePack();
57+
BufferPacker packer = msgpack.createBufferPacker();
58+
for (int i = 0; i < n; i++) {
59+
packer.write(EXAMPLE_STRING);
60+
}
61+
62+
byte[] raw = packer.toByteArray();
63+
BufferUnpacker unpacker = msgpack.createBufferUnpacker(raw);
64+
65+
for (int i = 0; i < n; i++) {
66+
String dst = unpacker.read(String.class);
67+
if (!dst.equals(EXAMPLE_STRING)) {
68+
throw new AssertionError();
69+
}
70+
}
71+
}
72+
73+
private void testMsgpackDynamicArray() throws IOException {
74+
List<String> src = list;
75+
76+
MessagePack msgpack = new MessagePack();
77+
byte[] raw;
78+
raw = msgpack.write(src);
79+
80+
List<String> dst = new LinkedList<String>();
81+
ArrayValue arrayValue = msgpack.read(raw).asArrayValue();
82+
for (Value v : arrayValue) {
83+
dst.add(v.asRawValue().getString());
84+
85+
if (!v.asRawValue().getString().equals(EXAMPLE_STRING)) {
86+
throw new AssertionError();
87+
}
88+
}
89+
}
90+
91+
private void testMsgpackDynamicMap() throws IOException {
92+
Map<String, String> src = map;
93+
94+
MessagePack msgpack = new MessagePack();
95+
byte[] raw;
96+
raw = msgpack.write(src);
97+
98+
MapValue mv = msgpack.read(raw).asMapValue();
99+
for (Entry<Value, Value> kv : mv.entrySet()) {
100+
if (!kv.getValue().asRawValue().getString().equals(EXAMPLE_STRING)) {
101+
throw new AssertionError();
102+
}
103+
}
104+
}
105+
106+
static class TestRunner implements Callable<Void> {
107+
private final TestThreadSafety main;
108+
109+
public TestRunner(TestThreadSafety main) {
110+
this.main = main;
111+
}
112+
113+
@Override
114+
public Void call() throws Exception {
115+
try {
116+
main.testMsgpackDynamicString(1000);
117+
main.testMsgpackDynamicArray();
118+
main.testMsgpackDynamicMap();
119+
} catch (IOException e) {
120+
throw new RuntimeException(e);
121+
}
122+
return null;
123+
}
124+
}
125+
126+
@Test
127+
public void testWithBulkData() throws InterruptedException, ExecutionException, TimeoutException {
128+
final TestThreadSafety main = new TestThreadSafety();
129+
List<Future<Void>> futures = new LinkedList<Future<Void>>();
130+
ExecutorService executorService = Executors.newCachedThreadPool();
131+
for (int i = 0; i < 20; i++) {
132+
futures.add(executorService.submit(new TestRunner(main)));
133+
}
134+
135+
for (Future<Void> future : futures) {
136+
future.get(30, TimeUnit.SECONDS);
137+
}
138+
}
139+
}

0 commit comments

Comments
 (0)