示例#1
0
 /**
  * Shutdown this client and close all open connections. The client should be discarded after
  * calling shutdown.
  */
 public void shutdown() {
   for (Channel c : channels) {
     ChannelPipeline pipeline = c.getPipeline();
     RedisAsyncConnection<?, ?> connection = pipeline.get(RedisAsyncConnection.class);
     connection.close();
   }
   ChannelGroupFuture future = channels.close();
   future.awaitUninterruptibly();
   bootstrap.releaseExternalResources();
 }
  @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
  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();
    }
  }
  @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();
    }
  }
示例#8
0
  @Test
  public void retryAfterConnectionIsDisconnected() throws Exception {

    RedisAsyncConnection<String, String> connection = client.connectAsync();
    RedisChannelHandler<String, String> redisChannelHandler =
        (RedisChannelHandler) connection.getStatefulConnection();
    RedisCommands<String, String> verificationConnection = client.connect().sync();

    connection.set(key, "1").get();

    ConnectionWatchdog connectionWatchdog =
        Connections.getConnectionWatchdog(connection.getStatefulConnection());
    connectionWatchdog.setListenOnChannelInactive(false);

    connection.quit();
    while (connection.isOpen()) {
      Thread.sleep(100);
    }

    assertThat(connection.incr(key).await(1, TimeUnit.SECONDS)).isFalse();

    assertThat(verificationConnection.get("key")).isEqualTo("1");

    assertThat(getQueue(redisChannelHandler)).isEmpty();
    assertThat(getCommandBuffer(redisChannelHandler).size()).isGreaterThan(0);

    connectionWatchdog.setListenOnChannelInactive(true);
    connectionWatchdog.scheduleReconnect();

    while (!getCommandBuffer(redisChannelHandler).isEmpty()
        || !getQueue(redisChannelHandler).isEmpty()) {
      Thread.sleep(10);
    }

    assertThat(connection.get(key).get()).isEqualTo("2");
    assertThat(verificationConnection.get(key)).isEqualTo("2");

    connection.close();
    verificationConnection.close();
  }