Search in sources :

Example 76 with AttributesFactory

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

the class RegionTestCase method testCustomEntryIdleReset.

/**
   * Verify that a get or put resets the idle time on an entry
   */
@Test
public void testCustomEntryIdleReset() {
    final String name = this.getUniqueName();
    // ms
    final int timeout = 200 * 1000;
    final String key1 = "KEY1";
    final String value = "VALUE";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
    // factory.setEntryIdleTimeout(expire);
    factory.setCustomEntryIdleTimeout(new TestExpiry(key1, expire));
    factory.setStatisticsEnabled(true);
    TestCacheListener list = new TestCacheListener() {

        public void afterCreate2(EntryEvent e) {
        }

        public void afterUpdate2(EntryEvent e) {
        }

        public void afterInvalidate2(EntryEvent e) {
        }
    };
    factory.addCacheListener(list);
    RegionAttributes attrs = factory.create();
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    try {
        LocalRegion region = (LocalRegion) createRegion(name, attrs);
        // DebuggerSupport.waitForJavaDebugger(getLogWriter(), "Set breakpoint in invalidate");
        ExpiryTask.suspendExpiration();
        try {
            region.create(key1, value);
            assertTrue(list.waitForInvocation(5000));
            Region.Entry entry = region.getEntry(key1);
            assertNotNull(entry.getValue());
            EntryExpiryTask eet = region.getEntryExpiryTask(key1);
            final long createExpiryTime = eet.getExpirationTime();
            Wait.waitForExpiryClockToChange(region);
            region.get(key1);
            assertSame(eet, region.getEntryExpiryTask(key1));
            final long getExpiryTime = eet.getExpirationTime();
            if (getExpiryTime - createExpiryTime <= 0L) {
                fail("get did not reset the expiration time. createExpiryTime=" + createExpiryTime + " getExpiryTime=" + getExpiryTime);
            }
            Wait.waitForExpiryClockToChange(region);
            region.put(key1, value);
            assertSame(eet, region.getEntryExpiryTask(key1));
            final long putExpiryTime = eet.getExpirationTime();
            if (putExpiryTime - getExpiryTime <= 0L) {
                fail("put did not reset the expiration time. getExpiryTime=" + getExpiryTime + " putExpiryTime=" + putExpiryTime);
            }
        } finally {
            ExpiryTask.permitExpiration();
        }
    } finally {
        System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
    }
}
Also used : EntryExpiryTask(org.apache.geode.internal.cache.EntryExpiryTask) RegionAttributes(org.apache.geode.cache.RegionAttributes) LocalRegion(org.apache.geode.internal.cache.LocalRegion) AttributesFactory(org.apache.geode.cache.AttributesFactory) 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 77 with AttributesFactory

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

the class RegionTestCase method testCustomEntryTtl1.

/**
   * Verify that special entries expire but other entries in the region don't
   */
@Test
public void testCustomEntryTtl1() {
    final String name = this.getUniqueName();
    // ms!
    final int timeout = 20;
    final String key1 = "KEY1";
    final String key2 = "KEY2";
    final String value = "VALUE";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
    // factory.setEntryTimeToLive(expire);
    factory.setCustomEntryTimeToLive(new TestExpiry(key2, expire));
    factory.setStatisticsEnabled(true);
    RegionAttributes attrs = factory.create();
    Region region = null;
    /**
     * Crank up the expiration so test runs faster. This property only needs to be set while the
     * region is created
     */
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    try {
        region = createRegion(name, attrs);
    } finally {
        System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
    }
    // Random values should not expire
    region.put(key1, value);
    Wait.pause(timeout * 2);
    assert (region.get(key1).equals(value));
    // key2 *should* expire
    ExpiryTask.suspendExpiration();
    Region.Entry entry = null;
    long tilt;
    try {
        region.put(key2, value);
        tilt = System.currentTimeMillis() + timeout;
        entry = region.getEntry(key2);
        assertNotNull(entry.getValue());
    } finally {
        ExpiryTask.permitExpiration();
    }
    waitForInvalidate(entry, tilt);
    assert (region.get(key1).equals(value));
}
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)

Example 78 with AttributesFactory

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

the class RemoveAllMultiVmDUnitTest method createCache.

public static void createCache() {
    try {
        ds = (new RemoveAllMultiVmDUnitTest()).getSystem(props);
        cache = CacheFactory.create(ds);
        AttributesFactory factory = new AttributesFactory();
        factory.setScope(Scope.DISTRIBUTED_ACK);
        RegionAttributes attr = factory.create();
        region = cache.createRegion("map", attr);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) CacheException(org.apache.geode.cache.CacheException)

Example 79 with AttributesFactory

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

the class RegionTestCase method testCloseRegion.

/**
   * Tests closing a region, and checks different behavior when this is a disk region with
   * persistBackup.
   */
@Test
public void testCloseRegion() throws CacheException {
    // @todo added a remote region to make sure close just does a localDestroy
    String name = this.getUniqueName();
    AttributesFactory fac = new AttributesFactory(getRegionAttributes());
    TestCacheListener list = new TestCacheListener() {

        public void afterCreate2(EntryEvent event) {
        // do nothing
        }

        public void afterRegionDestroy2(RegionEvent re) {
            assertEquals(Operation.REGION_CLOSE, re.getOperation());
        }

        public void close2() {
        // okay
        }
    };
    fac.setCacheListener(list);
    RegionAttributes attrs = fac.create();
    Region region = createRegion(name, attrs);
    File diskDir = null;
    if (attrs.getDataPolicy().withPersistence()) {
        diskDir = getCache().findDiskStore(attrs.getDiskStoreName()).getDiskDirs()[0];
        // @todo We no longer start with a clean slate because the DiskStore hangs around.
        // If we want a clean slate then we need to destroy the DiskStore after each
        // test completes.
        // assert that if this is a disk region, the disk dirs are empty
        // to make sure we start with a clean slate
        getCache().getLogger().info("list=" + Arrays.toString(diskDir.list()));
    // assertIndexDetailsEquals("list="+Arrays.toString(diskDir.list()),
    // 0, diskDir.list().length);
    }
    for (int i = 0; i < 1000; i++) {
        region.put(new Integer(i), String.valueOf(i));
    }
    // reset wasInvoked after creates
    assertTrue(list.wasInvoked());
    // assert that if this is a disk region, the disk dirs are not empty
    if (attrs.getDataPolicy().withPersistence()) {
        assertTrue(diskDir.list().length > 0);
    }
    boolean persistent = region.getAttributes().getDataPolicy().withPersistence();
    region.close();
    // assert that if this is a disk region, the disk dirs are not empty
    if (attrs.getDataPolicy().withPersistence()) {
        assertTrue(diskDir.list().length > 0);
    }
    assertTrue(list.waitForInvocation(333));
    assertTrue(list.isClosed());
    assertTrue(region.isDestroyed());
    // if (persistent) {
    // // remove this when bug #41049 is fixed
    // return;
    // }
    // if this is a disk region, then check to see if recreating the region
    // repopulates with data
    region = createRegion(name, attrs);
    if (attrs.getDataPolicy().withPersistence()) {
        for (int i = 0; i < 1000; i++) {
            Region.Entry entry = region.getEntry(new Integer(i));
            assertNotNull("entry " + i + " not found", entry);
            assertEquals(String.valueOf(i), entry.getValue());
        }
        assertEquals(1000, region.keySet().size());
    } else {
        assertEquals(0, region.keySet().size());
    }
    region.localDestroyRegion();
}
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) RegionEvent(org.apache.geode.cache.RegionEvent) File(java.io.File) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 80 with AttributesFactory

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

the class SearchAndLoadDUnitTest method testNetSearch.

@Test
public void testNetSearch() throws CacheException, InterruptedException {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    final String name = this.getUniqueName() + "-ACK";
    final String objectName = "NetSearchKey";
    final Integer value = new Integer(440);
    vm0.invoke(new SerializableRunnable("Create ACK Region") {

        public void run() {
            try {
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                factory.setEarlyAck(false);
                factory.setStatisticsEnabled(true);
                Region region = createRegion(name, factory.create());
                region.create(objectName, null);
            } catch (CacheException ex) {
                Assert.fail("While creating ACK region", ex);
            }
        }
    });
    vm1.invoke(new SerializableRunnable("Create ACK Region") {

        public void run() {
            try {
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                factory.setEarlyAck(false);
                factory.setStatisticsEnabled(true);
                Region region = createRegion(name, factory.create());
                region.put(objectName, value);
            } catch (CacheException ex) {
                Assert.fail("While creating ACK region", ex);
            }
        }
    });
    vm2.invoke(new SerializableRunnable("Create ACK Region") {

        public void run() {
            try {
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                factory.setEarlyAck(false);
                factory.setStatisticsEnabled(true);
                Region region = createRegion(name, factory.create());
                region.create(objectName, null);
            } catch (CacheException ex) {
                Assert.fail("While creating ACK region", ex);
            }
        }
    });
    vm0.invoke(new SerializableRunnable("Get a value") {

        public void run() {
            try {
                Object result = null;
                result = getRootRegion().getSubregion(name).get(objectName);
                assertEquals(value, result);
            // System.err.println("Results is " + result.toString() + " Key is " +
            // objectName.toString());
            } catch (CacheLoaderException cle) {
                Assert.fail("While Get a value", cle);
            } catch (TimeoutException te) {
                Assert.fail("While Get a value", te);
            }
        }
    });
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheException(org.apache.geode.cache.CacheException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) VM(org.apache.geode.test.dunit.VM) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) TimeoutException(org.apache.geode.cache.TimeoutException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

AttributesFactory (org.apache.geode.cache.AttributesFactory)1156 Region (org.apache.geode.cache.Region)565 Test (org.junit.Test)550 RegionAttributes (org.apache.geode.cache.RegionAttributes)471 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)468 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)356 VM (org.apache.geode.test.dunit.VM)304 Host (org.apache.geode.test.dunit.Host)288 Properties (java.util.Properties)244 CacheException (org.apache.geode.cache.CacheException)243 Cache (org.apache.geode.cache.Cache)229 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)206 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)201 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)199 LocalRegion (org.apache.geode.internal.cache.LocalRegion)173 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)156 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)139 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)129 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)126 IgnoredException (org.apache.geode.test.dunit.IgnoredException)125