Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/clients/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<version>7.2.1</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import org.apache.ignite.configuration.ConnectorConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.ClientSetInfoConfig;
import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;

/**
* Common for all Redis tests.
Expand All @@ -38,8 +40,8 @@ public class RedisCommonAbstractTest extends GridCommonAbstractTest {
/** Port. */
protected static final int PORT = 6379;

/** Pool. */
protected static JedisPool pool;
/** Redis client factory. */
protected static RedisClientFactory redisClientFactory;

/** Default Redis cache name. */
private static final String DFLT_CACHE_NAME = "redis-ignite-internal-cache-0";
Expand All @@ -48,23 +50,19 @@ public class RedisCommonAbstractTest extends GridCommonAbstractTest {
@Override protected void beforeTestsStarted() throws Exception {
startGrids(gridCount());

JedisPoolConfig jedisPoolCfg = new JedisPoolConfig();

jedisPoolCfg.setMaxWaitMillis(20000);
jedisPoolCfg.setMaxIdle(100);
jedisPoolCfg.setMinIdle(1);
jedisPoolCfg.setNumTestsPerEvictionRun(10);
jedisPoolCfg.setTestOnBorrow(true);
jedisPoolCfg.setTestOnReturn(true);
jedisPoolCfg.setTestWhileIdle(true);
jedisPoolCfg.setTimeBetweenEvictionRunsMillis(30000);

pool = new JedisPool(jedisPoolCfg, HOST, PORT, 10000);
redisClientFactory = new RedisClientFactory(
new HostAndPort(HOST, PORT),
DefaultJedisClientConfig.builder()
.connectionTimeoutMillis(10000)
.socketTimeoutMillis(10000)
.clientSetInfoConfig(ClientSetInfoConfig.DISABLED)
.build()
);
}

/** {@inheritDoc} */
@Override protected void afterTestsStopped() throws Exception {
pool.destroy();
redisClientFactory = null;
}

/** {@inheritDoc} */
Expand All @@ -82,7 +80,7 @@ public class RedisCommonAbstractTest extends GridCommonAbstractTest {

cfg.setConnectorConfiguration(redisCfg);

CacheConfiguration ccfg = defaultCacheConfiguration();
CacheConfiguration<String, String> ccfg = defaultCacheConfiguration();

ccfg.setStatisticsEnabled(true);
ccfg.setIndexedTypes(String.class, String.class);
Expand Down Expand Up @@ -116,4 +114,31 @@ protected int gridCount() {

assertTrue(jcache().localSize() == 0);
}

/**
* Lightweight Redis connection factory.
*/
protected static class RedisClientFactory {
/** Redis address. */
private final HostAndPort addr;

/** Client config. */
private final DefaultJedisClientConfig cfg;

/**
* @param addr Redis address.
* @param cfg Client config.
*/
RedisClientFactory(HostAndPort addr, DefaultJedisClientConfig cfg) {
this.addr = addr;
this.cfg = cfg;
}

/**
* @return Redis client.
*/
Jedis getResource() {
return new Jedis(addr, cfg);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class RedisProtocolConnectSelfTest extends RedisCommonAbstractTest {
*/
@Test
public void testPing() throws Exception {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
Assert.assertEquals("PONG", jedis.ping());
}
}
Expand All @@ -43,7 +43,7 @@ public void testPing() throws Exception {
*/
@Test
public void testEcho() throws Exception {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
Assert.assertEquals("Hello, grid!", jedis.echo("Hello, grid!"));
}
}
Expand All @@ -53,7 +53,7 @@ public void testEcho() throws Exception {
*/
@Test
public void testSelect() throws Exception {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
// connected to cache with index 0
jedis.set("k0", "v0");
Assert.assertEquals("v0", jedis.get("k0"));
Expand All @@ -64,7 +64,7 @@ public void testSelect() throws Exception {
Assert.assertEquals("v1", jedis.get("k1"));
Assert.assertNull(jedis.get("k0"));

try (Jedis jedis2 = pool.getResource()) {
try (Jedis jedis2 = redisClientFactory.getResource()) {
// connected to cache with index 0
Assert.assertEquals("v0", jedis2.get("k0"));
Assert.assertNull(jedis2.get("k1"));
Expand All @@ -81,7 +81,7 @@ public void testSelect() throws Exception {
/** */
@Test
public void testSetGetLongString() {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
for (int len : new int[] {8, 16, 32}) {
String key = "b" + len;
String val = RandomStringUtils.randomAscii((int)(len * KB));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class RedisProtocolServerSelfTest extends RedisCommonAbstractTest {
*/
@Test
public void testDbSize() throws Exception {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
Assert.assertEquals(0, (long)jedis.dbSize());

jcache().putAll(new HashMap<Integer, Integer>() {
Expand All @@ -50,7 +50,7 @@ public void testDbSize() throws Exception {
*/
@Test
public void testFlushDb() throws Exception {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
Assert.assertEquals(0, (long)jedis.dbSize());

jcache().putAll(new HashMap<Integer, Integer>() {
Expand Down Expand Up @@ -87,7 +87,7 @@ public void testFlushDb() throws Exception {
*/
@Test
public void testFlushAll() throws Exception {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
Assert.assertEquals(0, (long)jedis.dbSize());

for (int i = 0; i < 100; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class RedisProtocolStringAtomicDatastructuresSelfTest extends RedisCommon
*/
@Test
public void testAtomicCommandsTopologyChange() throws Exception {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
int size = grid(0).cachesx().size();

jedis.incr("key1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.params.SetParams;

/**
* Tests for String commands of Redis protocol.
Expand All @@ -36,7 +37,7 @@ public class RedisProtocolStringSelfTest extends RedisCommonAbstractTest {
*/
@Test
public void testGet() throws Exception {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
jcache().put("getKey1", "getVal1");

Assert.assertEquals("getVal1", jedis.get("getKey1"));
Expand All @@ -60,18 +61,18 @@ public void testGet() throws Exception {
*/
@Test
public void testGetSet() throws Exception {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
jcache().put("getSetKey1", "1");

Assert.assertEquals("1", jedis.getSet("getSetKey1", "0"));
Assert.assertEquals("1", jedis.setGet("getSetKey1", "0"));
Assert.assertNull(jedis.get("getSetNonExistingKey"));

jcache().put("setDataTypeKey", new HashSet<String>(Arrays.asList("1", "2")));
jcache().put("setDataTypeKey", new HashSet<>(Arrays.asList("1", "2")));

try {
jedis.getSet("setDataTypeKey", "0");
jedis.setGet("setDataTypeKey", "0");

assert false : "Exception has to be thrown!";
fail("Exception has to be thrown!");
}
catch (JedisDataException e) {
assertTrue(e.getMessage().startsWith("WRONGTYPE"));
Expand All @@ -84,7 +85,7 @@ public void testGetSet() throws Exception {
*/
@Test
public void testMGet() throws Exception {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
jcache().put("getKey1", "getVal1");
jcache().put("getKey2", 0);

Expand Down Expand Up @@ -133,7 +134,7 @@ public void testMGetOrder(boolean directOrder) {
values.add("getValue" + i);
}

try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
for (int i = 0; i < keysCnt; ++i)
jcache().put(keys.get(i), values.get(i));

Expand All @@ -158,7 +159,7 @@ public void testMGetOrder(boolean directOrder) {
*/
@Test
public void testMGetDuplicates() throws Exception {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
jcache().put("key-A", "value-A");
jcache().put("key-B", "value-B");

Expand All @@ -180,22 +181,22 @@ public void testSet() throws Exception {
long EXPIRE_MS = 1000L;
int EXPIRE_SEC = 1;

try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
jedis.set("setKey1", "1");
jedis.set("setKey2".getBytes(), "b0".getBytes());

Assert.assertEquals("1", jcache().get("setKey1"));
Assert.assertEquals("b0", jcache().get("setKey2"));

// test options.
jedis.set("setKey1", "2", "nx");
jedis.set("setKey3", "3", "nx", "px", EXPIRE_MS);
jedis.set("setKey1", "2", SetParams.setParams().nx());
jedis.set("setKey3", "3", SetParams.setParams().nx().px(EXPIRE_MS));

Assert.assertEquals("1", jcache().get("setKey1"));
Assert.assertEquals("3", jcache().get("setKey3"));

jedis.set("setKey1", "2", "xx", "ex", EXPIRE_SEC);
jedis.set("setKey4", "4", "xx");
jedis.set("setKey1", "2", SetParams.setParams().xx().ex(EXPIRE_SEC));
jedis.set("setKey4", "4", SetParams.setParams().xx());

Assert.assertEquals("2", jcache().get("setKey1"));
Assert.assertNull(jcache().get("setKey4"));
Expand All @@ -213,7 +214,7 @@ public void testSet() throws Exception {
*/
@Test
public void testMSet() throws Exception {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
jedis.mset("setKey1", "1", "setKey2", "2");

Assert.assertEquals("1", jcache().get("setKey1"));
Expand All @@ -226,7 +227,7 @@ public void testMSet() throws Exception {
*/
@Test
public void testIncrDecr() throws Exception {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
Assert.assertEquals(1, (long)jedis.incr("newKeyIncr"));
Assert.assertEquals(-1, (long)jedis.decr("newKeyDecr"));

Expand Down Expand Up @@ -310,7 +311,7 @@ public void testIncrDecr() throws Exception {
*/
@Test
public void testIncrDecrBy() throws Exception {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
Assert.assertEquals(2, (long)jedis.incrBy("newKeyIncrBy", 2));
Assert.assertEquals(-2, (long)jedis.decrBy("newKeyDecrBy", 2));

Expand Down Expand Up @@ -367,7 +368,7 @@ public void testIncrDecrBy() throws Exception {
*/
@Test
public void testAppend() throws Exception {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
Assert.assertEquals(5, (long)jedis.append("appendKey1", "Hello"));
Assert.assertEquals(12, (long)jedis.append("appendKey1", " World!"));

Expand All @@ -389,7 +390,7 @@ public void testAppend() throws Exception {
*/
@Test
public void testStrlen() throws Exception {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
Assert.assertEquals(0, (long)jedis.strlen("strlenKeyNonExisting"));

jcache().put("strlenKey", "abc");
Expand All @@ -414,7 +415,7 @@ public void testStrlen() throws Exception {
*/
@Test
public void testSetRange() throws Exception {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
Assert.assertEquals(0, (long)jedis.setrange("setRangeKey1", 0, ""));

jcache().put("setRangeKey2", "abc");
Expand Down Expand Up @@ -463,7 +464,7 @@ public void testSetRange() throws Exception {
*/
@Test
public void testGetRange() throws Exception {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
Assert.assertEquals("", jedis.getrange("getRangeKeyNonExisting", 0, 0));

jcache().put("getRangeKey", "This is a string");
Expand Down Expand Up @@ -493,7 +494,7 @@ public void testGetRange() throws Exception {
public void testDel() throws Exception {
jcache().put("delKey1", "abc");
jcache().put("delKey2", "abcd");
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
// Should return the number of actually deleted entries.
// Assert.assertEquals(0, (long)jedis.del("nonExistingDelKey"));
Assert.assertEquals(2, (long)jedis.del("delKey1", "delKey2"));
Expand All @@ -507,7 +508,7 @@ public void testDel() throws Exception {
public void testExists() throws Exception {
jcache().put("existsKey1", "abc");
jcache().put("existsKey2", "abcd");
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
Assert.assertFalse(jedis.exists("nonExistingDelKey"));
Assert.assertEquals(2, (long)jedis.exists("existsKey1", "existsKey2"));
}
Expand Down Expand Up @@ -539,7 +540,7 @@ public void testExpireMs() throws Exception {

/** */
private void testExpire(Expiration exp) throws Exception {
try (Jedis jedis = pool.getResource()) {
try (Jedis jedis = redisClientFactory.getResource()) {
jedis.set("k1", "v1");

Assert.assertTrue(jedis.exists("k1"));
Expand Down
Loading