Skip to content

Commit

Permalink
Modify tests (#2083)
Browse files Browse the repository at this point in the history
Changes mostly includes:
- Move utility methods (arrayContains, setContains) from JedisCommandTestBase
- close()/disconnect() Jedis objects
  • Loading branch information
sazzad16 committed Nov 26, 2019
1 parent 6b86fe7 commit d1d3b54
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 43 deletions.
7 changes: 6 additions & 1 deletion src/test/java/redis/clients/jedis/tests/JedisPoolTest.java
Expand Up @@ -149,10 +149,13 @@ public void startWithUrlString() {
j.auth("foobared");
j.select(2);
j.set("foo", "bar");
j.close();

JedisPool pool = new JedisPool("redis://:foobared@localhost:6380/2");
Jedis jedis = pool.getResource();
assertEquals("PONG", jedis.ping());
assertEquals("bar", jedis.get("foo"));
jedis.close();
}

@Test
Expand All @@ -161,6 +164,8 @@ public void startWithUrl() throws URISyntaxException {
j.auth("foobared");
j.select(2);
j.set("foo", "bar");
j.close();

JedisPool pool = new JedisPool(new URI("redis://:foobared@localhost:6380/2"));
Jedis jedis = pool.getResource();
assertEquals("PONG", jedis.ping());
Expand Down Expand Up @@ -398,7 +403,7 @@ public void testCloseConnectionOnMakeObject() {
} catch (Exception e) {
assertEquals(currentClientCount, getClientCount(jedis.clientList()));
}

jedis.close();
}

private int getClientCount(final String clientList) {
Expand Down
Expand Up @@ -11,6 +11,7 @@
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -44,7 +45,13 @@ public void setUp() throws Exception {
sentinelJedis1 = new Jedis(sentinel1);
sentinelJedis2 = new Jedis(sentinel2);
}


@After
public void tearDown() throws Exception {
sentinelJedis1.close();
sentinelJedis2.close();
}

@Test
public void repeatedSentinelPoolInitialization() {

Expand Down
Expand Up @@ -96,6 +96,7 @@ public void sentinelFailover() throws InterruptedException {
assertNotEquals(newMaster, currentMaster);
} finally {
j.close();
j2.close();
}

}
Expand Down
13 changes: 11 additions & 2 deletions src/test/java/redis/clients/jedis/tests/JedisTest.java
Expand Up @@ -24,11 +24,13 @@
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
Expand All @@ -51,6 +53,7 @@ public void connectWithShardInfo() {
shardInfo.setPassword("foobared");
Jedis jedis = new Jedis(shardInfo);
jedis.get("foo");
jedis.close();
}

@Test
Expand Down Expand Up @@ -103,7 +106,6 @@ public void failWhenSendingNullValues() {
@Test(expected = InvalidURIException.class)
public void shouldThrowInvalidURIExceptionForInvalidURI() throws URISyntaxException {
Jedis j = new Jedis(new URI("localhost:6380"));
j.ping();
}

@Test
Expand All @@ -121,9 +123,12 @@ public void startWithUrlString() {
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
Expand All @@ -132,13 +137,15 @@ public void startWithUrl() throws URISyntaxException {
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() throws URISyntaxException {
public void shouldNotUpdateDbIndexIfSelectFails() {
int currentDb = jedis.getDB();
try {
int invalidDb = -1;
Expand All @@ -157,12 +164,14 @@ public void allowUrlWithNoDBAndNoPassword() {
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
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/redis/clients/jedis/tests/PipeliningTest.java
Expand Up @@ -22,6 +22,7 @@

import org.hamcrest.CoreMatchers;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Test;

import redis.clients.jedis.Jedis;
Expand All @@ -34,6 +35,11 @@

public class PipeliningTest extends JedisCommandTestBase {

@After
public void tearDown() throws Exception {
jedis.close();
}

@Test
public void pipeline() {
Pipeline p = jedis.pipelined();
Expand Down Expand Up @@ -651,6 +657,7 @@ public void testCloseable() throws IOException {
// it shouldn't meet any exception
retFuture1.get();
retFuture2.get();
jedis2.close();
}

@Test
Expand Down Expand Up @@ -681,6 +688,7 @@ public void testCloseableWithMulti() throws IOException {
// it shouldn't meet any exception
retFuture1.get();
retFuture2.get();
jedis2.close();
}

private void verifyHasBothValues(String firstKey, String secondKey, String value1, String value2) {
Expand Down
Expand Up @@ -188,10 +188,12 @@ public void startWithUrlString() {
Jedis j = new Jedis("localhost", 6380);
j.auth("foobared");
j.set("foo", "bar");
j.disconnect();

j = new Jedis("localhost", 6379);
j.auth("foobared");
j.set("foo", "bar");
j.disconnect();

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

j = new Jedis("localhost", 6379);
j.auth("foobared");
j.set("foo", "bar");
j.disconnect();

List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6380")));
Expand Down
Expand Up @@ -15,6 +15,7 @@
import static redis.clients.jedis.ScanParams.SCAN_POINTER_START;
import static redis.clients.jedis.ScanParams.SCAN_POINTER_START_BINARY;
import static redis.clients.jedis.params.SetParams.setParams;
import static redis.clients.jedis.tests.utils.AssertUtil.assertCollectionContains;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -232,8 +233,8 @@ public void keys() {

Set<byte[]> bkeys = jedis.keys(bfoostar);
assertEquals(2, bkeys.size());
assertTrue(setContains(bkeys, bfoo));
assertTrue(setContains(bkeys, bfoobar));
assertCollectionContains(bkeys, bfoo);
assertCollectionContains(bkeys, bfoobar);

bkeys = jedis.keys(bbarstar);

Expand Down
Expand Up @@ -37,7 +37,7 @@ public void setUp() throws Exception {

@After
@Override
public void tearDown() {
public void tearDown() throws Exception {
client.close();
super.tearDown();
}
Expand Down
Expand Up @@ -9,6 +9,7 @@
import static redis.clients.jedis.ScanParams.SCAN_POINTER_START_BINARY;
import static redis.clients.jedis.tests.utils.AssertUtil.assertByteArrayListEquals;
import static redis.clients.jedis.tests.utils.AssertUtil.assertByteArraySetEquals;
import static redis.clients.jedis.tests.utils.AssertUtil.assertCollectionContains;

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

assertEquals(2, bvals.size());
assertTrue(arrayContains(bvals, bbar));
assertTrue(arrayContains(bvals, bcar));
assertCollectionContains(bvals, bbar);
assertCollectionContains(bvals, bcar);
}

@Test
Expand Down
@@ -1,9 +1,7 @@
package redis.clients.jedis.tests.commands;

import static org.junit.Assert.assertArrayEquals;

import java.util.List;
import java.util.Set;
import java.util.LinkedHashMap;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
Expand All @@ -30,7 +28,7 @@ public void setUp() throws Exception {
}

@After
public void tearDown() {
public void tearDown() throws Exception {
jedis.disconnect();
}

Expand All @@ -40,28 +38,4 @@ protected Jedis createJedis() {
j.auth("foobared");
return j;
}

protected boolean arrayContains(List<byte[]> array, byte[] expected) {
for (byte[] a : array) {
try {
assertArrayEquals(a, expected);
return true;
} catch (AssertionError e) {

}
}
return false;
}

protected boolean setContains(Set<byte[]> set, byte[] expected) {
for (byte[] a : set) {
try {
assertArrayEquals(a, expected);
return true;
} catch (AssertionError e) {

}
}
return false;
}
}
Expand Up @@ -52,7 +52,7 @@ public void setUp() throws Exception {

@After
@Override
public void tearDown() {
public void tearDown() throws Exception {
dest.close();
destAuth.close();
super.tearDown();
Expand Down
Expand Up @@ -14,7 +14,9 @@ public class SlowlogCommandsTest extends JedisCommandTestBase {

@Test
public void slowlog() {
// do something
final String slowlogTimeParam = "slowlog-log-slower-than";
final String slowlogTimeValue = jedis.configGet(slowlogTimeParam).get(1);

jedis.configSet("slowlog-log-slower-than", "0");
jedis.set("foo", "bar");
jedis.set("foo2", "bar2");
Expand Down Expand Up @@ -48,5 +50,7 @@ public void slowlog() {
assertTrue(len1 > len2);
assertTrue(log1.size() > log2.size());
assertTrue(blog1.size() > blog2.size());

jedis.configSet(slowlogTimeParam, slowlogTimeValue);
}
}
Expand Up @@ -14,6 +14,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import org.junit.After;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -37,6 +38,7 @@ public class TransactionCommandsTest extends JedisCommandTestBase {
Jedis nj;

@Before
@Override
public void setUp() throws Exception {
super.setUp();

Expand All @@ -46,6 +48,13 @@ public void setUp() throws Exception {
nj.flushAll();
}

@After
@Override
public void tearDown() throws Exception {
nj.close();
super.tearDown();
}

@Test
public void multi() {
Transaction trans = jedis.multi();
Expand Down
10 changes: 9 additions & 1 deletion src/test/java/redis/clients/jedis/tests/utils/AssertUtil.java
Expand Up @@ -2,7 +2,6 @@

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.Collection;
Expand All @@ -15,6 +14,15 @@

public class AssertUtil {

public static boolean assertCollectionContains(Collection<byte[]> array, byte[] expected) {
for (byte[] bytes : array) {
if (Arrays.equals(bytes, expected)) {
return true;
}
}
throw new ComparisonFailure("element is missing", Arrays.toString(expected), array.toString());
}

public static void assertByteArrayListEquals(List<byte[]> expected, List<byte[]> actual) {
assertEquals(expected.size(), actual.size());
for (int n = 0; n < expected.size(); n++) {
Expand Down
@@ -1,7 +1,5 @@
package redis.clients.jedis.tests.utils;

import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
Expand Down

0 comments on commit d1d3b54

Please sign in to comment.