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

com.gemstone.gemfire.internal.cache.ClientMessagesRegionCreationAndDestroyJUnitTest 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.
 */
package com.gemstone.gemfire.internal.cache;

import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;

import junit.framework.TestCase;

import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheException;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.distributed.DistributedSystem;
import com.gemstone.gemfire.internal.AvailablePort;
import com.gemstone.gemfire.internal.cache.ha.HAContainerWrapper;
import com.gemstone.gemfire.internal.cache.ha.HARegionQueue;

/**
 * Test to verify that each bridge sever creates its own client_messages_region
 * at its start and destroys it when it stops.
 * 
 * @author aingle
 * @since 5.7
 */
public class ClientMessagesRegionCreationAndDestroyJUnitTest extends TestCase {

  /** The cache instance */
  private Cache cache = null;
  // max number of bridge server can attached to the cache 
  private int brigeNum = 5;
  // stores corresponding names of client messages region created by bridge
  // server
  private HashSet regionNames = new HashSet();
  
  /**
   * Constructor
   * 
   * @param name -
   *          name
   */
  public ClientMessagesRegionCreationAndDestroyJUnitTest(String name){
    super(name);
  }
  /**
   * Create the cache in setup.
   * 
   * @throws Exception -
   *           thrown if any exception occurs in setUp
   */
  protected void setUp() throws Exception
  {
    super.setUp();
    cache = createCache();
  }
  
  /**
   * Create and attach bridge server to cache
   * @throws IOException
   */
  
  private void attachBridgeServer() throws IOException {
    BridgeServerImpl server = (BridgeServerImpl)cache.addBridgeServer();
    assertNotNull(server);
    int port = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
    server.setPort(port);
    server.getClientSubscriptionConfig().setEvictionPolicy(HARegionQueue.HA_EVICTION_POLICY_ENTRY);
    server.start();
    assertNotNull("client messages region is null ", server.getAcceptor().getCacheClientNotifier().getHaContainer());
//  check for is LIFO Enable
    String regionName = ((HAContainerWrapper)server.getAcceptor().getCacheClientNotifier().getHaContainer()).getName();
    EvictionAttributesImpl ea = (EvictionAttributesImpl)cache.getRegion(
        Region.SEPARATOR + regionName).getAttributes().getEvictionAttributes();
    assertTrue("Eviction Algorithm is not LIFO", ea.isLIFO());
    // The CacheClientNotifier is a singleton. 
    if (cache.getBridgeServers().size() <= 1) {
      assertTrue("client messages region name should not be present ", (regionNames).add(regionName));
    } else {
      assertTrue("client messages region name should have been already present ", (regionNames).contains(regionName));      
    }
  }
  
  /**
   * This test does the following :
* 1)Verify client messages region get created when bridge serve start's
* 2)Verify client messages region get destroy when bridge serve stop's
*/ public void testCreationAndDestroyOfClientMessagesRegion() { for (int i = 0; i < 5; i++) { // sequential attachmentOfBridgeServer(); } // making sure stop all servers dettachmentOfBridgeServer(); } /** * Attach bridge server */ private void attachmentOfBridgeServer() { if (cache.getBridgeServers().size() < brigeNum) { try { // attaching and starting bridge server attachBridgeServer(); } catch (IOException e) { e.printStackTrace(); } } } /** * Stop's all bridge servers attached */ private void dettachmentOfBridgeServer() { // detach all bridge server to test destroy of client_messages_region for (Iterator itr = cache.getBridgeServers().iterator(); itr.hasNext();) { BridgeServerImpl server = (BridgeServerImpl)itr.next(); String rName = ((HAContainerWrapper)server.getAcceptor().getCacheClientNotifier().getHaContainer()).getName(); assertNotNull("client messages region is null ", cache.getRegion(Region.SEPARATOR + rName)); server.stop(); if (!itr.hasNext()) { assertNull("client messages region is not null ", cache.getRegion(Region.SEPARATOR + rName)); } } } /** * Close the cache in tear down * * * @throws Exception - * thrown if any exception occurs in tearDown */ protected void tearDown() throws Exception { super.tearDown(); // its stops all bridge serves associated with cache // delete client_messages_region cache.close(); } /** * Creates the cache instance for the test * * @return the cache instance * @throws CacheException - * thrown if any exception occurs in cache creation */ private Cache createCache() throws CacheException { return CacheFactory.create(DistributedSystem.connect(new Properties())); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy