forked from redis/jedis
/
JedisTest.java
191 lines (164 loc) · 5.26 KB
/
JedisTest.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
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
package redis.clients.jedis.tests;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import redis.clients.jedis.BinaryJedis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.exceptions.InvalidURIException;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.jedis.tests.commands.JedisCommandTestBase;
import redis.clients.jedis.util.SafeEncoder;
public class JedisTest extends JedisCommandTestBase {
@Test
public void useWithoutConnecting() {
Jedis jedis = new Jedis("localhost");
jedis.auth("foobared");
jedis.dbSize();
jedis.close();
}
@Test
public void checkBinaryData() {
byte[] bigdata = new byte[1777];
for (int b = 0; b < bigdata.length; b++) {
bigdata[b] = (byte) ((byte) b % 255);
}
Map<String, String> hash = new HashMap<String, String>();
hash.put("data", SafeEncoder.encode(bigdata));
String status = jedis.hmset("foo", hash);
assertEquals("OK", status);
assertEquals(hash, jedis.hgetAll("foo"));
}
@Test
public void connectWithShardInfo() {
JedisShardInfo shardInfo = new JedisShardInfo("localhost", Protocol.DEFAULT_PORT);
shardInfo.setPassword("foobared");
Jedis jedis = new Jedis(shardInfo);
jedis.get("foo");
jedis.close();
}
@Test
public void timeoutConnection() throws Exception {
Jedis jedis = new Jedis("localhost", 6379, 15000);
jedis.auth("foobared");
String timeout = jedis.configGet("timeout").get(1);
jedis.configSet("timeout", "1");
Thread.sleep(2000);
try {
jedis.hmget("foobar", "foo");
fail("Operation should throw JedisConnectionException");
} catch(JedisConnectionException jce) {
// expected
}
jedis.close();
// reset config
jedis = new Jedis("localhost", 6379);
jedis.auth("foobared");
jedis.configSet("timeout", timeout);
jedis.close();
}
@Test
public void timeoutConnectionWithURI() throws Exception {
Jedis jedis = new Jedis(new URI("redis://:foobared@localhost:6380/2"), 15000);
String timeout = jedis.configGet("timeout").get(1);
jedis.configSet("timeout", "1");
Thread.sleep(2000);
try {
jedis.hmget("foobar", "foo");
fail("Operation should throw JedisConnectionException");
} catch(JedisConnectionException jce) {
// expected
}
jedis.close();
// reset config
jedis = new Jedis(new URI("redis://:foobared@localhost:6380/2"));
jedis.configSet("timeout", timeout);
jedis.close();
}
@Test(expected = JedisDataException.class)
public void failWhenSendingNullValues() {
jedis.set("foo", null);
}
@Test(expected = InvalidURIException.class)
public void shouldThrowInvalidURIExceptionForInvalidURI() throws URISyntaxException {
Jedis j = new Jedis(new URI("localhost:6380"));
}
@Test
public void shouldReconnectToSameDB() throws IOException {
jedis.select(1);
jedis.set("foo", "bar");
jedis.getClient().getSocket().shutdownInput();
jedis.getClient().getSocket().shutdownOutput();
assertEquals("bar", jedis.get("foo"));
}
@Test
public void startWithUrlString() {
Jedis j = new Jedis("localhost", 6380);
j.auth("foobared");
j.select(2);
j.set("foo", "bar");
j.close();
Jedis jedis = new Jedis("redis://:foobared@localhost:6380/2");
assertEquals("PONG", jedis.ping());
assertEquals("bar", jedis.get("foo"));
jedis.close();
}
@Test
public void startWithUrl() throws URISyntaxException {
Jedis j = new Jedis("localhost", 6380);
j.auth("foobared");
j.select(2);
j.set("foo", "bar");
Jedis jedis = new Jedis(new URI("redis://:foobared@localhost:6380/2"));
assertEquals("PONG", jedis.ping());
assertEquals("bar", jedis.get("foo"));
jedis.close();
}
@Test
public void shouldNotUpdateDbIndexIfSelectFails() {
int currentDb = jedis.getDB();
try {
int invalidDb = -1;
jedis.select(invalidDb);
fail("Should throw an exception if tried to select invalid db");
} catch (JedisException e) {
assertEquals(currentDb, jedis.getDB());
}
}
@Test
public void allowUrlWithNoDBAndNoPassword() {
Jedis jedis = new Jedis("redis://localhost:6380");
jedis.auth("foobared");
assertEquals("localhost", jedis.getClient().getHost());
assertEquals(6380, jedis.getClient().getPort());
assertEquals(0, jedis.getDB());
jedis.close();
jedis = new Jedis("redis://localhost:6380/");
jedis.auth("foobared");
assertEquals("localhost", jedis.getClient().getHost());
assertEquals(6380, jedis.getClient().getPort());
assertEquals(0, jedis.getDB());
jedis.close();
}
@Test
public void checkCloseable() {
jedis.close();
BinaryJedis bj = new BinaryJedis("localhost");
bj.connect();
bj.close();
}
@Test
public void checkDisconnectOnQuit() {
jedis.quit();
assertFalse(jedis.getClient().isConnected());
}
}