Exemplo n.º 1
0
  @Test
  public void testOutPool() throws Exception {
    props.put(ClientConfig.LB_SERVER, "localhost:8900");
    props.setProperty(ClientConfig.MINIMUM_RECONNECT_TIME_INTERVAL, "0");
    props.setProperty(ClientConfig.RECONNECT_INTERVAL, "0");
    props.setProperty(ClientConfig.RECONNECT_TIME_INTERVAL, "0");

    servers = TestConnectionPool.startServers(1, 8900);

    injector =
        LifecycleInjector.builder()
            .withBootstrapModule(
                new BootstrapModule() {
                  @Override
                  public void configure(BootstrapBinder binder) {
                    binder
                        .bindConfigurationProvider()
                        .toInstance(new PropertiesConfigurationProvider(props));
                    binder.bind(ILoadBalancer.class).to(StaticLoadBalancer.class);
                  }
                })
            .build()
            .createInjector();
    injector.getInstance(LifecycleManager.class).start();

    final ConnectionPool pool = injector.getInstance(ConnectionPool.class);
    assertEquals(pool.getPoolSize(), 1);

    ExecutorService executors = Executors.newFixedThreadPool(10);
    for (int i = 0; i < 10; ++i) {
      executors.execute(
          new Runnable() {
            @Override
            public void run() {
              for (int i = 0; i < 5; ++i) {
                try {
                  ConnectionPool.SuroConnection client = pool.chooseConnection();
                  assertEquals(
                      client.send(TestConnectionPool.createMessageSet(100)).getResultCode(),
                      ResultCode.OK);
                  pool.endConnection(client);
                } catch (TException e) {
                  fail(e.getMessage());
                }
              }
            }
          });
    }
    executors.shutdown();
    executors.awaitTermination(10, TimeUnit.SECONDS);

    assertTrue(pool.getOutPoolSize() > 0);
    try {
      Thread.sleep(1000);
    } catch (Exception e) {
      e.printStackTrace();
    }
    TestConnectionPool.checkMessageSetCount(servers, 50, false);

    TestConnectionPool.shutdownServers(servers);
  }
 private TestConnection getNewConnection() throws Exception {
   TestConnection conn = connPool.getConnection(userName, password);
   if (schema != null) conn.setSchema(schema);
   return conn;
 }