geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dschnei...@apache.org
Subject incubator-geode git commit: GEODE-798: change unit test to not create a real cache
Date Wed, 20 Jan 2016 19:11:37 GMT
Repository: incubator-geode
Updated Branches:
  refs/heads/develop 9d0b12cb7 -> 27f9c0e87


GEODE-798: change unit test to not create a real cache

testCreateOffHeapStorage now uses mocks.
Also a LocalStatisticsFactory can now be created without starting a sampler.
This allows it to be used as a light weight StatisticsFactory compared to InternalDistributedSystem.
The nice thing compared to a mocked StatisticsFactory is it will still create functional Statistics
instances.


Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/27f9c0e8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/27f9c0e8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/27f9c0e8

Branch: refs/heads/develop
Commit: 27f9c0e8775d50981241a67d792abb3e94adb939
Parents: 9d0b12c
Author: Darrel Schneider <dschneider@pivotal.io>
Authored: Tue Jan 19 17:16:38 2016 -0800
Committer: Darrel Schneider <dschneider@pivotal.io>
Committed: Wed Jan 20 11:07:13 2016 -0800

----------------------------------------------------------------------
 .../internal/LocalStatisticsFactory.java        |   4 +-
 .../internal/offheap/OffHeapStorage.java        |  48 ++--
 .../offheap/OffHeapStorageJUnitTest.java        | 237 +++++++++----------
 3 files changed, 135 insertions(+), 154 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/27f9c0e8/gemfire-core/src/main/java/com/gemstone/gemfire/internal/LocalStatisticsFactory.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/LocalStatisticsFactory.java
b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/LocalStatisticsFactory.java
index 6bd83c0..e645d94 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/LocalStatisticsFactory.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/LocalStatisticsFactory.java
@@ -52,9 +52,11 @@ public class LocalStatisticsFactory extends AbstractStatisticsFactory
     if (statsDisabled) {
       this.sampler = null;
       logger.info(LogMarker.STATISTICS, LocalizedMessage.create(LocalizedStrings.LocalStatisticsFactory_STATISTIC_COLLECTION_IS_DISABLED_USE_DSTATSDISABLEFALSE_TO_TURN_ON_STATISTICS));
-    } else {
+    } else if (stopper != null) {
       this.sampler = new SimpleStatSampler(stopper, this);
       this.sampler.start();
+    } else {
+      this.sampler = null;
     }
   }
   

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/27f9c0e8/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStorage.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStorage.java
b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStorage.java
index be27b5e..c950134 100755
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStorage.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStorage.java
@@ -173,33 +173,35 @@ public class OffHeapStorage implements OffHeapMemoryStats {
    * @return MemoryAllocator for off-heap storage
    */
   public static MemoryAllocator createOffHeapStorage(LogWriter lw, StatisticsFactory sf,
long offHeapMemorySize, DistributedSystem system) {
-    MemoryAllocator result;
     if (offHeapMemorySize == 0 || Boolean.getBoolean(InternalLocator.FORCE_LOCATOR_DM_TYPE))
{
       // Checking the FORCE_LOCATOR_DM_TYPE is a quick hack to keep our locator from allocating
off heap memory.
-      result = null;
-    } else {
-      // Ensure that using off-heap will work with this JVM.
-      validateVmCompatibility();
-      
-      final OffHeapMemoryStats stats = new OffHeapStorage(sf);
-      
-      if (offHeapMemorySize < MIN_SLAB_SIZE) {
-        throw new IllegalArgumentException("The amount of off heap memory must be at least
" + MIN_SLAB_SIZE + " but it was set to " + offHeapMemorySize);
-      }
-      
-      // determine off-heap and slab sizes
-      final long maxSlabSize = calcMaxSlabSize(offHeapMemorySize);
-      
-      final int slabCount = calcSlabCount(maxSlabSize, offHeapMemorySize);
+      return null;
+    }
 
-      if (system == null) {
-        throw new IllegalArgumentException("InternalDistributedSystem is null");
-      }
-      // ooohml provides the hook for disconnecting and closing cache on OutOfOffHeapMemoryException
-      OutOfOffHeapMemoryListener ooohml = new DisconnectingOutOfOffHeapMemoryListener((InternalDistributedSystem)
system);
-      result = SimpleMemoryAllocatorImpl.create(ooohml, stats, lw, slabCount, offHeapMemorySize,
maxSlabSize);
+    if (offHeapMemorySize < MIN_SLAB_SIZE) {
+      throw new IllegalArgumentException("The amount of off heap memory must be at least
" + MIN_SLAB_SIZE + " but it was set to " + offHeapMemorySize);
     }
-    return result;
+
+    // Ensure that using off-heap will work with this JVM.
+    validateVmCompatibility();
+
+    if (system == null) {
+      throw new IllegalArgumentException("InternalDistributedSystem is null");
+    }
+    // ooohml provides the hook for disconnecting and closing cache on OutOfOffHeapMemoryException
+    OutOfOffHeapMemoryListener ooohml = new DisconnectingOutOfOffHeapMemoryListener((InternalDistributedSystem)
system);
+    return basicCreateOffHeapStorage(lw, sf, offHeapMemorySize, ooohml);
+  }
+  
+  static MemoryAllocator basicCreateOffHeapStorage(LogWriter lw, StatisticsFactory sf, long
offHeapMemorySize, OutOfOffHeapMemoryListener ooohml) {
+    final OffHeapMemoryStats stats = new OffHeapStorage(sf);
+
+   // determine off-heap and slab sizes
+    final long maxSlabSize = calcMaxSlabSize(offHeapMemorySize);
+
+    final int slabCount = calcSlabCount(maxSlabSize, offHeapMemorySize);
+
+    return SimpleMemoryAllocatorImpl.create(ooohml, stats, lw, slabCount, offHeapMemorySize,
maxSlabSize);
   }
   
   private static final long MAX_SLAB_SIZE = Integer.MAX_VALUE;

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/27f9c0e8/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStorageJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStorageJUnitTest.java
b/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStorageJUnitTest.java
index 07b65d9..d31daef 100755
--- a/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStorageJUnitTest.java
+++ b/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStorageJUnitTest.java
@@ -17,15 +17,9 @@
 package com.gemstone.gemfire.internal.offheap;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
-
-import java.io.File;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.concurrent.TimeUnit;
+import static org.mockito.Mockito.*;
 
 import org.junit.After;
 import org.junit.Before;
@@ -35,13 +29,12 @@ import org.junit.contrib.java.lang.system.RestoreSystemProperties;
 import org.junit.experimental.categories.Category;
 
 import com.gemstone.gemfire.OutOfOffHeapMemoryException;
-import com.gemstone.gemfire.cache.Cache;
-import com.gemstone.gemfire.cache.CacheFactory;
+import com.gemstone.gemfire.StatisticsFactory;
 import com.gemstone.gemfire.distributed.DistributedSystem;
 import com.gemstone.gemfire.distributed.internal.DistributionStats;
 import com.gemstone.gemfire.distributed.internal.InternalLocator;
+import com.gemstone.gemfire.internal.LocalStatisticsFactory;
 import com.gemstone.gemfire.test.junit.categories.UnitTest;
-import com.jayway.awaitility.Awaitility;
 
 @Category(UnitTest.class)
 public class OffHeapStorageJUnitTest {
@@ -132,141 +125,125 @@ public class OffHeapStorageJUnitTest {
     } finally {
       System.clearProperty(InternalLocator.FORCE_LOCATOR_DM_TYPE);
     }
-    // TODO: mock the StatiticsFactory and InternalDistributedSystem that createOffHeapStorage
require
-    Cache c = new CacheFactory().set("mcast-port", "0").create();
+    StatisticsFactory statsFactory = mock(StatisticsFactory.class);
     try {
-      try {
-        OffHeapStorage.createOffHeapStorage(null, c.getDistributedSystem(), OffHeapStorage.MIN_SLAB_SIZE-1,
c.getDistributedSystem());
-      } catch (IllegalArgumentException expected) {
-        expected.getMessage().equals("The amount of off heap memory must be at least " +
OffHeapStorage.MIN_SLAB_SIZE + " but it was set to " + (OffHeapStorage.MIN_SLAB_SIZE-1));
-      }
-      try {
-        OffHeapStorage.createOffHeapStorage(null, c.getDistributedSystem(), OffHeapStorage.MIN_SLAB_SIZE,
null);
-      } catch (IllegalArgumentException expected) {
-        expected.getMessage().equals("InternalDistributedSystem is null");
-      }
-      MemoryAllocator ma = OffHeapStorage.createOffHeapStorage(null, c.getDistributedSystem(),
1024*1024, c.getDistributedSystem());
-      try {
-        OffHeapMemoryStats stats = ma.getStats();
-        assertEquals(1024*1024, stats.getFreeMemory());
-        assertEquals(1024*1024, stats.getMaxMemory());
-        assertEquals(0, stats.getUsedMemory());
-        assertEquals(0, stats.getCompactions());
-        assertEquals(0, stats.getCompactionTime());
-        assertEquals(0, stats.getFragmentation());
-        assertEquals(1, stats.getFragments());
-        assertEquals(1024*1024, stats.getLargestFragment());
-        assertEquals(0, stats.getObjects());
-        assertEquals(0, stats.getReads());
+      OffHeapStorage.createOffHeapStorage(null, statsFactory, OffHeapStorage.MIN_SLAB_SIZE-1,
null);
+    } catch (IllegalArgumentException expected) {
+      expected.getMessage().equals("The amount of off heap memory must be at least " + OffHeapStorage.MIN_SLAB_SIZE
+ " but it was set to " + (OffHeapStorage.MIN_SLAB_SIZE-1));
+    }
+    try {
+      OffHeapStorage.createOffHeapStorage(null, statsFactory, OffHeapStorage.MIN_SLAB_SIZE,
(DistributedSystem)null);
+    } catch (IllegalArgumentException expected) {
+      expected.getMessage().equals("InternalDistributedSystem is null");
+    }
 
-        stats.incFreeMemory(100);
-        assertEquals(1024*1024+100, stats.getFreeMemory());
-        stats.incFreeMemory(-100);
-        assertEquals(1024*1024, stats.getFreeMemory());
+    StatisticsFactory localStatsFactory = new LocalStatisticsFactory(null);
+    OutOfOffHeapMemoryListener ooohml = mock(OutOfOffHeapMemoryListener.class);
+    MemoryAllocator ma = OffHeapStorage.basicCreateOffHeapStorage(null, localStatsFactory,
1024*1024, ooohml);
+    try {
+      OffHeapMemoryStats stats = ma.getStats();
+      assertEquals(1024*1024, stats.getFreeMemory());
+      assertEquals(1024*1024, stats.getMaxMemory());
+      assertEquals(0, stats.getUsedMemory());
+      assertEquals(0, stats.getCompactions());
+      assertEquals(0, stats.getCompactionTime());
+      assertEquals(0, stats.getFragmentation());
+      assertEquals(1, stats.getFragments());
+      assertEquals(1024*1024, stats.getLargestFragment());
+      assertEquals(0, stats.getObjects());
+      assertEquals(0, stats.getReads());
 
-        stats.incMaxMemory(100);
-        assertEquals(1024*1024+100, stats.getMaxMemory());
-        stats.incMaxMemory(-100);
-        assertEquals(1024*1024, stats.getMaxMemory());
+      stats.incFreeMemory(100);
+      assertEquals(1024*1024+100, stats.getFreeMemory());
+      stats.incFreeMemory(-100);
+      assertEquals(1024*1024, stats.getFreeMemory());
 
-        stats.incUsedMemory(100);
-        assertEquals(100, stats.getUsedMemory());
-        stats.incUsedMemory(-100);
-        assertEquals(0, stats.getUsedMemory());
+      stats.incMaxMemory(100);
+      assertEquals(1024*1024+100, stats.getMaxMemory());
+      stats.incMaxMemory(-100);
+      assertEquals(1024*1024, stats.getMaxMemory());
 
-        stats.incObjects(100);
-        assertEquals(100, stats.getObjects());
-        stats.incObjects(-100);
-        assertEquals(0, stats.getObjects());
+      stats.incUsedMemory(100);
+      assertEquals(100, stats.getUsedMemory());
+      stats.incUsedMemory(-100);
+      assertEquals(0, stats.getUsedMemory());
 
-        stats.incReads();
-        assertEquals(1, stats.getReads());
+      stats.incObjects(100);
+      assertEquals(100, stats.getObjects());
+      stats.incObjects(-100);
+      assertEquals(0, stats.getObjects());
 
-        stats.setFragmentation(100);
-        assertEquals(100, stats.getFragmentation());
-        stats.setFragmentation(0);
-        assertEquals(0, stats.getFragmentation());
+      stats.incReads();
+      assertEquals(1, stats.getReads());
 
-        stats.setFragments(2);
-        assertEquals(2, stats.getFragments());
-        stats.setFragments(1);
-        assertEquals(1, stats.getFragments());
+      stats.setFragmentation(100);
+      assertEquals(100, stats.getFragmentation());
+      stats.setFragmentation(0);
+      assertEquals(0, stats.getFragmentation());
 
-        stats.setLargestFragment(100);
-        assertEquals(100, stats.getLargestFragment());
-        stats.setLargestFragment(1024*1024);
-        assertEquals(1024*1024, stats.getLargestFragment());
+      stats.setFragments(2);
+      assertEquals(2, stats.getFragments());
+      stats.setFragments(1);
+      assertEquals(1, stats.getFragments());
 
-        boolean originalEnableClockStats = DistributionStats.enableClockStats;
-        DistributionStats.enableClockStats = true;
-        try {
-          long start = stats.startCompaction();
-          while (stats.startCompaction() == start) {
-            Thread.yield();
-          }
-          stats.endCompaction(start);
-          assertEquals(1, stats.getCompactions());
-          assertTrue(stats.getCompactionTime() > 0);
-        } finally {
-          DistributionStats.enableClockStats = originalEnableClockStats;
-        }
+      stats.setLargestFragment(100);
+      assertEquals(100, stats.getLargestFragment());
+      stats.setLargestFragment(1024*1024);
+      assertEquals(1024*1024, stats.getLargestFragment());
 
-        stats.incObjects(100);
-        stats.incUsedMemory(100);
-        stats.setFragmentation(100);
-        OffHeapStorage ohs = (OffHeapStorage) stats;
-        ohs.initialize(new NullOffHeapMemoryStats());
-        assertEquals(0, stats.getFreeMemory());
-        assertEquals(0, stats.getMaxMemory());
-        assertEquals(0, stats.getUsedMemory());
-        assertEquals(0, stats.getCompactions());
-        assertEquals(0, stats.getCompactionTime());
-        assertEquals(0, stats.getFragmentation());
-        assertEquals(0, stats.getFragments());
-        assertEquals(0, stats.getLargestFragment());
-        assertEquals(0, stats.getObjects());
-        assertEquals(0, stats.getReads());
-        System.setProperty(OffHeapStorage.STAY_CONNECTED_ON_OUTOFOFFHEAPMEMORY_PROPERTY,
"true");
-        try {
-          try {
-            ma.allocate(1024*1024+1, null);
-            fail("expected OutOfOffHeapMemoryException");
-          } catch (OutOfOffHeapMemoryException expected) {
-          }
-          assertTrue(c.getDistributedSystem().isConnected());
-          try {
-            ma.allocate(1024*1024+1, null);
-            fail("expected OutOfOffHeapMemoryException");
-          } catch (OutOfOffHeapMemoryException expected) {
-          }
-          assertTrue(c.getDistributedSystem().isConnected());
-        } finally {
-          System.clearProperty(OffHeapStorage.STAY_CONNECTED_ON_OUTOFOFFHEAPMEMORY_PROPERTY);
-        }
-        try {
-          ma.allocate(1024*1024+1, null);
-          fail("expected OutOfOffHeapMemoryException");
-        } catch (OutOfOffHeapMemoryException expected) {
-        }
-        try {
-          ma.allocate(1024*1024+1, null);
-          fail("expected OutOfOffHeapMemoryException");
-        } catch (OutOfOffHeapMemoryException expected) {
+      boolean originalEnableClockStats = DistributionStats.enableClockStats;
+      DistributionStats.enableClockStats = true;
+      try {
+        long start = stats.startCompaction();
+        while (stats.startCompaction() == start) {
+          Thread.yield();
         }
-        Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
-          return !c.getDistributedSystem().isConnected();
-        });
+        stats.endCompaction(start);
+        assertEquals(1, stats.getCompactions());
+        assertTrue(stats.getCompactionTime() > 0);
+      } finally {
+        DistributionStats.enableClockStats = originalEnableClockStats;
+      }
+
+      stats.incObjects(100);
+      stats.incUsedMemory(100);
+      stats.setFragmentation(100);
+      OffHeapStorage ohs = (OffHeapStorage) stats;
+      ohs.initialize(new NullOffHeapMemoryStats());
+      assertEquals(0, stats.getFreeMemory());
+      assertEquals(0, stats.getMaxMemory());
+      assertEquals(0, stats.getUsedMemory());
+      assertEquals(0, stats.getCompactions());
+      assertEquals(0, stats.getCompactionTime());
+      assertEquals(0, stats.getFragmentation());
+      assertEquals(0, stats.getFragments());
+      assertEquals(0, stats.getLargestFragment());
+      assertEquals(0, stats.getObjects());
+      assertEquals(0, stats.getReads());
+
+      OutOfOffHeapMemoryException ex = null;
+      try {
+        ma.allocate(1024*1024+1, null);
+        fail("expected OutOfOffHeapMemoryException");
+      } catch (OutOfOffHeapMemoryException expected) {
+        ex = expected;
+      }
+      verify(ooohml).outOfOffHeapMemory(ex);
+      try {
+        ma.allocate(1024*1024+1, null);
+        fail("expected OutOfOffHeapMemoryException");
+      } catch (OutOfOffHeapMemoryException expected) {
+        ex = expected;
+      }
+      verify(ooohml).outOfOffHeapMemory(ex);
 
+    } finally {
+      System.setProperty(SimpleMemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY, "true");
+      try {
+        ma.close();
       } finally {
-        System.setProperty(SimpleMemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY, "true");
-        try {
-          ma.close();
-        } finally {
-          System.clearProperty(SimpleMemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY);
-        }
+        System.clearProperty(SimpleMemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY);
       }
-   } finally {
-      c.close();
     }
   }
   @Test


Mime
View raw message