Search in sources :

Example 46 with Entry

use of org.apache.geode.cache.Region.Entry in project geode by apache.

the class RegionTestCase method testLocalInvalidateRegion.

/**
   * Tests locally invalidating an entire region
   */
@Test
public void testLocalInvalidateRegion() throws CacheException {
    String name = this.getUniqueName();
    Region region = createRegion(name);
    region.put("A", "a");
    region.put("B", "b");
    region.put("C", "c");
    boolean isKV = getRegionAttributes().getMirrorType().isKeysValues();
    try {
        region.localInvalidateRegion();
        if (isKV)
            fail("Should have thrown an IllegalStateException");
    } catch (IllegalStateException e) {
        if (!isKV)
            throw e;
        else
            // abort test
            return;
    }
    Region.Entry entry;
    entry = region.getEntry("A");
    assertNotNull(entry);
    assertNull(entry.getValue());
    entry = region.getEntry("B");
    assertNotNull(entry);
    assertNull(entry.getValue());
    entry = region.getEntry("C");
    assertNotNull(entry);
    assertNull(entry.getValue());
}
Also used : Entry(org.apache.geode.cache.Region.Entry) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 47 with Entry

use of org.apache.geode.cache.Region.Entry 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 48 with Entry

use of org.apache.geode.cache.Region.Entry in project geode by apache.

the class RegionTestCase method testLocalInvalidateEntry.

/**
   * Tests locally invalidating a region entry
   */
@Test
public void testLocalInvalidateEntry() throws CacheException {
    if (!supportsLocalDestroyAndLocalInvalidate()) {
        return;
    }
    String name = this.getUniqueName();
    Object key = "KEY";
    Object value = "VALUE";
    Region region = createRegion(name);
    region.put(key, value);
    Region.Entry entry = region.getEntry(key);
    boolean isMirrorKeysValues = getRegionAttributes().getMirrorType().isKeysValues();
    try {
        region.localInvalidate(key);
        if (isMirrorKeysValues)
            fail("Should have thrown an IllegalStateException");
    } catch (IllegalStateException e) {
        if (!isMirrorKeysValues)
            throw e;
        else
            // abort test
            return;
    }
    assertNull(entry.getValue());
    assertNull(region.get(key));
}
Also used : Entry(org.apache.geode.cache.Region.Entry) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 49 with Entry

use of org.apache.geode.cache.Region.Entry in project geode by apache.

the class RegionTestCase method testDestroyEntry.

/**
   * Tests {@link Region#destroy destroying} an entry and attempting to access it afterwards.
   */
@Test
public void testDestroyEntry() throws CacheException {
    String name = this.getUniqueName();
    Object key = name;
    Object value = new Integer(42);
    Region region = createRegion(name);
    try {
        region.destroy(key);
        fail("Should have thrown an EntryNotFoundException");
    } catch (EntryNotFoundException ex) {
    // pass...
    }
    region.put(key, value);
    Region.Entry entry = region.getEntry(key);
    assertNotNull(entry);
    region.destroy(key);
    Region.Entry entry2 = region.getEntry(key);
    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("Found entry for destroyed key: " + entry2);
    assertNull(entry2);
    if (entry.isLocal()) {
        assertTrue(entry.isDestroyed());
    } else {
        assertFalse(entry.isDestroyed());
    }
    assertEquals(0, region.keySet().size());
    if (entry.isLocal()) {
        try {
            entry.getKey();
            fail("Should have thrown an EntryDestroyedException");
        } catch (EntryDestroyedException ex) {
        // pass...
        }
        try {
            entry.getRegion();
            fail("Should have thrown an EntryDestroyedException");
        } catch (EntryDestroyedException ex) {
        // pass...
        }
        try {
            entry.getStatistics();
            fail("Should have thrown an EntryDestroyedException");
        } catch (EntryDestroyedException ex) {
        // pass...
        }
        try {
            entry.getUserAttribute();
            fail("Should have thrown an EntryDestroyedException");
        } catch (EntryDestroyedException ex) {
        // pass...
        }
        try {
            entry.setUserAttribute("blah");
            fail("Should have thrown an EntryDestroyedException");
        } catch (EntryDestroyedException ex) {
        // pass...
        }
        try {
            entry.getValue();
            fail("Should have thrown an EntryDestroyedException");
        } catch (EntryDestroyedException ex) {
        // pass...
        }
    }
}
Also used : EntryDestroyedException(org.apache.geode.cache.EntryDestroyedException) Entry(org.apache.geode.cache.Region.Entry) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 50 with Entry

use of org.apache.geode.cache.Region.Entry 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)

Aggregations

Entry (org.apache.geode.cache.Region.Entry)67 Test (org.junit.Test)52 Region (org.apache.geode.cache.Region)51 LocalRegion (org.apache.geode.internal.cache.LocalRegion)37 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)37 AttributesFactory (org.apache.geode.cache.AttributesFactory)22 ExpirationAttributes (org.apache.geode.cache.ExpirationAttributes)21 RegionAttributes (org.apache.geode.cache.RegionAttributes)21 VM (org.apache.geode.test.dunit.VM)13 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)13 EntryEvent (org.apache.geode.cache.EntryEvent)12 NonTXEntry (org.apache.geode.internal.cache.LocalRegion.NonTXEntry)12 VersionTag (org.apache.geode.internal.cache.versions.VersionTag)12 Host (org.apache.geode.test.dunit.Host)11 VersionSource (org.apache.geode.internal.cache.versions.VersionSource)10 VersionStamp (org.apache.geode.internal.cache.versions.VersionStamp)10 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)10 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)9 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)8 Iterator (java.util.Iterator)7