public
Description: A simple, asynchronous, single-threaded memcached client written in java.
Homepage: http://code.google.com/p/spymemcached/
Clone URL: git://github.com/dustin/java-memcached-client.git
java-memcached-client / src / test / java / net / spy / memcached / QueueOverflowTest.java
100644 102 lines (93 sloc) 2.609 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
package net.spy.memcached;
 
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
 
/**
* Test queue overflow.
*/
public class QueueOverflowTest extends ClientBaseCase {
 
@Override
protected void initClient() throws Exception {
initClient(new DefaultConnectionFactory(5, 1024) {
@Override
public MemcachedConnection createConnection(
List<InetSocketAddress> addrs) throws IOException {
MemcachedConnection rv = super.createConnection(addrs);
rv.setGetOptimization(false);
return rv;
}
@Override
public long getOperationTimeout() {
return 1000;
}
});
}
 
private void runOverflowTest(byte b[]) throws Exception {
Collection<Future<Boolean>> c=new ArrayList<Future<Boolean>>();
try {
for(int i=0; i<1000; i++) {
c.add(client.set("k" + i, 0, b));
}
fail("Didn't catch an illegal state exception");
} catch(IllegalStateException e) {
// expected
}
try {
Thread.sleep(50);
for(Future<Boolean> f : c) {
f.get(1, TimeUnit.SECONDS);
}
} catch(TimeoutException e) {
// OK, at least we got one back.
} catch(ExecutionException e) {
// OK, at least we got one back.
}
Thread.sleep(500);
assertTrue(client.set("kx", 0, "woo").get(1, TimeUnit.SECONDS));
}
 
public void testOverflowingInputQueue() throws Exception {
runOverflowTest(new byte[]{1});
}
 
public void testOverflowingWriteQueue() throws Exception {
byte[] b=new byte[8192];
Random r=new Random();
r.nextBytes(b);
runOverflowTest(b);
}
 
public void testOverflowingReadQueue() throws Exception {
byte[] b=new byte[8192];
Random r=new Random();
r.nextBytes(b);
client.set("x", 0, b);
 
Collection<Future<Object>> c=new ArrayList<Future<Object>>();
try {
for(int i=0; i<1000; i++) {
c.add(client.asyncGet("x"));
}
fail("Didn't catch an illegal state exception");
} catch(IllegalStateException e) {
// expected
}
try {
Thread.sleep(50);
for(Future<Object> f : c) {
assertTrue(Arrays.equals(b,
(byte[])f.get(5, TimeUnit.SECONDS)));
}
} catch(TimeoutException e) {
// OK, just want to make sure the client doesn't crash
} catch(ExecutionException e) {
// OK, at least we got one back.
}
Thread.sleep(500);
assertTrue(client.set("kx", 0, "woo").get(1, TimeUnit.SECONDS));
}
}