Search in sources :

Example 41 with WaitCriterion

use of org.apache.geode.test.dunit.WaitCriterion in project geode by apache.

the class RegionTestCase method testEntryTtl3.

/**
   * Configure entry expiration with a ttl time. Create an entry and records its scheduled
   * expiration time. Then mutate the region expiration configuration and confirm that the entry's
   * expiration time is rescheduled.
   */
@Test
public void testEntryTtl3() {
    final String name = this.getUniqueName();
    // test no longer waits for this expiration to happen
    // ms
    final int timeout1 = 500 * 1000;
    // ms
    final int timeout2 = 2000 * 1000;
    final String key1 = "KEY1";
    final String value1 = "VALUE1";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    ExpirationAttributes expire1 = new ExpirationAttributes(timeout1, ExpirationAction.INVALIDATE);
    factory.setEntryTimeToLive(expire1);
    factory.setStatisticsEnabled(true);
    TestCacheListener list = new TestCacheListener() {

        public void afterCreate2(EntryEvent e) {
        }

        public void afterUpdate2(EntryEvent e) {
        }

        public void afterInvalidate2(EntryEvent e) {
            eventCount++;
        }
    };
    eventCount = 0;
    factory.addCacheListener(list);
    RegionAttributes attrs = factory.create();
    LocalRegion region;
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    try {
        region = (LocalRegion) createRegion(name, attrs);
    } finally {
        System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
    }
    region.create(key1, value1);
    EntryExpiryTask eet = region.getEntryExpiryTask(key1);
    final long firstExpiryTime = eet.getExpirationTime();
    AttributesMutator mutt = region.getAttributesMutator();
    ExpirationAttributes expire2 = new ExpirationAttributes(timeout2, ExpirationAction.INVALIDATE);
    mutt.setEntryTimeToLive(expire2);
    eet = region.getEntryExpiryTask(key1);
    final long secondExpiryTime = eet.getExpirationTime();
    if ((secondExpiryTime - firstExpiryTime) <= 0) {
        fail("expiration time should have been greater after changing region config from 500 to 2000. firstExpiryTime=" + firstExpiryTime + " secondExpiryTime=" + secondExpiryTime);
    }
    // now set back to be more recent
    mutt = region.getAttributesMutator();
    ExpirationAttributes expire3 = new ExpirationAttributes(timeout1, ExpirationAction.INVALIDATE);
    mutt.setEntryTimeToLive(expire3);
    eet = region.getEntryExpiryTask(key1);
    final long thirdExpiryTime = eet.getExpirationTime();
    assertEquals(firstExpiryTime, thirdExpiryTime);
    // confirm that it still has not expired
    assertEquals(0, eventCount);
    // now set it to a really short time and make sure it expires immediately
    Wait.waitForExpiryClockToChange(region);
    final Region.Entry entry = region.getEntry(key1);
    mutt = region.getAttributesMutator();
    ExpirationAttributes expire4 = new ExpirationAttributes(1, ExpirationAction.INVALIDATE);
    mutt.setEntryTimeToLive(expire4);
    WaitCriterion wc = new WaitCriterion() {

        public boolean done() {
            return fetchEntryValue(entry) == null;
        }

        public String description() {
            return "entry never became invalid";
        }
    };
    Wait.waitForCriterion(wc, 10 * 1000, 10, true);
    WaitCriterion waitForEventCountToBeOne = new WaitCriterion() {

        public boolean done() {
            return eventCount == 1;
        }

        public String description() {
            return "eventCount never became 1";
        }
    };
    Wait.waitForCriterion(waitForEventCountToBeOne, 10 * 1000, 10, true);
    eventCount = 0;
}
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) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) 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) AttributesMutator(org.apache.geode.cache.AttributesMutator) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 42 with WaitCriterion

use of org.apache.geode.test.dunit.WaitCriterion in project geode by apache.

the class SlowRecDUnitTest method forceQueuing.

private void forceQueuing(final Region r) throws CacheException {
    Connection.FORCE_ASYNC_QUEUE = true;
    final DMStats stats = getSystem().getDistributionManager().getStats();
    r.put("forcekey", "forcevalue");
    // wait for the flusher to get its first flush in progress
    WaitCriterion ev = new WaitCriterion() {

        public boolean done() {
            return stats.getAsyncQueueFlushesInProgress() != 0;
        }

        public String description() {
            return "waiting for flushes to start";
        }
    };
    Wait.waitForCriterion(ev, 2 * 1000, 200, true);
}
Also used : DMStats(org.apache.geode.distributed.internal.DMStats) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion)

Example 43 with WaitCriterion

use of org.apache.geode.test.dunit.WaitCriterion in project geode by apache.

the class SlowRecDUnitTest method testTimeoutDisconnect.

/**
   * Make sure that exceeding the async-queue-timeout causes a disconnect.
   * <p>
   * [bruce] This test was disabled when the SlowRecDUnitTest was re-enabled in build.xml in the
   * splitbrainNov07 branch. It had been disabled since June 2006 due to hangs. Some of the tests,
   * like this one, still need work because the periodically (some quite often) fail.
   */
@Test
public void testTimeoutDisconnect() throws Exception {
    final String expected = "org.apache.geode.internal.tcp.ConnectionException: Forced disconnect sent to" + "||java.io.IOException: Broken pipe";
    final String addExpected = "<ExpectedException action=add>" + expected + "</ExpectedException>";
    final String removeExpected = "<ExpectedException action=remove>" + expected + "</ExpectedException>";
    final AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_NO_ACK);
    final Region r = createRootRegion("slowrec", factory.create());
    final DM dm = getSystem().getDistributionManager();
    final DMStats stats = dm.getStats();
    // set others before vm0 connects
    final Set others = dm.getOtherDistributionManagerIds();
    // create receiver in vm0 with queuing enabled
    Properties p = new Properties();
    p.setProperty(ASYNC_DISTRIBUTION_TIMEOUT, "5");
    // 500 ms
    p.setProperty(ASYNC_QUEUE_TIMEOUT, "500");
    doCreateOtherVm(p, true);
    final Object key = "key";
    // 1k
    final int VALUE_SIZE = 1024;
    final byte[] value = new byte[VALUE_SIZE];
    int count = 0;
    long queuedMsgs = stats.getAsyncQueuedMsgs();
    long queueSize = stats.getAsyncQueueSize();
    final long timeoutLimit = System.currentTimeMillis() + 5000;
    getCache().getLogger().info(addExpected);
    try {
        while (stats.getAsyncQueueTimeouts() == 0) {
            r.put(key, value);
            count++;
            if (stats.getAsyncQueueSize() > 0) {
                queuedMsgs = stats.getAsyncQueuedMsgs();
                queueSize = stats.getAsyncQueueSize();
            }
            if (System.currentTimeMillis() > timeoutLimit) {
                fail("should have exceeded async-queue-timeout by now");
            }
        }
        LogWriterUtils.getLogWriter().info("After " + count + " " + VALUE_SIZE + " byte puts slowrec mode kicked in but the queue filled when its size reached " + queueSize + " with " + queuedMsgs + " msgs");
        // make sure we lost a connection to vm0
        WaitCriterion ev = new WaitCriterion() {

            public boolean done() {
                if (dm.getOtherDistributionManagerIds().size() > others.size()) {
                    return false;
                }
                return stats.getAsyncQueueSize() == 0;
            }

            public String description() {
                return "waiting for departure";
            }
        };
        Wait.waitForCriterion(ev, 2 * 1000, 200, true);
    } finally {
        getCache().getLogger().info(removeExpected);
    }
    assertEquals(others, dm.getOtherDistributionManagerIds());
    assertEquals(0, stats.getAsyncQueueSize());
}
Also used : DMStats(org.apache.geode.distributed.internal.DMStats) AttributesFactory(org.apache.geode.cache.AttributesFactory) Set(java.util.Set) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) Region(org.apache.geode.cache.Region) DM(org.apache.geode.distributed.internal.DM) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 44 with WaitCriterion

use of org.apache.geode.test.dunit.WaitCriterion in project geode by apache.

the class SlowRecDUnitTest method testSizeDisconnect.

/**
   * Make sure that exceeding the queue size limit causes a disconnect.
   */
@Test
public void testSizeDisconnect() throws Exception {
    final String expected = "org.apache.geode.internal.tcp.ConnectionException: Forced disconnect sent to" + "||java.io.IOException: Broken pipe";
    final String addExpected = "<ExpectedException action=add>" + expected + "</ExpectedException>";
    final String removeExpected = "<ExpectedException action=remove>" + expected + "</ExpectedException>";
    final AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_NO_ACK);
    final Region r = createRootRegion("slowrec", factory.create());
    final DM dm = getSystem().getDistributionManager();
    final DMStats stats = dm.getStats();
    // set others before vm0 connects
    final Set others = dm.getOtherDistributionManagerIds();
    // create receiver in vm0 with queuing enabled
    Properties p = new Properties();
    p.setProperty(ASYNC_DISTRIBUTION_TIMEOUT, "5");
    // 1 meg
    p.setProperty(ASYNC_MAX_QUEUE_SIZE, "1");
    doCreateOtherVm(p, false);
    final Object key = "key";
    // .1M async-max-queue-size should give us 10 of these 100K
    final int VALUE_SIZE = 1024 * 100;
    // msgs before queue full
    final byte[] value = new byte[VALUE_SIZE];
    int count = 0;
    forceQueuing(r);
    long queuedMsgs = stats.getAsyncQueuedMsgs();
    long queueSize = stats.getAsyncQueueSize();
    getCache().getLogger().info(addExpected);
    try {
        while (stats.getAsyncQueueSizeExceeded() == 0 && stats.getAsyncQueueTimeouts() == 0) {
            r.put(key, value);
            count++;
            if (stats.getAsyncQueueSize() > 0) {
                queuedMsgs = stats.getAsyncQueuedMsgs();
                queueSize = stats.getAsyncQueueSize();
            }
            if (count > 100) {
                fail("should have exceeded max-queue-size by now");
            }
        }
        LogWriterUtils.getLogWriter().info("After " + count + " " + VALUE_SIZE + " byte puts slowrec mode kicked in but the queue filled when its size reached " + queueSize + " with " + queuedMsgs + " msgs");
        // make sure we lost a connection to vm0
        WaitCriterion ev = new WaitCriterion() {

            public boolean done() {
                return dm.getOtherDistributionManagerIds().size() <= others.size() && stats.getAsyncQueueSize() == 0;
            }

            public String description() {
                return "waiting for connection loss";
            }
        };
        Wait.waitForCriterion(ev, 30 * 1000, 200, true);
    } finally {
        forceQueueFlush();
        getCache().getLogger().info(removeExpected);
    }
    assertEquals(others, dm.getOtherDistributionManagerIds());
    assertEquals(0, stats.getAsyncQueueSize());
}
Also used : DMStats(org.apache.geode.distributed.internal.DMStats) AttributesFactory(org.apache.geode.cache.AttributesFactory) Set(java.util.Set) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) Region(org.apache.geode.cache.Region) DM(org.apache.geode.distributed.internal.DM) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 45 with WaitCriterion

use of org.apache.geode.test.dunit.WaitCriterion in project geode by apache.

the class ConcurrentMapOpsDUnitTest method doReplacePutsKeyInLocalClientCacheWork.

private void doReplacePutsKeyInLocalClientCacheWork(final String regionName) {
    Host host = Host.getHost(0);
    VM server = host.getVM(0);
    VM client = host.getVM(2);
    int port1 = createRegionsAndStartServer(server);
    createClientRegion(client, port1, false, -1);
    server.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            final Region r = getCache().getRegion(regionName);
            assertNull(r.putIfAbsent("key0", "value"));
            assertTrue(r.containsKey("key0"));
            assertNull(r.putIfAbsent("key2", "value2"));
            assertTrue(r.containsKey("key2"));
            return null;
        }
    });
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            final Region r = getCache().getRegion(regionName);
            assertEquals("value", r.replace("key0", "newValue"));
            assertTrue(r.containsKeyOnServer("key0"));
            assertTrue(r.containsKey("key0"));
            assertTrue(r.containsValueForKey("key0"));
            assertFalse(r.replace("key2", "DontReplace", "newValue"));
            assertTrue(r.replace("key2", "value2", "newValu2"));
            assertTrue(r.containsKeyOnServer("key2"));
            assertTrue(r.containsKey("key2"));
            assertTrue(r.containsValueForKey("key2"));
            return null;
        }
    });
    // bug #42221 - replace does not put entry on client when server has invalid value
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region r = getCache().getRegion(regionName);
            final String key = "bug42221";
            r.putIfAbsent(key, null);
            assertTrue(r.containsKey(key));
            Object result = r.replace(key, "not null");
            assertEquals(null, result);
            assertTrue(r.containsKey(key));
            assertEquals(r.get(key), "not null");
            // cleanup
            r.remove(key);
            return null;
        }
    });
    // bug #42242 - remove(K,null) doesn't work
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region r = getCache().getRegion(regionName);
            final String key = "bug42242";
            r.putIfAbsent(key, null);
            assertTrue(r.containsKey(key));
            assertTrue(r.containsKeyOnServer(key));
            boolean result = r.remove(key, null);
            assertTrue(result);
            assertFalse(r.containsKey(key));
            assertFalse(r.containsKeyOnServer(key));
            return null;
        }
    });
    // bug #42242b - second scenario with a replace(K,V,V) that didn't work
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region r = getCache().getRegion(regionName);
            final String key = "bug42242b";
            r.putIfAbsent(key, null);
            assertTrue(r.containsKey(key));
            assertTrue(r.containsKeyOnServer(key));
            boolean result = r.replace(key, null, "new value");
            assertTrue(result);
            result = r.remove(key, "new value");
            assertTrue(result);
            assertFalse(r.containsKey(key));
            assertFalse(r.containsKeyOnServer(key));
            return null;
        }
    });
    // bug #42242c - remove does not work for entry that's on the server but not on the client
    final String key = "bug42242c";
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region r = getCache().getRegion(regionName);
            r.registerInterest("ALL_KEYS");
            return null;
        }
    });
    server.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region r = getCache().getRegion(regionName);
            r.putIfAbsent(key, null);
            assertTrue(r.containsKey(key));
            return null;
        }
    });
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            final Region r = getCache().getRegion(regionName);
            WaitCriterion w = new WaitCriterion() {

                public String description() {
                    return "waiting for server operation to reach client";
                }

                public boolean done() {
                    return r.containsKey(key);
                }
            };
            Wait.waitForCriterion(w, 10000, 200, true);
            assertTrue(r.containsKeyOnServer(key));
            boolean result = r.remove(key, null);
            // if (!result) {
            // ((LocalRegion)r).dumpBackingMap();
            // }
            assertTrue(result);
            assertFalse(r.containsKey(key));
            assertFalse(r.containsKeyOnServer(key));
            return null;
        }
    });
}
Also used : WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) InvalidDeltaException(org.apache.geode.InvalidDeltaException) IOException(java.io.IOException) IgnoredException(org.apache.geode.test.dunit.IgnoredException)

Aggregations

WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)368 Test (org.junit.Test)132 Region (org.apache.geode.cache.Region)105 VM (org.apache.geode.test.dunit.VM)96 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)93 Host (org.apache.geode.test.dunit.Host)73 LocalRegion (org.apache.geode.internal.cache.LocalRegion)58 CacheException (org.apache.geode.cache.CacheException)57 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)53 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)50 AttributesFactory (org.apache.geode.cache.AttributesFactory)41 IgnoredException (org.apache.geode.test.dunit.IgnoredException)40 IOException (java.io.IOException)36 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)36 Cache (org.apache.geode.cache.Cache)34 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)34 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)33 Properties (java.util.Properties)31 AsyncInvocation (org.apache.geode.test.dunit.AsyncInvocation)28 Iterator (java.util.Iterator)27