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

com.gemstone.gemfire.internal.cache.ha.BlockingHARQAddOperationJUnitTest 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.ha;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.gemstone.gemfire.cache.CacheException;
import com.gemstone.gemfire.internal.cache.Conflatable;
import com.gemstone.gemfire.internal.cache.EventID;

import io.snappydata.test.dunit.DistributedTestBase;

/**
 * Test runs all tests of HARQAddOperationJUnitTest using BlockingHARegionQueue
 * instead of HARegionQueue
 * 
 * @author Suyog Bhokare
 * 
 */

public class BlockingHARQAddOperationJUnitTest extends
    HARQAddOperationJUnitTest
{

  public BlockingHARQAddOperationJUnitTest(String name) {
    super(name);
  }

  /**
   * Creates Blocking HA region-queue object
   * 
   * @return Blocking HA region-queue object
   * @throws IOException
   * @throws ClassNotFoundException
   * @throws CacheException
   * @throws InterruptedException
   */
  protected HARegionQueue createHARegionQueue(String name)
      throws IOException, ClassNotFoundException, CacheException, InterruptedException
  {
    HARegionQueue regionqueue = HARegionQueue.getHARegionQueueInstance(name,
        cache, HARegionQueue.BLOCKING_HA_QUEUE, false);
    return regionqueue;
  }

  /**
   * Creates Blocking HA region-queue object
   * 
   * @return Blocking HA region-queue object
   * @throws IOException
   * @throws ClassNotFoundException
   * @throws CacheException
   * @throws InterruptedException
   */
  protected HARegionQueue createHARegionQueue(String name,
      HARegionQueueAttributes attrs) throws IOException, ClassNotFoundException, CacheException, InterruptedException
  {
    HARegionQueue regionqueue = HARegionQueue.getHARegionQueueInstance(name,
        cache, attrs, HARegionQueue.BLOCKING_HA_QUEUE, false);
    return regionqueue;
  }

  /**
   * Tests the take() functionality of
   * BlockingHARegionQueue with conflation disabled.
   * 
   * @throws Exception
   */
  public void testBlockingTakeConflationDisabled() throws Exception
  {
    this.logger
        .info("HARQAddOperationJUnitTest : testBlockingTakeConflationDisabled BEGIN");

    doBlockingTake(false);
    this.logger
        .info("HARQAddOperationJUnitTest : testBlockingTakeConflationDisabled END");
  }

  /**
   * Tests the take() functionality of
   * BlockingHARegionQueue with conflation enabled.
   * 
   * @throws Exception
   * 
   * @author Dinesh Patel
   */
  public void testBlockingTakeConflationEnabled() throws Exception
  {
    this.logger
        .info("HARQAddOperationJUnitTest : testBlockingTakeConflationEnabled BEGIN");

    doBlockingTake(true);
    this.logger
        .info("HARQAddOperationJUnitTest : testBlockingTakeConflationEnabled END");
  }

  /**
   * This method performs the following steps :
* 1)Create a blocking queue and start a thread which does take() on it. * 2)Verify after significant time that the thread is still alive as it should * be blocked on take() since there are no events in the queue.
* 3)Do a put into the queue and verify that the take thread returns with the * same object. * * @param conflationEnabled - * whether conflation is enabled or not * @throws Exception * * @author Dinesh Patel */ public void doBlockingTake(boolean conflationEnabled) throws Exception { testFailed = false; message = null; final HARegionQueue rq = createHARegionQueue("testBlockingTake"); final List takenObjects = new ArrayList(); Thread takeThread = new Thread() { public void run() { try { takenObjects.add(rq.take()); } catch (Exception e) { testFailed = true; message.append("Exception while performing take operation " + e.getStackTrace()); } } }; takeThread.start(); DistributedTestBase.staticPause(20 * 1000); if (!takeThread.isAlive()) { fail("take() thread died "); } EventID id1 = new EventID(new byte[] { 1 }, 1, 1); ConflatableObject c1 = new ConflatableObject(KEY1, VALUE1, id1, conflationEnabled, "region1"); rq.put(c1); DistributedTestBase.join(takeThread, 20 * 1000, null); assertEquals(1, takenObjects.size()); Conflatable obj = (Conflatable)takenObjects.get(0); assertNotNull(obj); assertEquals(id1, obj.getEventId()); if (testFailed) fail("Test failed due to " + message); } /** * This test performs the following steps :
* 1)Create a blocking queue.
* 2) Start two threads which does take() on it and add the return object to a * list.
* 3)Put two object into the queue.
* 4)Verify both both take() threads return with an object by ensuring that * the size of the list containing return objects is two.
* * @throws Exception * * @author Dinesh Patel */ public void testConcurrentBlockingTake() throws Exception { this.logger .info("HARQAddOperationJUnitTest : testConcurrentBlockingTake BEGIN"); testFailed = false; message = null; final HARegionQueue rq = createHARegionQueue("testBlockingTake"); final List takenObjects = new Vector(); final int totalTakeThreads = 2; Thread[] takeThreads = new Thread[totalTakeThreads]; for (int i = 0; i < totalTakeThreads; i++) { takeThreads[i] = new Thread() { public void run() { try { takenObjects.add(rq.take()); } catch (Exception e) { testFailed = true; message.append("Exception while performing take operation " + e.getStackTrace()); } } }; takeThreads[i].start(); } Conflatable c = null; EventID id = null; for (int i = 0; i < totalTakeThreads; i++) { id = new EventID(new byte[] { 1 }, 1, i); c = new ConflatableObject("k" + i, "v" + i, id, true, "region1"); rq.put(c); } for (int i = 0; i < totalTakeThreads; i++) { DistributedTestBase.join(takeThreads[i], 20 * 1000, null); } assertEquals(totalTakeThreads, takenObjects.size()); for (int i = 0; i < totalTakeThreads; i++) { c = (Conflatable)takenObjects.get(i); assertNotNull(c); } if (testFailed) fail("Test failed due to " + message); this.logger .info("HARQAddOperationJUnitTest : testConcurrentBlockingTake END"); } }