Search in sources :

Example 71 with ExpirationAttributes

use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.

the class RegionTestCase method testEntryIdleInvalidate.

/**
   * Tests that an entry in a local region that remains idle for a given amount of time is
   * invalidated.
   */
@Test
public void testEntryIdleInvalidate() throws CacheException, InterruptedException {
    final String name = this.getUniqueName();
    // ms
    final int timeout = 20;
    final String key = "KEY";
    final String value = "VALUE";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
    factory.setEntryIdleTimeout(expire);
    factory.setStatisticsEnabled(true);
    TestCacheListener list = new TestCacheListener() {

        public void afterCreate2(EntryEvent e) {
        }

        public void afterUpdate2(EntryEvent e) {
        }

        public void afterInvalidate2(EntryEvent e) {
        }
    };
    factory.setCacheListener(list);
    RegionAttributes attrs = factory.create();
    Region region = null;
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    try {
        region = createRegion(name, attrs);
        // DebuggerSupport.waitForJavaDebugger(getLogWriter(), "Set breakpoint in
        // invalidate");
        ExpiryTask.suspendExpiration();
        Region.Entry entry = null;
        long tilt;
        try {
            region.create(key, value);
            tilt = System.currentTimeMillis() + timeout;
            assertTrue(list.waitForInvocation(333));
            entry = region.getEntry(key);
            assertNotNull(entry.getValue());
        } finally {
            ExpiryTask.permitExpiration();
        }
        waitForInvalidate(entry, tilt);
        ExpiryTask.suspendExpiration();
        try {
            region.put(key, value);
            tilt = System.currentTimeMillis() + timeout;
            entry = region.getEntry(key);
            assertNotNull(entry.getValue());
        } finally {
            ExpiryTask.permitExpiration();
        }
        waitForInvalidate(entry, tilt);
    } finally {
        System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) Entry(org.apache.geode.cache.Region.Entry) EntryEvent(org.apache.geode.cache.EntryEvent) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 72 with ExpirationAttributes

use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.

the class RegionTestCase method testEntryIdleDestroy.

/**
   * Tests that an entry in a region that remains idle for a given amount of time is destroyed.
   */
@Test
public void testEntryIdleDestroy() throws Exception {
    EntryExpiryTask.expiryTaskListener = new ExpiryCallbacks();
    final String name = this.getUniqueName();
    // ms
    final int timeout = 20;
    final String key = "KEY";
    final String value = "VALUE";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.DESTROY);
    factory.setEntryIdleTimeout(expire);
    factory.setStatisticsEnabled(true);
    TestCacheListener list = new TestCacheListener() {

        public void afterCreate2(EntryEvent e) {
        }

        public void afterDestroy2(EntryEvent e) {
        }
    };
    factory.setCacheListener(list);
    RegionAttributes attrs = factory.create();
    Region region = null;
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    try {
        region = createRegion(name, attrs);
        ExpiryTask.suspendExpiration();
        Region.Entry entry = null;
        long tilt;
        try {
            region.create(key, null);
            tilt = System.currentTimeMillis() + timeout;
            assertTrue(list.wasInvoked());
            entry = region.getEntry(key);
        } finally {
            ExpiryTask.permitExpiration();
        }
        waitForDestroy(entry, tilt);
        assertNull(region.getEntry(key));
        ExpiryTask.suspendExpiration();
        try {
            region.put(key, value);
            tilt = System.currentTimeMillis() + timeout;
            entry = region.getEntry(key);
            assertNotNull(entry.getValue());
        } finally {
            ExpiryTask.permitExpiration();
        }
        waitForDestroy(entry, tilt);
    } finally {
        System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
        EntryExpiryTask.expiryTaskListener = null;
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) Entry(org.apache.geode.cache.Region.Entry) EntryEvent(org.apache.geode.cache.EntryEvent) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 73 with ExpirationAttributes

use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.

the class RegionTestCase method testRegionTtlInvalidate.

/**
   * Tests that a region expires with an invalidation after a given time to live.
   */
@Test
public void testRegionTtlInvalidate() throws CacheException, InterruptedException {
    if (getRegionAttributes().getPartitionAttributes() != null)
        return;
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    final String name = this.getUniqueName();
    vm0.invoke(new CacheSerializableRunnable("testRegionTtlInvalidate") {

        public void run2() throws CacheException {
            // ms
            final int timeout = 22;
            final Object key = "KEY";
            final Object value = "VALUE";
            AttributesFactory factory = new AttributesFactory(getRegionAttributes());
            ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
            factory.setRegionTimeToLive(expire);
            factory.setStatisticsEnabled(true);
            RegionAttributes attrs = factory.create();
            Region region = null;
            Region.Entry entry = null;
            long tilt;
            ExpiryTask.suspendExpiration();
            try {
                System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
                try {
                    region = createRegion(name, attrs);
                    region.put(key, value);
                    region.put("k2", "v2");
                    tilt = System.currentTimeMillis() + timeout;
                    entry = region.getEntry(key);
                    assertNotNull(entry.getValue());
                } finally {
                    System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
                }
            } finally {
                ExpiryTask.permitExpiration();
            }
            waitForInvalidate(entry, tilt, 10);
            waitForInvalidate(region.getEntry("k2"), tilt, 10);
        }
    });
}
Also used : Entry(org.apache.geode.cache.Region.Entry) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheException(org.apache.geode.cache.CacheException) RegionAttributes(org.apache.geode.cache.RegionAttributes) VM(org.apache.geode.test.dunit.VM) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 74 with ExpirationAttributes

use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.

the class RegionTestCase method testRegionIdleDestroy.

/**
   * Tests that a region expires with a destruction after a given idle time.
   */
@Test
public void testRegionIdleDestroy() throws CacheException, InterruptedException {
    if (getRegionAttributes().getPartitionAttributes() != null)
        return;
    final String name = this.getUniqueName();
    // ms
    final int timeout = 22;
    final Object key = "KEY";
    final Object value = "VALUE";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.DESTROY);
    factory.setRegionIdleTimeout(expire);
    factory.setStatisticsEnabled(true);
    RegionAttributes attrs = factory.create();
    Region region = null;
    long tilt;
    ExpiryTask.suspendExpiration();
    try {
        System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
        try {
            region = createRegion(name, attrs);
            region.put(key, value);
            tilt = System.currentTimeMillis() + timeout;
            assertFalse(region.isDestroyed());
        } finally {
            ExpiryTask.permitExpiration();
        }
        waitForRegionDestroy(region, tilt);
    } finally {
        System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 75 with ExpirationAttributes

use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.

the class RegionTestCase method testRegionTtlDestroy.

/**
   * Tests that a region expires with a destruction after a given time to live.
   */
@Test
public void testRegionTtlDestroy() throws CacheException, InterruptedException {
    if (getRegionAttributes().getPartitionAttributes() != null)
        return;
    final String name = this.getUniqueName();
    // ms
    final int timeout = 22;
    final Object key = "KEY";
    final Object value = "VALUE";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.DESTROY);
    factory.setRegionTimeToLive(expire);
    factory.setStatisticsEnabled(true);
    RegionAttributes attrs = factory.create();
    Region region = null;
    long tilt;
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    ExpiryTask.suspendExpiration();
    try {
        try {
            region = createRegion(name, attrs);
            assertFalse(region.isDestroyed());
            tilt = System.currentTimeMillis() + timeout;
            region.put(key, value);
            Region.Entry entry = region.getEntry(key);
            assertNotNull(entry.getValue());
        } finally {
            System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
        }
    } finally {
        ExpiryTask.permitExpiration();
    }
    waitForRegionDestroy(region, tilt);
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) Entry(org.apache.geode.cache.Region.Entry) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Aggregations

ExpirationAttributes (org.apache.geode.cache.ExpirationAttributes)84 Region (org.apache.geode.cache.Region)51 AttributesFactory (org.apache.geode.cache.AttributesFactory)50 Test (org.junit.Test)50 RegionAttributes (org.apache.geode.cache.RegionAttributes)41 LocalRegion (org.apache.geode.internal.cache.LocalRegion)41 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)35 Entry (org.apache.geode.cache.Region.Entry)21 EntryEvent (org.apache.geode.cache.EntryEvent)15 CacheException (org.apache.geode.cache.CacheException)12 AttributesMutator (org.apache.geode.cache.AttributesMutator)11 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)11 Host (org.apache.geode.test.dunit.Host)11 VM (org.apache.geode.test.dunit.VM)11 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)11 Properties (java.util.Properties)10 EntryExpiryTask (org.apache.geode.internal.cache.EntryExpiryTask)10 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)10 Cache (org.apache.geode.cache.Cache)8 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)8