Search in sources :

Example 6 with CacheWriterAdapter

use of org.apache.geode.cache.util.CacheWriterAdapter in project geode by apache.

the class ClientServerTransactionDUnitTest method testCallbacks.

// Disabled due to bug 47083
@Ignore
@Test
public void testCallbacks() {
    Host host = Host.getHost(0);
    VM datastore = host.getVM(1);
    VM client = host.getVM(2);
    int port = createRegionsAndStartServer(datastore, false);
    createClientRegion(client, port, true);
    class ClientTxListener extends TransactionListenerAdapter {

        private boolean afterRollbackInvoked = false;

        boolean afterCommitInvoked = false;

        @Override
        public void afterCommit(TransactionEvent event) {
            afterCommitInvoked = true;
            verifyEvent(event);
        }

        @Override
        public void afterRollback(TransactionEvent event) {
            afterRollbackInvoked = true;
            verifyEvent(event);
        }

        protected void verifyEvent(TransactionEvent event) {
            Iterator it = event.getEvents().iterator();
            int i = 0;
            while (it.hasNext()) {
                EntryEvent ev = (EntryEvent) it.next();
                if (i == 0)
                    assertNull(ev.getNewValue());
                if (i > 1) {
                    assertEquals(new CustId(i), ev.getKey());
                    assertEquals(new Customer("name" + i, "address" + i), ev.getNewValue());
                }
                assertTrue(ev.isOriginRemote());
                i++;
            }
            assertEquals(5, event.getEvents().size());
        }
    }
    class ClientTxWriter implements TransactionWriter {

        boolean txWriterCalled = false;

        public void close() {
        }

        public void beforeCommit(TransactionEvent event) throws TransactionWriterException {
            txWriterCalled = true;
            Iterator it = event.getEvents().iterator();
            int i = 0;
            while (it.hasNext()) {
                EntryEvent ev = (EntryEvent) it.next();
                if (i == 0)
                    assertNull(ev.getNewValue());
                if (i > 1) {
                    assertEquals(new CustId(i), ev.getKey());
                    assertEquals(new Customer("name" + i, "address" + i), ev.getNewValue());
                }
                assertTrue(ev.isOriginRemote());
                i++;
            }
            assertEquals(5, event.getEvents().size());
        }
    }
    class ClientListener extends CacheListenerAdapter {

        boolean invoked = false;

        @Override
        public void afterCreate(EntryEvent event) {
            CustId c = (CustId) event.getKey();
            if (c.getCustId() > 1) {
                invoked = true;
            }
            // we document that client transaction are proxied to the server
            // and that the callback should be handled appropriately (hence true)
            assertTrue(event.isOriginRemote());
            assertTrue(event.isOriginRemote());
        }

        @Override
        public void afterUpdate(EntryEvent event) {
            assertFalse(event.isOriginRemote());
        }
    }
    class ClientWriter extends CacheWriterAdapter {

        @Override
        public void beforeCreate(EntryEvent event) throws CacheWriterException {
            CustId c = (CustId) event.getKey();
            if (c.getCustId() < 2) {
                return;
            }
            fail("cache writer should not be invoked");
        }

        @Override
        public void beforeUpdate(EntryEvent event) throws CacheWriterException {
            fail("cache writer should not be invoked");
        }
    }
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getCache().getCacheTransactionManager();
            mgr.addListener(new ClientTxListener());
            mgr.setWriter(new ClientTxWriter());
            Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
            pr.getAttributesMutator().addCacheListener(new ServerListener());
            pr.getAttributesMutator().setCacheWriter(new ServerWriter());
            return null;
        }
    });
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getCache().getCacheTransactionManager();
            mgr.addListener(new ClientTxListener());
            try {
                mgr.setWriter(new ClientTxWriter());
                fail("expected exception not thrown");
            } catch (IllegalStateException e) {
            }
            Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
            pr.getAttributesMutator().addCacheListener(new ClientListener());
            pr.getAttributesMutator().setCacheWriter(new ClientWriter());
            return null;
        }
    });
    class doOps extends SerializableCallable {

        public doOps(boolean commit) {
            this.commit = commit;
        }

        boolean commit = false;

        public Object call() throws Exception {
            Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
            CacheTransactionManager mgr = getCache().getCacheTransactionManager();
            pr.put(new CustId(0), new Customer("name0", "address0"));
            pr.put(new CustId(1), new Customer("name1", "address1"));
            mgr.begin();
            pr.invalidate(new CustId(0));
            pr.destroy(new CustId(1));
            for (int i = 2; i < 5; i++) {
                pr.put(new CustId(i), new Customer("name" + i, "address" + i));
            }
            if (commit) {
                mgr.commit();
            } else {
                mgr.rollback();
            }
            return null;
        }
    }
    client.invoke(new doOps(false));
    datastore.invoke(new SerializableCallable() {

        @SuppressWarnings("synthetic-access")
        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getCacheTransactionManager();
            ClientTxListener l = (ClientTxListener) mgr.getListeners()[0];
            assertTrue(l.afterRollbackInvoked);
            return null;
        }
    });
    client.invoke(new doOps(true));
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getCacheTransactionManager();
            ClientTxListener l = (ClientTxListener) mgr.getListeners()[0];
            assertTrue(l.afterCommitInvoked);
            ClientTxWriter w = (ClientTxWriter) mgr.getWriter();
            assertTrue(w.txWriterCalled);
            return null;
        }
    });
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getCacheTransactionManager();
            ClientTxListener l = (ClientTxListener) mgr.getListeners()[0];
            assertFalse(l.afterCommitInvoked);
            Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
            ClientListener cl = (ClientListener) pr.getAttributes().getCacheListeners()[0];
            assertTrue(cl.invoked);
            return null;
        }
    });
}
Also used : TransactionListenerAdapter(org.apache.geode.cache.util.TransactionListenerAdapter) Customer(org.apache.geode.internal.cache.execute.data.Customer) CustId(org.apache.geode.internal.cache.execute.data.CustId) Iterator(java.util.Iterator) CacheWriterAdapter(org.apache.geode.cache.util.CacheWriterAdapter) RollbackException(javax.transaction.RollbackException) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) Ignore(org.junit.Ignore) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 7 with CacheWriterAdapter

use of org.apache.geode.cache.util.CacheWriterAdapter in project geode by apache.

the class CacheAdvisorDUnitTest method testNetWriteAdvice.

@Test
public void testNetWriteAdvice() throws Exception {
    final String rgnName = getUniqueName();
    Set expected = new HashSet();
    for (int i = 0; i < vms.length; i++) {
        VM vm = vms[i];
        InternalDistributedMember id = ids[i];
        if (i % 2 == 0) {
            expected.add(id);
        }
        final int index = i;
        vm.invoke(new CacheSerializableRunnable("CacheAdvisorDUnitTest.testNetWriteAdvice") {

            public void run2() throws CacheException {
                AttributesFactory fac = new AttributesFactory();
                if (index % 2 == 0) {
                    fac.setCacheWriter(new CacheWriterAdapter());
                }
                createRegion(rgnName, fac.create());
            }
        });
    }
    RegionAttributes attrs = new AttributesFactory().create();
    DistributedRegion rgn = (DistributedRegion) createRegion(rgnName, attrs);
    assertEquals(expected, rgn.getCacheDistributionAdvisor().adviseNetWrite());
}
Also used : CacheWriterAdapter(org.apache.geode.cache.util.CacheWriterAdapter) HashSet(java.util.HashSet) Set(java.util.Set) AttributesFactory(org.apache.geode.cache.AttributesFactory) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) RegionAttributes(org.apache.geode.cache.RegionAttributes) VM(org.apache.geode.test.dunit.VM) HashSet(java.util.HashSet) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 8 with CacheWriterAdapter

use of org.apache.geode.cache.util.CacheWriterAdapter in project geode by apache.

the class GemcachedBinaryClientJUnitTest method testCacheWriterException.

@SuppressWarnings("unchecked")
public void testCacheWriterException() throws Exception {
    MemcachedClient client = createMemcachedClient();
    assertTrue(client.set("key", 0, "value".getBytes()).get());
    client.set("exceptionkey", 0, "exceptionvalue").get();
    GemFireCacheImpl cache = GemFireCacheImpl.getInstance();
    Region region = cache.getRegion(GemFireMemcachedServer.REGION_NAME);
    region.getAttributesMutator().setCacheWriter(new CacheWriterAdapter() {

        @Override
        public void beforeCreate(EntryEvent event) throws CacheWriterException {
            if (event.getKey().equals(KeyWrapper.getWrappedKey("exceptionkey".getBytes()))) {
                throw new RuntimeException("ExpectedStrings: Cache writer exception");
            }
        }

        @Override
        public void beforeUpdate(EntryEvent event) throws CacheWriterException {
            if (event.getKey().equals(KeyWrapper.getWrappedKey("exceptionkey".getBytes()))) {
                throw new RuntimeException("ExpectedStrings: Cache writer exception");
            }
        }
    });
    long start = System.nanoTime();
    try {
        client.set("exceptionkey", 0, "exceptionvalue").get();
        throw new RuntimeException("expected exception not thrown");
    } catch (ExecutionException e) {
    // expected
    }
    assertTrue(client.set("key2", 0, "value2".getBytes()).get());
}
Also used : CacheWriterAdapter(org.apache.geode.cache.util.CacheWriterAdapter) MemcachedClient(net.spy.memcached.MemcachedClient) EntryEvent(org.apache.geode.cache.EntryEvent) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) Region(org.apache.geode.cache.Region) ExecutionException(java.util.concurrent.ExecutionException) CacheWriterException(org.apache.geode.cache.CacheWriterException)

Example 9 with CacheWriterAdapter

use of org.apache.geode.cache.util.CacheWriterAdapter in project geode by apache.

the class ProxyDUnitTest method remoteOriginOps.

/**
   * check remote ops done in a normal vm are correctly distributed to PROXY regions
   */
private void remoteOriginOps(DataPolicy dp, InterestPolicy ip) throws CacheException {
    initOtherId();
    AttributesFactory af = new AttributesFactory();
    af.setDataPolicy(dp);
    af.setSubscriptionAttributes(new SubscriptionAttributes(ip));
    af.setScope(Scope.DISTRIBUTED_ACK);
    CacheListener cl1 = new CacheListener() {

        public void afterUpdate(EntryEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterCreate(EntryEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterInvalidate(EntryEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterDestroy(EntryEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterRegionInvalidate(RegionEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterRegionDestroy(RegionEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterRegionClear(RegionEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterRegionCreate(RegionEvent e) {
        }

        public void afterRegionLive(RegionEvent e) {
        }

        public void close() {
        }
    };
    af.addCacheListener(cl1);
    Region r = createRootRegion("ProxyDUnitTest", af.create());
    this.clInvokeCount = 0;
    doCreateOtherVm();
    DMStats stats = getDMStats();
    long receivedMsgs = stats.getReceivedMessages();
    if (ip.isAll()) {
        getOtherVm().invoke(new CacheSerializableRunnable("do put") {

            public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                r.put("p", "v");
            }
        });
        assertEquals(1, this.clInvokeCount);
        assertEquals(Operation.CREATE, this.clLastEvent.getOperation());
        assertEquals(true, this.clLastEvent.isOriginRemote());
        assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
        assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
        // failure
        assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
        assertEquals("v", ((EntryEvent) this.clLastEvent).getNewValue());
        assertEquals("p", ((EntryEvent) this.clLastEvent).getKey());
        this.clInvokeCount = 0;
        getOtherVm().invoke(new CacheSerializableRunnable("do create") {

            public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                r.create("c", "v");
            }
        });
        assertEquals(1, this.clInvokeCount);
        assertEquals(Operation.CREATE, this.clLastEvent.getOperation());
        assertEquals(true, this.clLastEvent.isOriginRemote());
        assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
        assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
        assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
        assertEquals("v", ((EntryEvent) this.clLastEvent).getNewValue());
        assertEquals("c", ((EntryEvent) this.clLastEvent).getKey());
        this.clInvokeCount = 0;
        getOtherVm().invoke(new CacheSerializableRunnable("do update") {

            public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                r.put("c", "v2");
            }
        });
        assertEquals(1, this.clInvokeCount);
        assertEquals(Operation.UPDATE, this.clLastEvent.getOperation());
        assertEquals(true, this.clLastEvent.isOriginRemote());
        assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
        assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
        assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
        assertEquals("v2", ((EntryEvent) this.clLastEvent).getNewValue());
        assertEquals("c", ((EntryEvent) this.clLastEvent).getKey());
        this.clInvokeCount = 0;
        getOtherVm().invoke(new CacheSerializableRunnable("do invalidate") {

            public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                r.invalidate("c");
            }
        });
        assertEquals(1, this.clInvokeCount);
        assertEquals(Operation.INVALIDATE, this.clLastEvent.getOperation());
        assertEquals(true, this.clLastEvent.isOriginRemote());
        assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
        assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
        assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
        assertEquals(null, ((EntryEvent) this.clLastEvent).getNewValue());
        assertEquals("c", ((EntryEvent) this.clLastEvent).getKey());
        this.clInvokeCount = 0;
        getOtherVm().invoke(new CacheSerializableRunnable("do destroy") {

            public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                r.destroy("c");
            }
        });
        assertEquals(1, this.clInvokeCount);
        assertEquals(Operation.DESTROY, this.clLastEvent.getOperation());
        assertEquals(true, this.clLastEvent.isOriginRemote());
        assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
        assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
        assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
        assertEquals(null, ((EntryEvent) this.clLastEvent).getNewValue());
        assertEquals("c", ((EntryEvent) this.clLastEvent).getKey());
        this.clInvokeCount = 0;
        getOtherVm().invoke(new CacheSerializableRunnable("do putAll") {

            public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                Map m = new HashMap();
                m.put("putAllKey1", "putAllValue1");
                m.put("putAllKey2", "putAllValue2");
                r.putAll(m);
            }
        });
        assertEquals(2, this.clInvokeCount);
        // @todo darrel; check putAll events
        this.clInvokeCount = 0;
        getOtherVm().invoke(new CacheSerializableRunnable("do netsearch") {

            public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                // total miss
                assertEquals(null, r.get("loadkey"));
            }
        });
        assertEquals(0, this.clInvokeCount);
    } else {
        getOtherVm().invoke(new CacheSerializableRunnable("do entry ops") {

            public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                r.put("p", "v");
                r.create("c", "v");
                // update
                r.put("c", "v");
                r.invalidate("c");
                r.destroy("c");
                {
                    Map m = new HashMap();
                    m.put("putAllKey1", "putAllValue1");
                    m.put("putAllKey2", "putAllValue2");
                    r.putAll(m);
                }
                // total miss
                assertEquals(null, r.get("loadkey"));
            }
        });
        assertEquals(0, this.clInvokeCount);
        assertEquals(0, r.size());
        // check the stats to make sure none of the above sent up messages
        assertEquals(receivedMsgs, stats.getReceivedMessages());
    }
    {
        AttributesMutator am = r.getAttributesMutator();
        CacheLoader cl = new CacheLoader() {

            public Object load(LoaderHelper helper) throws CacheLoaderException {
                if (helper.getKey().equals("loadkey")) {
                    return "loadvalue";
                } else if (helper.getKey().equals("loadexception")) {
                    throw new CacheLoaderException("expected");
                } else {
                    return null;
                }
            }

            public void close() {
            }
        };
        am.setCacheLoader(cl);
    }
    receivedMsgs = stats.getReceivedMessages();
    getOtherVm().invoke(new CacheSerializableRunnable("check net loader") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            // net load
            assertEquals("loadvalue", r.get("loadkey"));
            // total miss
            assertEquals(null, r.get("foobar"));
            try {
                r.get("loadexception");
                fail("expected CacheLoaderException");
            } catch (CacheLoaderException expected) {
            }
        }
    });
    assertTrue(stats.getReceivedMessages() > receivedMsgs);
    if (ip.isAll()) {
        assertEquals(1, this.clInvokeCount);
        assertEquals(Operation.NET_LOAD_CREATE, this.clLastEvent.getOperation());
        assertEquals(true, this.clLastEvent.isOriginRemote());
        assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
        assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
        assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
        this.clInvokeCount = 0;
    } else {
        assertEquals(0, this.clInvokeCount);
    }
    {
        AttributesMutator am = r.getAttributesMutator();
        am.setCacheLoader(null);
        CacheWriter cw = new CacheWriterAdapter() {

            public void beforeCreate(EntryEvent event) throws CacheWriterException {
                throw new CacheWriterException("expected");
            }
        };
        am.setCacheWriter(cw);
    }
    receivedMsgs = stats.getReceivedMessages();
    getOtherVm().invoke(new CacheSerializableRunnable("check net write") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            try {
                r.put("putkey", "putvalue");
                fail("expected CacheWriterException");
            } catch (CacheWriterException expected) {
            }
        }
    });
    assertTrue(stats.getReceivedMessages() > receivedMsgs);
    {
        AttributesMutator am = r.getAttributesMutator();
        am.setCacheWriter(null);
    }
    assertEquals(0, this.clInvokeCount);
    this.clLastEvent = null;
    getOtherVm().invoke(new CacheSerializableRunnable("check region invalidate") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            r.invalidateRegion();
        }
    });
    assertEquals(1, this.clInvokeCount);
    assertEquals(Operation.REGION_INVALIDATE, this.clLastEvent.getOperation());
    assertEquals(true, this.clLastEvent.isOriginRemote());
    assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
    this.clLastEvent = null;
    getOtherVm().invoke(new CacheSerializableRunnable("check region clear") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            r.clear();
        }
    });
    assertEquals(2, this.clInvokeCount);
    assertEquals(Operation.REGION_CLEAR, this.clLastEvent.getOperation());
    assertEquals(true, this.clLastEvent.isOriginRemote());
    assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
    this.clLastEvent = null;
    getOtherVm().invoke(new CacheSerializableRunnable("check region destroy") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            r.destroyRegion();
        }
    });
    assertEquals(3, this.clInvokeCount);
    assertEquals(Operation.REGION_DESTROY, this.clLastEvent.getOperation());
    assertEquals(true, this.clLastEvent.isOriginRemote());
    assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
    assertTrue(r.isDestroyed());
}
Also used : CacheWriterAdapter(org.apache.geode.cache.util.CacheWriterAdapter) DMStats(org.apache.geode.distributed.internal.DMStats) CacheException(org.apache.geode.cache.CacheException) HashMap(java.util.HashMap) RegionEvent(org.apache.geode.cache.RegionEvent) CacheListener(org.apache.geode.cache.CacheListener) LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) EntryEvent(org.apache.geode.cache.EntryEvent) CacheWriter(org.apache.geode.cache.CacheWriter) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) HashMap(java.util.HashMap) Map(java.util.Map) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator) CacheWriterException(org.apache.geode.cache.CacheWriterException)

Example 10 with CacheWriterAdapter

use of org.apache.geode.cache.util.CacheWriterAdapter in project geode by apache.

the class ProxyDUnitTest method distributedOps.

////////////////////// Test Methods //////////////////////
/**
   * check distributed ops that originate in a PROXY are correctly distributed to non-proxy regions.
   */
private void distributedOps(DataPolicy dp, InterestPolicy ip) throws CacheException {
    initOtherId();
    AttributesFactory af = new AttributesFactory();
    af.setDataPolicy(dp);
    af.setSubscriptionAttributes(new SubscriptionAttributes(ip));
    af.setScope(Scope.DISTRIBUTED_ACK);
    Region r = createRootRegion("ProxyDUnitTest", af.create());
    doCreateOtherVm();
    r.put("putkey", "putvalue1");
    getOtherVm().invoke(new CacheSerializableRunnable("check put") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(true, r.containsKey("putkey"));
            assertEquals("putvalue1", r.getEntry("putkey").getValue());
            r.put("putkey", "putvalue2");
        }
    });
    assertEquals(false, r.containsKey("putkey"));
    // netsearch
    assertEquals("putvalue2", r.get("putkey"));
    r.invalidate("putkey");
    getOtherVm().invoke(new CacheSerializableRunnable("check invalidate") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(true, r.containsKey("putkey"));
            assertEquals(null, r.getEntry("putkey").getValue());
        }
    });
    // invalid so total miss
    assertEquals(null, r.get("putkey"));
    r.destroy("putkey");
    getOtherVm().invoke(new CacheSerializableRunnable("check destroy") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(false, r.containsKey("putkey"));
        }
    });
    // total miss
    assertEquals(null, r.get("putkey"));
    r.create("createKey", "createValue1");
    getOtherVm().invoke(new CacheSerializableRunnable("check create") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(true, r.containsKey("createKey"));
            assertEquals("createValue1", r.getEntry("createKey").getValue());
        }
    });
    {
        Map m = new HashMap();
        m.put("putAllKey1", "putAllValue1");
        m.put("putAllKey2", "putAllValue2");
        r.putAll(m, "putAllCallback");
    }
    getOtherVm().invoke(new CacheSerializableRunnable("check putAll") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(true, r.containsKey("putAllKey1"));
            assertEquals("putAllValue1", r.getEntry("putAllKey1").getValue());
            assertEquals(true, r.containsKey("putAllKey2"));
            assertEquals("putAllValue2", r.getEntry("putAllKey2").getValue());
        }
    });
    r.clear();
    getOtherVm().invoke(new CacheSerializableRunnable("check clear") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(0, r.size());
        }
    });
    getOtherVm().invoke(new CacheSerializableRunnable("install CacheWriter") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            AttributesMutator am = r.getAttributesMutator();
            CacheWriter cw = new CacheWriterAdapter() {

                public void beforeCreate(EntryEvent event) throws CacheWriterException {
                    throw new CacheWriterException("expected");
                }
            };
            am.setCacheWriter(cw);
        }
    });
    try {
        r.put("putkey", "putvalue");
        fail("expected CacheWriterException");
    } catch (CacheWriterException expected) {
    }
    getOtherVm().invoke(new CacheSerializableRunnable("check clear") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(0, r.size());
        }
    });
    // total miss
    assertEquals(null, r.get("loadkey"));
    getOtherVm().invoke(new CacheSerializableRunnable("install CacheLoader") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            AttributesMutator am = r.getAttributesMutator();
            // clear csche writer
            am.setCacheWriter(null);
            CacheLoader cl = new CacheLoader() {

                public Object load(LoaderHelper helper) throws CacheLoaderException {
                    if (helper.getKey().equals("loadkey")) {
                        return "loadvalue";
                    } else if (helper.getKey().equals("loadexception")) {
                        throw new CacheLoaderException("expected");
                    } else {
                        return null;
                    }
                }

                public void close() {
                }
            };
            am.setCacheLoader(cl);
        }
    });
    // net load
    assertEquals("loadvalue", r.get("loadkey"));
    // total miss
    assertEquals(null, r.get("foobar"));
    try {
        r.get("loadexception");
        fail("expected CacheLoaderException");
    } catch (CacheLoaderException expected) {
    }
    r.destroyRegion();
    getOtherVm().invoke(new CacheSerializableRunnable("check clear") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(null, r);
        }
    });
}
Also used : CacheWriterAdapter(org.apache.geode.cache.util.CacheWriterAdapter) CacheException(org.apache.geode.cache.CacheException) HashMap(java.util.HashMap) LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) EntryEvent(org.apache.geode.cache.EntryEvent) CacheWriter(org.apache.geode.cache.CacheWriter) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) HashMap(java.util.HashMap) Map(java.util.Map) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator) CacheWriterException(org.apache.geode.cache.CacheWriterException)

Aggregations

CacheWriterAdapter (org.apache.geode.cache.util.CacheWriterAdapter)13 CacheWriterException (org.apache.geode.cache.CacheWriterException)9 EntryEvent (org.apache.geode.cache.EntryEvent)9 Test (org.junit.Test)8 AttributesFactory (org.apache.geode.cache.AttributesFactory)7 Region (org.apache.geode.cache.Region)6 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)4 Properties (java.util.Properties)3 CacheException (org.apache.geode.cache.CacheException)3 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)3 CacheWriter (org.apache.geode.cache.CacheWriter)3 RegionAttributes (org.apache.geode.cache.RegionAttributes)3 RegionEvent (org.apache.geode.cache.RegionEvent)3 CacheListenerAdapter (org.apache.geode.cache.util.CacheListenerAdapter)3 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)3 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 RollbackException (javax.transaction.RollbackException)2