Skip to content

Commit d1d3b54

Browse files
authored
Modify tests (#2083)
Changes mostly includes: - Move utility methods (arrayContains, setContains) from JedisCommandTestBase - close()/disconnect() Jedis objects
1 parent 6b86fe7 commit d1d3b54

15 files changed

+72
-43
lines changed

src/test/java/redis/clients/jedis/tests/JedisPoolTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,13 @@ public void startWithUrlString() {
149149
j.auth("foobared");
150150
j.select(2);
151151
j.set("foo", "bar");
152+
j.close();
153+
152154
JedisPool pool = new JedisPool("redis://:foobared@localhost:6380/2");
153155
Jedis jedis = pool.getResource();
154156
assertEquals("PONG", jedis.ping());
155157
assertEquals("bar", jedis.get("foo"));
158+
jedis.close();
156159
}
157160

158161
@Test
@@ -161,6 +164,8 @@ public void startWithUrl() throws URISyntaxException {
161164
j.auth("foobared");
162165
j.select(2);
163166
j.set("foo", "bar");
167+
j.close();
168+
164169
JedisPool pool = new JedisPool(new URI("redis://:foobared@localhost:6380/2"));
165170
Jedis jedis = pool.getResource();
166171
assertEquals("PONG", jedis.ping());
@@ -398,7 +403,7 @@ public void testCloseConnectionOnMakeObject() {
398403
} catch (Exception e) {
399404
assertEquals(currentClientCount, getClientCount(jedis.clientList()));
400405
}
401-
406+
jedis.close();
402407
}
403408

404409
private int getClientCount(final String clientList) {

src/test/java/redis/clients/jedis/tests/JedisSentinelPoolTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.apache.commons.pool2.PooledObjectFactory;
1212
import org.apache.commons.pool2.impl.DefaultPooledObject;
1313
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
14+
import org.junit.After;
1415
import org.junit.Before;
1516
import org.junit.Test;
1617

@@ -44,7 +45,13 @@ public void setUp() throws Exception {
4445
sentinelJedis1 = new Jedis(sentinel1);
4546
sentinelJedis2 = new Jedis(sentinel2);
4647
}
47-
48+
49+
@After
50+
public void tearDown() throws Exception {
51+
sentinelJedis1.close();
52+
sentinelJedis2.close();
53+
}
54+
4855
@Test
4956
public void repeatedSentinelPoolInitialization() {
5057

src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public void sentinelFailover() throws InterruptedException {
9696
assertNotEquals(newMaster, currentMaster);
9797
} finally {
9898
j.close();
99+
j2.close();
99100
}
100101

101102
}

src/test/java/redis/clients/jedis/tests/JedisTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
import redis.clients.jedis.util.SafeEncoder;
2525

2626
public class JedisTest extends JedisCommandTestBase {
27+
2728
@Test
2829
public void useWithoutConnecting() {
2930
Jedis jedis = new Jedis("localhost");
3031
jedis.auth("foobared");
3132
jedis.dbSize();
33+
jedis.close();
3234
}
3335

3436
@Test
@@ -51,6 +53,7 @@ public void connectWithShardInfo() {
5153
shardInfo.setPassword("foobared");
5254
Jedis jedis = new Jedis(shardInfo);
5355
jedis.get("foo");
56+
jedis.close();
5457
}
5558

5659
@Test
@@ -103,7 +106,6 @@ public void failWhenSendingNullValues() {
103106
@Test(expected = InvalidURIException.class)
104107
public void shouldThrowInvalidURIExceptionForInvalidURI() throws URISyntaxException {
105108
Jedis j = new Jedis(new URI("localhost:6380"));
106-
j.ping();
107109
}
108110

109111
@Test
@@ -121,9 +123,12 @@ public void startWithUrlString() {
121123
j.auth("foobared");
122124
j.select(2);
123125
j.set("foo", "bar");
126+
j.close();
127+
124128
Jedis jedis = new Jedis("redis://:foobared@localhost:6380/2");
125129
assertEquals("PONG", jedis.ping());
126130
assertEquals("bar", jedis.get("foo"));
131+
jedis.close();
127132
}
128133

129134
@Test
@@ -132,13 +137,15 @@ public void startWithUrl() throws URISyntaxException {
132137
j.auth("foobared");
133138
j.select(2);
134139
j.set("foo", "bar");
140+
135141
Jedis jedis = new Jedis(new URI("redis://:foobared@localhost:6380/2"));
136142
assertEquals("PONG", jedis.ping());
137143
assertEquals("bar", jedis.get("foo"));
144+
jedis.close();
138145
}
139146

140147
@Test
141-
public void shouldNotUpdateDbIndexIfSelectFails() throws URISyntaxException {
148+
public void shouldNotUpdateDbIndexIfSelectFails() {
142149
int currentDb = jedis.getDB();
143150
try {
144151
int invalidDb = -1;
@@ -157,12 +164,14 @@ public void allowUrlWithNoDBAndNoPassword() {
157164
assertEquals("localhost", jedis.getClient().getHost());
158165
assertEquals(6380, jedis.getClient().getPort());
159166
assertEquals(0, jedis.getDB());
167+
jedis.close();
160168

161169
jedis = new Jedis("redis://localhost:6380/");
162170
jedis.auth("foobared");
163171
assertEquals("localhost", jedis.getClient().getHost());
164172
assertEquals(6380, jedis.getClient().getPort());
165173
assertEquals(0, jedis.getDB());
174+
jedis.close();
166175
}
167176

168177
@Test

src/test/java/redis/clients/jedis/tests/PipeliningTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.hamcrest.CoreMatchers;
2424
import org.hamcrest.Matcher;
25+
import org.junit.After;
2526
import org.junit.Test;
2627

2728
import redis.clients.jedis.Jedis;
@@ -34,6 +35,11 @@
3435

3536
public class PipeliningTest extends JedisCommandTestBase {
3637

38+
@After
39+
public void tearDown() throws Exception {
40+
jedis.close();
41+
}
42+
3743
@Test
3844
public void pipeline() {
3945
Pipeline p = jedis.pipelined();
@@ -651,6 +657,7 @@ public void testCloseable() throws IOException {
651657
// it shouldn't meet any exception
652658
retFuture1.get();
653659
retFuture2.get();
660+
jedis2.close();
654661
}
655662

656663
@Test
@@ -681,6 +688,7 @@ public void testCloseableWithMulti() throws IOException {
681688
// it shouldn't meet any exception
682689
retFuture1.get();
683690
retFuture2.get();
691+
jedis2.close();
684692
}
685693

686694
private void verifyHasBothValues(String firstKey, String secondKey, String value1, String value2) {

src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,12 @@ public void startWithUrlString() {
188188
Jedis j = new Jedis("localhost", 6380);
189189
j.auth("foobared");
190190
j.set("foo", "bar");
191+
j.disconnect();
191192

192193
j = new Jedis("localhost", 6379);
193194
j.auth("foobared");
194195
j.set("foo", "bar");
196+
j.disconnect();
195197

196198
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
197199
shards.add(new JedisShardInfo("redis://:foobared@localhost:6380"));
@@ -216,10 +218,12 @@ public void startWithUrl() throws URISyntaxException {
216218
Jedis j = new Jedis("localhost", 6380);
217219
j.auth("foobared");
218220
j.set("foo", "bar");
221+
j.disconnect();
219222

220223
j = new Jedis("localhost", 6379);
221224
j.auth("foobared");
222225
j.set("foo", "bar");
226+
j.disconnect();
223227

224228
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
225229
shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6380")));

src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import static redis.clients.jedis.ScanParams.SCAN_POINTER_START;
1616
import static redis.clients.jedis.ScanParams.SCAN_POINTER_START_BINARY;
1717
import static redis.clients.jedis.params.SetParams.setParams;
18+
import static redis.clients.jedis.tests.utils.AssertUtil.assertCollectionContains;
1819

1920
import java.util.ArrayList;
2021
import java.util.Arrays;
@@ -232,8 +233,8 @@ public void keys() {
232233

233234
Set<byte[]> bkeys = jedis.keys(bfoostar);
234235
assertEquals(2, bkeys.size());
235-
assertTrue(setContains(bkeys, bfoo));
236-
assertTrue(setContains(bkeys, bfoobar));
236+
assertCollectionContains(bkeys, bfoo);
237+
assertCollectionContains(bkeys, bfoobar);
237238

238239
bkeys = jedis.keys(bbarstar);
239240

src/test/java/redis/clients/jedis/tests/commands/ClientCommandsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void setUp() throws Exception {
3737

3838
@After
3939
@Override
40-
public void tearDown() {
40+
public void tearDown() throws Exception {
4141
client.close();
4242
super.tearDown();
4343
}

src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static redis.clients.jedis.ScanParams.SCAN_POINTER_START_BINARY;
1010
import static redis.clients.jedis.tests.utils.AssertUtil.assertByteArrayListEquals;
1111
import static redis.clients.jedis.tests.utils.AssertUtil.assertByteArraySetEquals;
12+
import static redis.clients.jedis.tests.utils.AssertUtil.assertCollectionContains;
1213

1314
import java.util.ArrayList;
1415
import java.util.HashMap;
@@ -318,8 +319,8 @@ public void hvals() {
318319
List<byte[]> bvals = jedis.hvals(bfoo);
319320

320321
assertEquals(2, bvals.size());
321-
assertTrue(arrayContains(bvals, bbar));
322-
assertTrue(arrayContains(bvals, bcar));
322+
assertCollectionContains(bvals, bbar);
323+
assertCollectionContains(bvals, bcar);
323324
}
324325

325326
@Test
Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package redis.clients.jedis.tests.commands;
22

3-
import static org.junit.Assert.assertArrayEquals;
4-
5-
import java.util.List;
6-
import java.util.Set;
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
75

86
import org.junit.After;
97
import org.junit.Before;
@@ -30,7 +28,7 @@ public void setUp() throws Exception {
3028
}
3129

3230
@After
33-
public void tearDown() {
31+
public void tearDown() throws Exception {
3432
jedis.disconnect();
3533
}
3634

@@ -40,28 +38,4 @@ protected Jedis createJedis() {
4038
j.auth("foobared");
4139
return j;
4240
}
43-
44-
protected boolean arrayContains(List<byte[]> array, byte[] expected) {
45-
for (byte[] a : array) {
46-
try {
47-
assertArrayEquals(a, expected);
48-
return true;
49-
} catch (AssertionError e) {
50-
51-
}
52-
}
53-
return false;
54-
}
55-
56-
protected boolean setContains(Set<byte[]> set, byte[] expected) {
57-
for (byte[] a : set) {
58-
try {
59-
assertArrayEquals(a, expected);
60-
return true;
61-
} catch (AssertionError e) {
62-
63-
}
64-
}
65-
return false;
66-
}
6741
}

0 commit comments

Comments
 (0)