/
ClientCommandsTest.java
165 lines (134 loc) · 4.39 KB
/
ClientCommandsTest.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
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
package redis.clients.jedis.tests.commands;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static redis.clients.jedis.params.ClientKillParams.Type;
import static redis.clients.jedis.params.ClientKillParams.SkipMe;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.params.ClientKillParams;
public class ClientCommandsTest extends JedisCommandTestBase {
private final String clientName = "fancy_jedis_name";
private final Pattern pattern = Pattern.compile("\\bname=" + clientName + "\\b");
private Jedis client;
@Before
@Override
public void setUp() throws Exception {
super.setUp();
client = new Jedis(hnp.getHost(), hnp.getPort(), 500);
client.auth("foobared");
client.clientSetname(clientName);
}
@After
@Override
public void tearDown() throws Exception {
client.close();
super.tearDown();
}
@Test
public void nameString() {
String name = "string";
client.clientSetname(name);
assertEquals(name, client.clientGetname());
}
@Test
public void nameBinary() {
byte[] name = "binary".getBytes();
client.clientSetname(name);
assertArrayEquals(name, client.clientGetnameBinary());
}
@Test
public void killIdString() {
String info = findInClientList();
Matcher matcher = Pattern.compile("\\bid=(\\d+)\\b").matcher(info);
matcher.find();
String id = matcher.group(1);
long clients = jedis.clientKill(new ClientKillParams().id(id));
assertEquals(1, clients);
assertDisconnected(client);
}
@Test
public void killIdBinary() {
String info = findInClientList();
Matcher matcher = Pattern.compile("\\bid=(\\d+)\\b").matcher(info);
matcher.find();
byte[] id = matcher.group(1).getBytes();
long clients = jedis.clientKill(new ClientKillParams().id(id));
assertEquals(1, clients);
assertDisconnected(client);
}
@Test
public void killTypeNormal() {
long clients = jedis.clientKill(new ClientKillParams().type(Type.NORMAL));
assertTrue(clients > 0);
assertDisconnected(client);
}
@Test
public void killSkipmeNo() {
jedis.clientKill(new ClientKillParams().type(Type.NORMAL).skipMe(SkipMe.NO));
assertDisconnected(client);
assertDisconnected(jedis);
}
@Test
public void killSkipmeYesNo() {
jedis.clientKill(new ClientKillParams().type(Type.NORMAL).skipMe(SkipMe.YES));
assertDisconnected(client);
long clients = jedis.clientKill(new ClientKillParams().type(Type.NORMAL).skipMe(SkipMe.NO));
assertEquals(1, clients);
assertDisconnected(jedis);
}
@Test
public void killAddrString() {
String info = findInClientList();
Matcher matcher = Pattern.compile("\\baddr=(\\S+)\\b").matcher(info);
matcher.find();
String addr = matcher.group(1);
long clients = jedis.clientKill(new ClientKillParams().addr(addr));
assertEquals(1, clients);
assertDisconnected(client);
}
@Test
public void killAddrBinary() {
String info = findInClientList();
Matcher matcher = Pattern.compile("\\baddr=(\\S+)\\b").matcher(info);
matcher.find();
String addr = matcher.group(1);
long clients = jedis.clientKill(new ClientKillParams().addr(addr));
assertEquals(1, clients);
assertDisconnected(client);
}
@Test
public void killAddrIpPort() {
String info = findInClientList();
Matcher matcher = Pattern.compile("\\baddr=(\\S+)\\b").matcher(info);
matcher.find();
String addr = matcher.group(1);
String[] hp = HostAndPort.extractParts(addr);
long clients = jedis.clientKill(new ClientKillParams().addr(hp[0], Integer.parseInt(hp[1])));
assertEquals(1, clients);
assertDisconnected(client);
}
private void assertDisconnected(Jedis j) {
try {
j.ping();
fail("Jedis connection should be disconnected");
} catch(JedisConnectionException jce) {
// should be here
}
}
private String findInClientList() {
for (String clientInfo : jedis.clientList().split("\n")) {
if (pattern.matcher(clientInfo).find()) {
return clientInfo;
}
}
return null;
}
}