Search in sources :

Example 36 with WaitCriterion

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

the class CertifiableTestCacheListener method waitForUpdated.

public boolean waitForUpdated(final Object key) {
    WaitCriterion ev = new WaitCriterion() {

        public boolean done() {
            return CertifiableTestCacheListener.this.updates.contains(key);
        }

        public String description() {
            return "Waiting for key update: " + key;
        }
    };
    Wait.waitForCriterion(ev, MAX_TIME, 200, true);
    return true;
}
Also used : WaitCriterion(org.apache.geode.test.dunit.WaitCriterion)

Example 37 with WaitCriterion

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

the class CertifiableTestCacheListener method waitForInvalidated.

public boolean waitForInvalidated(final Object key) {
    WaitCriterion ev = new WaitCriterion() {

        public boolean done() {
            return CertifiableTestCacheListener.this.invalidates.contains(key);
        }

        public String description() {
            return "Waiting for key invalidate: " + key;
        }
    };
    Wait.waitForCriterion(ev, MAX_TIME, 200, true);
    return true;
}
Also used : WaitCriterion(org.apache.geode.test.dunit.WaitCriterion)

Example 38 with WaitCriterion

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

the class SlowRecDUnitTest method checkLastValueInOtherVm.

private void checkLastValueInOtherVm(final String lastValue, final Object lcb) {
    VM vm = getOtherVm();
    vm.invoke(new CacheSerializableRunnable("check last value") {

        public void run2() throws CacheException {
            Region r1 = getRootRegion("slowrec");
            if (lcb != null) {
                WaitCriterion ev = new WaitCriterion() {

                    public boolean done() {
                        return lcb.equals(lastCallback);
                    }

                    public String description() {
                        return "waiting for callback";
                    }
                };
                Wait.waitForCriterion(ev, 50 * 1000, 200, true);
                assertEquals(lcb, lastCallback);
            }
            if (lastValue == null) {
                final Region r = r1;
                WaitCriterion ev = new WaitCriterion() {

                    public boolean done() {
                        return r.getEntry("key") == null;
                    }

                    public String description() {
                        return "waiting for key to become null";
                    }
                };
                Wait.waitForCriterion(ev, 50 * 1000, 200, true);
                assertEquals(null, r1.getEntry("key"));
            } else if (CHECK_INVALID.equals(lastValue)) {
                // should be invalid
                {
                    final Region r = r1;
                    WaitCriterion ev = new WaitCriterion() {

                        public boolean done() {
                            Entry e = r.getEntry("key");
                            if (e == null) {
                                return false;
                            }
                            return e.getValue() == null;
                        }

                        public String description() {
                            return "waiting for invalidate";
                        }
                    };
                    Wait.waitForCriterion(ev, 50 * 1000, 200, true);
                }
            } else {
                {
                    int retryCount = 1000;
                    Region.Entry re = null;
                    Object value = null;
                    while (retryCount-- > 0) {
                        re = r1.getEntry("key");
                        if (re != null) {
                            value = re.getValue();
                            if (value != null && value.equals(lastValue)) {
                                break;
                            }
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException ignore) {
                            fail("interrupted");
                        }
                    }
                    assertNotNull(re);
                    assertNotNull(value);
                    assertEquals(lastValue, value);
                }
            }
        }
    });
}
Also used : Entry(org.apache.geode.cache.Region.Entry) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region)

Example 39 with WaitCriterion

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

the class RegionReliabilityTestCase method testLimitedAccessWithLocalEntryExpiration.

/**
   * Tests affect of LIMITED_ACCESS on local entry expiration actions.
   */
@Test
public void testLimitedAccessWithLocalEntryExpiration() throws Exception {
    final String name = this.getUniqueName();
    final String roleA = name + "-A";
    // assign names to 4 vms...
    final String[] requiredRoles = { roleA };
    Set requiredRolesSet = new HashSet();
    for (int i = 0; i < requiredRoles.length; i++) {
        requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
    }
    assertEquals(requiredRoles.length, requiredRolesSet.size());
    // connect controller to system...
    Properties config = new Properties();
    config.setProperty(ROLES, "");
    getSystem(config);
    getCache();
    // create region in controller...
    MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.LIMITED_ACCESS, ResumptionAction.NONE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(getRegionScope());
    fac.setStatisticsEnabled(true);
    RegionAttributes attr = fac.create();
    final Region region = createExpiryRootRegion(name, attr);
    // wait for memberTimeout to expire
    waitForMemberTimeout();
    // use vm1 to create role
    Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Create Region") {

        public void run2() throws CacheException {
            createConnection(new String[] { roleA });
            AttributesFactory fac = new AttributesFactory();
            fac.setScope(getRegionScope());
            RegionAttributes attr = fac.create();
            createRootRegion(name, attr);
        }
    });
    // test to make sure expiration is suspended
    region.put("expireMe", "expireMe");
    assertTrue(region.size() == 1);
    Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Close Region") {

        public void run2() throws CacheException {
            Region region = getRootRegion(name);
            region.close();
        }
    });
    // TODO: waitForMemberTimeout(); ?
    // set expiration and sleep
    AttributesMutator mutator = region.getAttributesMutator();
    mutator.setEntryTimeToLive(new ExpirationAttributes(1, ExpirationAction.LOCAL_DESTROY));
    WaitCriterion wc1 = new WaitCriterion() {

        public boolean done() {
            return ((LocalRegion) region).basicEntries(false).size() == 0;
        }

        public String description() {
            return "expected zero entries but have " + ((LocalRegion) region).basicEntries(false).size();
        }
    };
    Wait.waitForCriterion(wc1, 30 * 1000, 10, true);
    // create region again
    Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Create Region") {

        public void run2() throws CacheException {
            AttributesFactory fac = new AttributesFactory();
            fac.setScope(getRegionScope());
            RegionAttributes attr = fac.create();
            createRootRegion(name, attr);
        }
    });
    region.put("expireMe", "expireMe");
    waitForEntryDestroy(region, "expireMe");
    assertTrue(region.size() == 0);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) RegionAttributes(org.apache.geode.cache.RegionAttributes) CacheException(org.apache.geode.cache.CacheException) LocalRegion(org.apache.geode.internal.cache.LocalRegion) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) AttributesFactory(org.apache.geode.cache.AttributesFactory) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) HashSet(java.util.HashSet) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 40 with WaitCriterion

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

the class RequiredRolesDUnitTest method testWaitForRequiredRoles.

/**
   * Tests RequiredRoles.waitForRequiredRoles().
   */
@Test
public void testWaitForRequiredRoles() throws Exception {
    final String name = this.getUniqueName();
    final int vm0 = 0;
    final int vm1 = 1;
    final int vm2 = 2;
    final int vm3 = 3;
    final String roleA = name + "-A";
    final String roleC = name + "-C";
    final String roleD = name + "-D";
    // assign names to 4 vms...
    final String[] requiredRoles = { roleA, roleC, roleD };
    final String[] rolesProp = { "", roleA, roleA, roleC + "," + roleD };
    final String[][] vmRoles = new String[][] { {}, { roleA }, { roleA }, { roleC, roleD } };
    for (int i = 0; i < vmRoles.length; i++) {
        final int vm = i;
        Host.getHost(0).getVM(vm).invoke(new SerializableRunnable() {

            public void run() {
                Properties config = new Properties();
                config.setProperty(ROLES, rolesProp[vm]);
                getSystem(config);
            }
        });
    }
    // connect controller to system...
    Properties config = new Properties();
    config.setProperty(ROLES, "");
    getSystem(config);
    // create region in controller...
    MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.FULL_ACCESS, ResumptionAction.NONE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(Scope.DISTRIBUTED_ACK);
    RegionAttributes attr = fac.create();
    final Region region = createRootRegion(name, attr);
    // wait for memberTimeout to expire
    waitForMemberTimeout();
    // assert in state of role loss... test all are missing according to RequiredRoles
    assertMissingRoles(name, requiredRoles);
    // create thread to call waitForRequiredRoles
    Runnable runWaitForRequiredRoles = new Runnable() {

        public void run() {
            startTestWaitForRequiredRoles = true;
            try {
                rolesTestWaitForRequiredRoles = RequiredRoles.waitForRequiredRoles(region, -1);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                failTestWaitForRequiredRoles = true;
            }
            finishTestWaitForRequiredRoles = true;
        }
    };
    // assert thread is waiting
    Thread threadA = new Thread(group, runWaitForRequiredRoles);
    threadA.start();
    WaitCriterion ev = new WaitCriterion() {

        public boolean done() {
            return RequiredRolesDUnitTest.this.startTestWaitForRequiredRoles;
        }

        public String description() {
            return "waiting for test start";
        }
    };
    Wait.waitForCriterion(ev, 60 * 1000, 200, true);
    assertTrue(this.startTestWaitForRequiredRoles);
    assertFalse(this.finishTestWaitForRequiredRoles);
    // create region in vms and assert impact on threadA
    SerializableRunnable create = new CacheSerializableRunnable("Create Region") {

        public void run2() throws CacheException {
            AttributesFactory fac = new AttributesFactory();
            fac.setScope(Scope.DISTRIBUTED_ACK);
            RegionAttributes attr = fac.create();
            createRootRegion(name, attr);
        }
    };
    // create region in vm0... no gain for no role
    Host.getHost(0).getVM(vm0).invoke(create);
    assertFalse(this.finishTestWaitForRequiredRoles);
    // create region in vm1... gain for 1st instance of redundant role
    Host.getHost(0).getVM(vm1).invoke(create);
    assertFalse(this.finishTestWaitForRequiredRoles);
    // create region in vm2... no gain for 2nd instance of redundant role
    Host.getHost(0).getVM(vm2).invoke(create);
    assertFalse(this.finishTestWaitForRequiredRoles);
    // create region in vm3... gain for 2 roles
    Host.getHost(0).getVM(vm3).invoke(create);
    ThreadUtils.join(threadA, 30 * 1000);
    assertTrue(this.finishTestWaitForRequiredRoles);
    assertTrue(this.rolesTestWaitForRequiredRoles.isEmpty());
    // assert loss is fired...
    SerializableRunnable destroy = new CacheSerializableRunnable("Destroy Region") {

        public void run2() throws CacheException {
            Region region = getRootRegion(name);
            region.localDestroyRegion();
        }
    };
    // destroy region in vm0... no loss of any role
    Host.getHost(0).getVM(vm0).invoke(destroy);
    // assert new call to RequiredRoles doesn't wait (no role in vm0)
    this.startTestWaitForRequiredRoles = false;
    this.finishTestWaitForRequiredRoles = false;
    threadA = new Thread(group, runWaitForRequiredRoles);
    threadA.start();
    ThreadUtils.join(threadA, 30 * 1000);
    assertTrue(this.startTestWaitForRequiredRoles);
    assertTrue(this.finishTestWaitForRequiredRoles);
    assertTrue(this.rolesTestWaitForRequiredRoles.isEmpty());
    // destroy region in vm1... nothing happens in 1st removal of redundant role
    Host.getHost(0).getVM(vm1).invoke(destroy);
    // assert new call to RequiredRoles doesn't wait (redundant role in vm1)
    this.startTestWaitForRequiredRoles = false;
    this.finishTestWaitForRequiredRoles = false;
    threadA = new Thread(group, runWaitForRequiredRoles);
    threadA.start();
    ThreadUtils.join(threadA, 30 * 1000);
    assertTrue(this.startTestWaitForRequiredRoles);
    assertTrue(this.finishTestWaitForRequiredRoles);
    assertTrue(this.rolesTestWaitForRequiredRoles.isEmpty());
    // destroy region in vm2... 2nd removal of redundant role is loss
    Host.getHost(0).getVM(vm2).invoke(destroy);
    // assert new call to RequiredRoles does wait (lost role in vm2)
    this.startTestWaitForRequiredRoles = false;
    this.finishTestWaitForRequiredRoles = false;
    threadA = new Thread(group, runWaitForRequiredRoles);
    threadA.start();
    // assert thread is waiting
    ev = new WaitCriterion() {

        public boolean done() {
            return RequiredRolesDUnitTest.this.startTestWaitForRequiredRoles;
        }

        public String description() {
            return "waiting for test start";
        }
    };
    Wait.waitForCriterion(ev, 60 * 1000, 200, true);
    assertTrue(this.startTestWaitForRequiredRoles);
    assertFalse(this.finishTestWaitForRequiredRoles);
    assertMissingRoles(name, vmRoles[vm2]);
    // end the wait and make sure no roles are missing
    Host.getHost(0).getVM(vm2).invoke(create);
    ThreadUtils.join(threadA, 30 * 1000);
    assertTrue(this.startTestWaitForRequiredRoles);
    assertTrue(this.finishTestWaitForRequiredRoles);
    assertTrue(this.rolesTestWaitForRequiredRoles.isEmpty());
    assertMissingRoles(name, new String[] {});
    assertFalse(failTestWaitForRequiredRoles);
}
Also used : RegionAttributes(org.apache.geode.cache.RegionAttributes) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) AttributesFactory(org.apache.geode.cache.AttributesFactory) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Region(org.apache.geode.cache.Region) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

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