All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.internal.datasource.ConnectionPoolCacheImplTest Maven / Gradle / Ivy

There is a newer version: 2.0-BETA
Show newest version
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You
 * may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License. See accompanying
 * LICENSE file.
 */
/*
 * ConnectionPoolCacheImplTest.java JUnit based test
 * 
 * Created on March 3, 2005, 5:26 PM
 */
package com.gemstone.gemfire.internal.datasource;

import java.sql.Connection;
import java.util.*;
import javax.sql.PooledConnection;
import junit.framework.*;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.distributed.DistributedSystem;
import javax.naming.Context;

/*
 * @author tnegi this is a Multithreaded test for datasource connection pool.
 */
public class ConnectionPoolCacheImplTest extends TestCase {

	protected static ConnectionPoolCacheImpl poolCache = null;
  private Thread ThreadA;
  protected Thread ThreadB;
//  private Thread ThreadC;
  protected static int maxPoolSize;
  protected static GemFireConnPooledDataSource ds = null;
  private static GemFireConnectionPoolManager provider = null;
  private static Properties props = null;
  private static DistributedSystem ds1 = null;
  private static Cache cache = null;
  static {
    try {
      props = new Properties();
      props.setProperty("mcast-port", "0");
      String path = System.getProperty("JTAXMLFILE");
      props.setProperty("cache-xml-file", path);
      ds1 = DistributedSystem.connect(props);
      cache = CacheFactory.create(ds1);
    }
    catch (Exception e) {
      fail("Exception occured in creation of ds and cache due to " + e);
      e.printStackTrace();
    }
  }

  public ConnectionPoolCacheImplTest(java.lang.String testName) {
    super(testName);
  }

  public void setup() {
  }

  public void teardown() {
  }

  public void testGetSimpleDataSource() throws Exception {
    try {
      Context ctx = cache.getJNDIContext();
      GemFireBasicDataSource ds = (GemFireBasicDataSource) ctx
          .lookup("java:/SimpleDataSource");
      Connection conn = ds.getConnection();
      if (conn == null)
          fail("DataSourceFactoryTest-testGetSimpleDataSource() Error in creating the GemFireBasicDataSource");
    }
    catch (Exception e) {
      fail("Exception occured in testGetSimpleDataSource due to " + e);
      e.printStackTrace();
    }
  }

  public void testConnectionPoolFunctions() {
    try {
      createPool();
      PoolClient_1 clientA = new PoolClient_1();
      ThreadA = new Thread(clientA, "ThreadA");
      PoolClient_2 clientB = new PoolClient_2();
      ThreadB = new Thread(clientB, "ThreadB");
      ThreadA.start();
    }
    catch (Exception e) {
      fail("Exception occured in testConnectionPoolFunctions due to " + e);
      e.printStackTrace();
    }
  }

  public static void createPool() throws Exception {
    Context ctx = cache.getJNDIContext();
    GemFireConnPooledDataSource ds = (GemFireConnPooledDataSource) ctx
        .lookup("java:/PooledDataSource");
    provider = (GemFireConnectionPoolManager) ds.getConnectionProvider();
    poolCache = (ConnectionPoolCacheImpl) provider.getConnectionPoolCache();
    maxPoolSize = poolCache.getMaxLimit();
  }

  class PoolClient_1 implements Runnable {

    List poolConnlist = new ArrayList();

    public void run() {
      String threadName = Thread.currentThread().getName();
      //	System.out.println(" Inside Run method of " + threadName);
      int numConn = 0;
      while (poolConnlist.size() < maxPoolSize) {
        try {
          //	System.out.println(" Getting a connection from " + threadName);
          PooledConnection conn = (PooledConnection) poolCache
              .getPooledConnectionFromPool();
          poolConnlist.add(conn);
          numConn++;
          //System.out.println(" Got connection " + numConn + "from "+
          // threadName);
        }
        catch (Exception ex) {
          fail("Exception occured in trying to getPooledConnectionfromPool due to "
              + ex);
          ex.printStackTrace();
        }
      }
      if (numConn != maxPoolSize)
          fail("#### Error in filling the the connection pool from "
              + threadName);
      ThreadB.start();
      //System.out.println(" AFTER starting THREADB");
      int numC = 0;
//      int display = 0;
      long birthTime = 0;
      long newTime = 0;
      long duration = 0;
      for (numC = 0; numC < poolConnlist.size(); numC++) {
        try {
          birthTime = System.currentTimeMillis();
          while (true) {
            newTime = System.currentTimeMillis();
            duration = newTime - birthTime;
            if (duration > 5) break;
          }
//          display = numC + 1;
          //System.out.println(" Returning connection " + display + "from "+
          // threadName);
          poolCache
              .returnPooledConnectionToPool(poolConnlist
                  .get(numC));
          //System.out.println(" Returned connection " + display + "from "+
          // threadName);
        }
        catch (Exception ex) {
          fail("Exception occured in trying to returnPooledConnectiontoPool due to "
              + ex);
          ex.printStackTrace();
        }
      }
      if (numC != maxPoolSize)
          fail("#### Error in returning all the connections to the  pool from "
              + threadName);
      //System.out.println(" ****************Returned all connections " +
      // threadName + "***********");
    }
  }

  class PoolClient_2 implements Runnable {

    List poolConnlist = new ArrayList();

    public void run() {
      String threadName = Thread.currentThread().getName();
      //System.out.println(" Inside Run method of " + threadName);
      int numConn2 = 0;
//      int display = 0;
      while (numConn2 < maxPoolSize) {
        try {
          PooledConnection conn = (PooledConnection) poolCache
              .getPooledConnectionFromPool();
          poolConnlist.add(conn);
          numConn2++;
          //	System.out.println(" ********** Got connection " + numConn2+ "from
          // " + threadName);
        }
        catch (Exception ex) {
          fail("Exception occured in trying to getPooledConnectionFromPool due to "
              + ex);
          ex.printStackTrace();
        }
      }
      if (numConn2 != maxPoolSize)
          fail("#### Error in getting all connections from the " + threadName);
      //System.out.println(" ****************GOT ALL connections "+ threadName
      // + "***********");
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy