/** @see DATAREDIS-462 */ @Test public void poolWorksWithoutClientResources() { pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort()); pool.setDatabase(1); pool.afterPropertiesSet(); assertNotNull(pool.getResource()); }
@Test(expected = PoolException.class) public void testCreateWithPasswordNoPassword() { pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort()); pool.setClientResources(LettuceTestClientResources.getSharedClientResources()); pool.setPassword("notthepassword"); pool.afterPropertiesSet(); pool.getResource(); }
@Test public void testCreateWithDbIndex() { pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort()); pool.setClientResources(LettuceTestClientResources.getSharedClientResources()); pool.setDatabase(1); pool.afterPropertiesSet(); assertNotNull(pool.getResource()); }
@Test(expected = PoolException.class) public void testCreateWithDbIndexInvalid() { pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort()); pool.setClientResources(LettuceTestClientResources.getSharedClientResources()); pool.setDatabase(17); pool.afterPropertiesSet(); pool.getResource(); }
@Ignore("Redis must have requirepass set to run this test") @Test(expected = PoolException.class) public void testCreateInvalidPassword() { pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort()); pool.setClientResources(LettuceTestClientResources.getSharedClientResources()); pool.setPassword("bad"); pool.afterPropertiesSet(); pool.getResource(); }
@Test public void testGetResource() { pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort()); pool.setClientResources(LettuceTestClientResources.getSharedClientResources()); pool.afterPropertiesSet(); RedisAsyncConnection<byte[], byte[]> client = pool.getResource(); assertNotNull(client); client.ping(); client.close(); }
@Test public void testGetResourceValidate() { GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); poolConfig.setTestOnBorrow(true); pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig); pool.setClientResources(LettuceTestClientResources.getSharedClientResources()); pool.afterPropertiesSet(); RedisAsyncConnection<byte[], byte[]> client = pool.getResource(); assertNotNull(client); client.close(); }
@Ignore("Redis must have requirepass set to run this test") @Test public void testCreatePassword() { pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort()); pool.setClientResources(LettuceTestClientResources.getSharedClientResources()); pool.setPassword("foo"); pool.afterPropertiesSet(); RedisAsyncConnection<byte[], byte[]> conn = pool.getResource(); conn.ping(); conn.close(); }
@Test public void testReturnResource() { GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); poolConfig.setMaxTotal(1); poolConfig.setMaxWaitMillis(1); pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig); pool.setClientResources(LettuceTestClientResources.getSharedClientResources()); pool.afterPropertiesSet(); RedisAsyncConnection<byte[], byte[]> client = pool.getResource(); assertNotNull(client); pool.returnResource(client); assertNotNull(pool.getResource()); client.close(); }
@Test(expected = PoolException.class) public void testGetResourceCreationUnsuccessful() throws Exception { pool = new DefaultLettucePool(SettingsUtils.getHost(), 3333); pool.setClientResources(LettuceTestClientResources.getSharedClientResources()); pool.afterPropertiesSet(); pool.getResource(); }
@Test public void testGetResourcePoolExhausted() { GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); poolConfig.setMaxTotal(1); poolConfig.setMaxWaitMillis(1); pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig); pool.setClientResources(LettuceTestClientResources.getSharedClientResources()); pool.afterPropertiesSet(); RedisAsyncConnection<byte[], byte[]> client = pool.getResource(); assertNotNull(client); try { pool.getResource(); fail("PoolException should be thrown when pool exhausted"); } catch (PoolException e) { } finally { client.close(); } }
@Before public void setUp() throws Exception { executorMock = mock(Executor.class); connectionFactory = new JedisConnectionFactory(); connectionFactory.setPort(SettingsUtils.getPort()); connectionFactory.setHostName(SettingsUtils.getHost()); connectionFactory.setDatabase(2); connectionFactory.afterPropertiesSet(); container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setBeanName("container"); container.setTaskExecutor(new SyncTaskExecutor()); container.setSubscriptionExecutor(executorMock); container.afterPropertiesSet(); }
@Test public void testReturnBrokenResource() { GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); poolConfig.setMaxTotal(1); poolConfig.setMaxWaitMillis(1); pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig); pool.setClientResources(LettuceTestClientResources.getSharedClientResources()); pool.afterPropertiesSet(); RedisAsyncConnection<byte[], byte[]> client = pool.getResource(); assertNotNull(client); pool.returnBrokenResource(client); RedisAsyncConnection<byte[], byte[]> client2 = pool.getResource(); assertNotSame(client, client2); try { client.ping(); fail("Broken resouce connection should be closed"); } catch (RedisException e) { } finally { client.close(); client2.close(); } }
@Parameters public static Collection<Object[]> testParams() { // XStream serializer XStreamMarshaller xstream = new XStreamMarshaller(); try { xstream.afterPropertiesSet(); } catch (Exception ex) { throw new RuntimeException("Cannot init XStream", ex); } OxmSerializer serializer = new OxmSerializer(xstream, xstream); JacksonJsonRedisSerializer<Person> jsonSerializer = new JacksonJsonRedisSerializer<Person>(Person.class); JacksonJsonRedisSerializer<String> jsonStringSerializer = new JacksonJsonRedisSerializer<String>(String.class); // create Jedis Factory ObjectFactory<String> stringFactory = new StringObjectFactory(); JedisConnectionFactory jedisConnFactory = new JedisConnectionFactory(); jedisConnFactory.setUsePool(true); jedisConnFactory.setPort(SettingsUtils.getPort()); jedisConnFactory.setHostName(SettingsUtils.getHost()); jedisConnFactory.afterPropertiesSet(); RedisTemplate<String, String> genericTemplate = new StringRedisTemplate(jedisConnFactory); RedisTemplate<String, String> xstreamGenericTemplate = new RedisTemplate<String, String>(); xstreamGenericTemplate.setConnectionFactory(jedisConnFactory); xstreamGenericTemplate.setDefaultSerializer(serializer); xstreamGenericTemplate.afterPropertiesSet(); RedisTemplate<String, Person> jsonPersonTemplate = new RedisTemplate<String, Person>(); jsonPersonTemplate.setConnectionFactory(jedisConnFactory); jsonPersonTemplate.setDefaultSerializer(jsonSerializer); jsonPersonTemplate.setHashKeySerializer(jsonSerializer); jsonPersonTemplate.setHashValueSerializer(jsonStringSerializer); jsonPersonTemplate.afterPropertiesSet(); // JRedis JredisConnectionFactory jredisConnFactory = new JredisConnectionFactory(); jredisConnFactory.setUsePool(true); jredisConnFactory.setPort(SettingsUtils.getPort()); jredisConnFactory.setHostName(SettingsUtils.getHost()); jredisConnFactory.afterPropertiesSet(); RedisTemplate<String, String> genericTemplateJR = new StringRedisTemplate(jredisConnFactory); RedisTemplate<String, Person> xGenericTemplateJR = new RedisTemplate<String, Person>(); xGenericTemplateJR.setConnectionFactory(jredisConnFactory); xGenericTemplateJR.setDefaultSerializer(serializer); xGenericTemplateJR.afterPropertiesSet(); RedisTemplate<String, Person> jsonPersonTemplateJR = new RedisTemplate<String, Person>(); jsonPersonTemplateJR.setConnectionFactory(jredisConnFactory); jsonPersonTemplateJR.setDefaultSerializer(jsonSerializer); jsonPersonTemplateJR.setHashKeySerializer(jsonSerializer); jsonPersonTemplateJR.setHashValueSerializer(jsonStringSerializer); jsonPersonTemplateJR.afterPropertiesSet(); // RJC // rjc RjcConnectionFactory rjcConnFactory = new RjcConnectionFactory(); rjcConnFactory.setUsePool(true); rjcConnFactory.setPort(SettingsUtils.getPort()); rjcConnFactory.setHostName(SettingsUtils.getHost()); rjcConnFactory.afterPropertiesSet(); RedisTemplate<String, String> genericTemplateRJC = new StringRedisTemplate(jredisConnFactory); RedisTemplate<String, Person> xGenericTemplateRJC = new RedisTemplate<String, Person>(); xGenericTemplateRJC.setConnectionFactory(rjcConnFactory); xGenericTemplateRJC.setDefaultSerializer(serializer); xGenericTemplateRJC.afterPropertiesSet(); RedisTemplate<String, Person> jsonPersonTemplateRJC = new RedisTemplate<String, Person>(); jsonPersonTemplateRJC.setConnectionFactory(rjcConnFactory); jsonPersonTemplateRJC.setDefaultSerializer(jsonSerializer); jsonPersonTemplateRJC.setHashKeySerializer(jsonSerializer); jsonPersonTemplateRJC.setHashValueSerializer(jsonStringSerializer); jsonPersonTemplateRJC.afterPropertiesSet(); // Lettuce LettuceConnectionFactory lettuceConnFactory = new LettuceConnectionFactory(); lettuceConnFactory.setPort(SettingsUtils.getPort()); lettuceConnFactory.setHostName(SettingsUtils.getHost()); lettuceConnFactory.afterPropertiesSet(); RedisTemplate<String, String> genericTemplateLtc = new StringRedisTemplate(lettuceConnFactory); RedisTemplate<String, Person> xGenericTemplateLtc = new RedisTemplate<String, Person>(); xGenericTemplateLtc.setConnectionFactory(lettuceConnFactory); xGenericTemplateLtc.setDefaultSerializer(serializer); xGenericTemplateLtc.afterPropertiesSet(); RedisTemplate<String, Person> jsonPersonTemplateLtc = new RedisTemplate<String, Person>(); jsonPersonTemplateLtc.setConnectionFactory(lettuceConnFactory); jsonPersonTemplateLtc.setDefaultSerializer(jsonSerializer); jsonPersonTemplateLtc.setHashKeySerializer(jsonSerializer); jsonPersonTemplateLtc.setHashValueSerializer(jsonStringSerializer); jsonPersonTemplateLtc.afterPropertiesSet(); return Arrays.asList( new Object[][] { {stringFactory, stringFactory, genericTemplate}, {stringFactory, stringFactory, genericTemplate}, {stringFactory, stringFactory, genericTemplate}, {stringFactory, stringFactory, genericTemplate}, {stringFactory, stringFactory, xstreamGenericTemplate}, {stringFactory, stringFactory, genericTemplateJR}, {stringFactory, stringFactory, genericTemplateJR}, {stringFactory, stringFactory, genericTemplateJR}, {stringFactory, stringFactory, genericTemplateJR}, {stringFactory, stringFactory, xGenericTemplateJR}, {stringFactory, stringFactory, jsonPersonTemplate}, {stringFactory, stringFactory, jsonPersonTemplateJR}, {stringFactory, stringFactory, genericTemplateRJC}, {stringFactory, stringFactory, genericTemplateRJC}, {stringFactory, stringFactory, genericTemplateRJC}, {stringFactory, stringFactory, genericTemplateRJC}, {stringFactory, stringFactory, xGenericTemplateRJC}, {stringFactory, stringFactory, jsonPersonTemplateRJC}, {stringFactory, stringFactory, genericTemplateLtc}, {stringFactory, stringFactory, genericTemplateLtc}, {stringFactory, stringFactory, genericTemplateLtc}, {stringFactory, stringFactory, genericTemplateLtc}, {stringFactory, stringFactory, xGenericTemplateLtc}, {stringFactory, stringFactory, jsonPersonTemplateLtc} }); }