@Override
      public Object call() throws Exception {
        HeapMemoryMonitor.setTestDisableMemoryUpdates(false);
        GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
        InternalResourceManager irm = cache.getInternalResourceManager();
        HeapMemoryMonitor hmm = irm.getHeapMonitor();
        hmm.setTestMaxMemoryBytes(100);
        HeapMemoryMonitor.setTestBytesUsedForThresholdSet(1);

        if (isCritical) {
          irm.setCriticalHeapPercentage(95);
        } else {
          irm.setEvictionHeapPercentage(50);
        }

        int previousMemoryStateChangeTolerance = hmm.getMemoryStateChangeTolerance();
        final int criticalBytesUsed = 96;
        final int evictionBytesUsed = 55;
        final int memoryStateChangeTolerance = 3;

        try {
          hmm.setMemoryStateChangeTolerance(memoryStateChangeTolerance);

          for (int i = 0; i < memoryStateChangeTolerance; i++) {
            if (isCritical) {
              hmm.updateStateAndSendEvent(criticalBytesUsed, "test");
              assertFalse(hmm.getState().isCritical());
            } else {
              hmm.updateStateAndSendEvent(evictionBytesUsed, "test");
              assertFalse(hmm.getState().isEviction());
            }
          }
          if (isCritical) {
            // Adding expected strings so we do not fail the
            // test prematurely
            getCache().getLogger().fine(addExpectedExString);
            hmm.updateStateAndSendEvent(criticalBytesUsed, "test");
            assertTrue(hmm.getState().isCritical());
            getCache().getLogger().fine(removeExpectedExString);
            getCache().getLogger().fine(addExpectedBelow);
            final int belowCriticalBytes = 92;
            hmm.updateStateAndSendEvent(belowCriticalBytes, "test");
            getCache().getLogger().fine(removeExpectedBelow);
            assertFalse(hmm.getState().isCritical());
          } else {
            hmm.updateStateAndSendEvent(evictionBytesUsed, "test");
            assertTrue(hmm.getState().isEviction());
            final int belowEvictionBytes = 45;
            hmm.updateStateAndSendEvent(belowEvictionBytes, "test");
            assertFalse(hmm.getState().isEviction());

          }

          HeapMemoryMonitor.setTestDisableMemoryUpdates(true);
        } finally {
          hmm.setMemoryStateChangeTolerance(previousMemoryStateChangeTolerance);
        }
        return null;
      }
Test that a Partitioned Region loader invocation is rejected if the VM with the bucket is in a
critical state.
geode-core/src/distributedTest/java/org/apache/geode/cache/management/MemoryThresholdsDUnitTest.java