Search in sources :

Example 26 with LoaderHelper

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

the class LocalRegionDUnitTest method testLocalCreateModifiedCallbackArgument.

/**
   * Tests that a local writer receives a modified version of the callback argument on a create.
   */
@Test
public void testLocalCreateModifiedCallbackArgument() throws CacheException {
    final String name = this.getUniqueName();
    final Object key = "KEY";
    final Object value = "VALUE";
    final Object one = "ONE";
    final Object two = "TWO";
    TestCacheLoader loader = new TestCacheLoader() {

        public Object load2(LoaderHelper helper) throws CacheLoaderException {
            Object[] array = (Object[]) helper.getArgument();
            assertEquals(one, array[0]);
            array[0] = two;
            return value;
        }
    };
    TestCacheWriter writer = new TestCacheWriter() {

        public void beforeCreate2(EntryEvent event) throws CacheWriterException {
            Object[] array = (Object[]) event.getCallbackArgument();
            assertEquals(two, array[0]);
        }
    };
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    factory.setCacheLoader(loader);
    factory.setCacheWriter(writer);
    Region region = createRegion(name, factory.create());
    Object[] array = new Object[] { one };
    assertEquals(value, region.get(key, array));
    assertTrue(loader.wasInvoked());
    assertTrue(writer.wasInvoked());
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) EntryEvent(org.apache.geode.cache.EntryEvent) Region(org.apache.geode.cache.Region) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 27 with LoaderHelper

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

the class HARegion method findObjectInSystem.

/**
   * @return the deserialized value
   * @see LocalRegion#findObjectInSystem(KeyInfo, boolean, TXStateInterface, boolean, Object,
   *      boolean, boolean, ClientProxyMembershipID, EntryEventImpl, boolean)
   * 
   */
@Override
protected Object findObjectInSystem(KeyInfo keyInfo, boolean isCreate, TXStateInterface txState, boolean generateCallbacks, Object localValue, boolean disableCopyOnRead, boolean preferCD, ClientProxyMembershipID requestingClient, EntryEventImpl clientEvent, boolean returnTombstones) throws CacheLoaderException, TimeoutException {
    Object value = null;
    final Object key = keyInfo.getKey();
    final Object aCallbackArgument = keyInfo.getCallbackArg();
    // copy into local var to prevent race condition
    RegionEntry re = null;
    Assert.assertTrue(!hasServerProxy());
    CacheLoader loader = basicGetLoader();
    if (loader != null) {
        final LoaderHelper loaderHelper = loaderHelperFactory.createLoaderHelper(key, aCallbackArgument, false, /* netSearchAllowed */
        true, /* netloadallowed */
        null);
        try {
            value = loader.load(loaderHelper);
        } finally {
        }
        if (value != null) {
            try {
                validateKey(key);
                Operation op;
                if (isCreate) {
                    op = Operation.LOCAL_LOAD_CREATE;
                } else {
                    op = Operation.LOCAL_LOAD_UPDATE;
                }
                @Released EntryEventImpl event = EntryEventImpl.create(this, op, key, value, aCallbackArgument, false, getMyId(), generateCallbacks);
                try {
                    re = basicPutEntry(event, 0L);
                } finally {
                    event.release();
                }
                if (txState == null) {
                }
            } catch (CacheWriterException cwe) {
            // @todo smenon Log the exception
            }
        }
    }
    if (isCreate) {
        recordMiss(re, key);
    }
    return value;
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) Released(org.apache.geode.internal.offheap.annotations.Released) CacheLoader(org.apache.geode.cache.CacheLoader) Operation(org.apache.geode.cache.Operation) CacheWriterException(org.apache.geode.cache.CacheWriterException)

Example 28 with LoaderHelper

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

the class TXJUnitTest method testCacheCallbacks.

/**
   * Test to make sure CacheListener callbacks are called in place with the CacheEvents properly
   * constructed
   */
@Test
public void testCacheCallbacks() throws CacheException {
    final String key1 = "Key1";
    final String value1 = "value1";
    final String value2 = "value2";
    final String callBackArg = "call back arg";
    // install listeners
    AttributesMutator<String, String> mutator = this.region.getAttributesMutator();
    TXCallBackValidator cbv = new TXCallBackValidator();
    // Cache Listener
    ValidatableCacheListener vCl = new ValidatableCacheListener() {

        TXCallBackValidator v;

        int callCount;

        int prevCallCount;

        EntryEvent lastEvent;

        @Override
        public void validate() {
            this.v.validate(this.lastEvent, this.callCount);
        }

        void validate(EntryEvent event) {
            this.v.validate(event, ++this.callCount);
        }

        @Override
        public void setValidator(TXCallBackValidator v) {
            this.v = v;
        }

        @Override
        public void close() {
        }

        @Override
        public void afterCreate(EntryEvent event) {
            lastEvent = event;
            if (this.v.isSuspendValidation()) {
                return;
            }
            this.validate(event);
            this.v.setPassedValidation(false);
            assertTrue("IsCreate Assertion!", this.v.isCreate());
            assertTrue(event.getRegion().containsKey(this.v.getKey()));
            assertTrue(event.getRegion().containsValueForKey(this.v.getKey()));
            assertNotNull(event.getRegion().getEntry(event.getKey()).getValue());
            this.v.setPassedValidation(true);
        }

        @Override
        public void afterUpdate(EntryEvent event) {
            lastEvent = event;
            if (this.v.isSuspendValidation()) {
                return;
            }
            validate(event);
            this.v.setPassedValidation(false);
            assertTrue("IsUpdate Assertion!", this.v.isUpdate());
            assertTrue(event.getRegion().containsKey(this.v.getKey()));
            assertTrue(event.getRegion().containsValueForKey(this.v.getKey()));
            assertNotNull(event.getRegion().getEntry(event.getKey()).getValue());
            this.v.setPassedValidation(true);
        }

        @Override
        public void afterInvalidate(EntryEvent event) {
            lastEvent = event;
            if (this.v.isSuspendValidation()) {
                return;
            }
            validate(event);
            this.v.setPassedValidation(false);
            assertTrue("IsInvalidate Assertion!", this.v.isInvalidate());
            assertTrue(event.getRegion().containsKey(this.v.getKey()));
            assertTrue(!event.getRegion().containsValueForKey(this.v.getKey()));
            assertNull(event.getRegion().getEntry(event.getKey()).getValue());
            this.v.setPassedValidation(true);
        }

        @Override
        public void afterDestroy(EntryEvent event) {
            lastEvent = event;
            if (this.v.isSuspendValidation()) {
                return;
            }
            validate(event);
            this.v.setPassedValidation(false);
            assertTrue("IsDestroy Assertion!", this.v.isDestroy());
            assertTrue(!event.getRegion().containsKey(this.v.getKey()));
            assertTrue(!event.getRegion().containsValueForKey(this.v.getKey()));
            assertNull(event.getRegion().getEntry(event.getKey()));
            this.v.setPassedValidation(true);
        }

        @Override
        public void afterRegionInvalidate(RegionEvent event) {
            fail("Unexpected invocation of afterRegionInvalidate");
        }

        @Override
        public void afterRegionDestroy(RegionEvent event) {
            if (!event.getOperation().isClose()) {
                fail("Unexpected invocation of afterRegionDestroy");
            }
        }

        @Override
        public void afterRegionClear(RegionEvent event) {
        }

        @Override
        public void afterRegionCreate(RegionEvent event) {
        }

        @Override
        public void afterRegionLive(RegionEvent event) {
        }

        @Override
        public void reset() {
            lastEvent = null;
            prevCallCount = callCount;
        }

        @Override
        public void validateNoEvents() {
            assertNull("Did not expect listener callback", lastEvent);
            assertEquals(prevCallCount, callCount);
        }

        @Override
        public void setExpectedCount(int count) {
            callCount = count;
        }

        @Override
        public int getCallCount() {
            return callCount;
        }
    };
    vCl.setValidator(cbv);
    mutator.setCacheListener(vCl);
    // CacheWriter
    ValidatableCacheWriter vCw = new ValidatableCacheWriter() {

        TXCallBackValidator v;

        int callCount;

        int prevCallCount;

        EntryEvent lastEvent;

        @Override
        public int getCallCount() {
            return this.callCount;
        }

        @Override
        public void localDestroyMakeup(int count) {
            this.callCount += count;
        }

        @Override
        public void validate() {
            this.v.validate(this.lastEvent, this.callCount);
        }

        void validate(EntryEvent event) {
            this.v.validate(event, ++this.callCount);
        }

        @Override
        public void setValidator(TXCallBackValidator v) {
            this.v = v;
        }

        @Override
        public void close() {
        }

        @Override
        public void beforeCreate(EntryEvent event) {
            lastEvent = event;
            if (this.v.isSuspendValidation()) {
                return;
            }
            validate(event);
            this.v.setPassedValidation(false);
            assertTrue("IsCreate Assertion!", this.v.isCreate());
            assertTrue(!event.getRegion().containsKey(this.v.getKey()));
            assertTrue(!event.getRegion().containsValueForKey(this.v.getKey()));
            assertNull(event.getRegion().getEntry(event.getKey()));
            this.v.setPassedValidation(true);
        }

        @Override
        public void beforeUpdate(EntryEvent event) {
            lastEvent = event;
            if (this.v.isSuspendValidation()) {
                return;
            }
            validate(event);
            this.v.setPassedValidation(false);
            assertTrue("IsUpdate Assertion!", this.v.isUpdate());
            assertTrue(event.getRegion().containsKey(this.v.getKey()));
            // Can not assert the following line, as the value being update may be invalid
            // assertTrue(event.getRegion().containsValueForKey(this.v.getKey()));
            this.v.setPassedValidation(true);
        }

        @Override
        public void beforeDestroy(EntryEvent event) {
            lastEvent = event;
            if (this.v.isSuspendValidation()) {
                return;
            }
            validate(event);
            this.v.setPassedValidation(false);
            assertTrue("IsDestroy Assertion!", this.v.isDestroy());
            assertTrue(event.getRegion().containsKey(this.v.getKey()));
            this.v.setPassedValidation(true);
        }

        @Override
        public void beforeRegionDestroy(RegionEvent event) {
            fail("Unexpected invocation of beforeRegionDestroy");
        }

        @Override
        public void beforeRegionClear(RegionEvent event) {
            fail("Unexpected invocation of beforeRegionClear");
        }

        @Override
        public void reset() {
            lastEvent = null;
            prevCallCount = callCount;
        }

        @Override
        public void validateNoEvents() {
            assertNull("Did not expect a writer event", lastEvent);
            assertEquals(prevCallCount, callCount);
        }
    };
    vCw.setValidator(cbv);
    mutator.setCacheWriter(vCw);
    // Cache Loader
    mutator.setCacheLoader(new CacheLoader() {

        int count = 0;

        @Override
        public Object load(LoaderHelper helper) throws CacheLoaderException {
            return count++;
        }

        @Override
        public void close() {
        }
    });
    // Use this to track the number of callout method invocations
    int appCallCount = 1;
    // Create => beforeCreate/afterCreate tests
    cbv.setKey(key1);
    cbv.setCallBackArg(callBackArg);
    cbv.setNewValue(value1, false);
    cbv.setOldValue(null, true);
    cbv.setIsDistributed(true);
    cbv.setIsLoad(false);
    cbv.setIsCreate(true);
    cbv.setIsUpdate(false);
    // Test non-transactional create expecting beforeCreate/afterCreate call
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.create(key1, value1, callBackArg);
    assertTrue("Non-TX Create Validation Assertion", cbv.passedValidation());
    cbv.suspendValidation(true);
    this.region.localDestroy(key1);
    cbv.suspendValidation(false);
    // Test transactional create expecting afterCreate call
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.create(key1, value1, callBackArg);
    this.txMgr.commit();
    assertTrue("TX Create Validation Assertion", cbv.passedValidation());
    cbv.suspendValidation(true);
    this.region.localDestroy(key1);
    // Put => afterCreate tests
    cbv.suspendValidation(false);
    cbv.setNewValue(value2, false);
    cbv.setOldValue(null, true);
    cbv.setIsDistributed(true);
    cbv.setIsLoad(false);
    cbv.setIsCreate(true);
    cbv.setIsUpdate(false);
    // Test non-transactional put expecting afterCreate call due to no
    // previous Entry
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.put(key1, value2, callBackArg);
    assertTrue("Non-TX Put->Create Validation Assertion", cbv.passedValidation());
    cbv.suspendValidation(true);
    this.region.localDestroy(key1);
    cbv.suspendValidation(false);
    // Test transactional put expecting afterCreate call due to no
    // previous Entry
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.put(key1, value2, callBackArg);
    this.txMgr.commit();
    assertTrue("TX Put->Create Validation Assertion", cbv.passedValidation());
    // Put => afterUpdate tests
    cbv.setNewValue(value1, false);
    cbv.setOldValue(value2, false);
    cbv.setIsDistributed(true);
    cbv.setIsLoad(false);
    cbv.setIsCreate(false);
    cbv.setIsUpdate(true);
    // Test non-transactional put expecting afterUpdate call due to
    // previous Entry
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.put(key1, value1, callBackArg);
    assertTrue("Non-TX Put->Update Validation Assertion", cbv.passedValidation());
    cbv.suspendValidation(true);
    this.region.localDestroy(key1);
    this.region.put(key1, value2);
    cbv.suspendValidation(false);
    // Test transactional put expecting afterUpdate call due to
    // previous Entry
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.put(key1, value1, callBackArg);
    this.txMgr.commit();
    assertTrue("TX Put->Update Validation Assertion", cbv.passedValidation());
    // LocalDestroy => afterDestroy, non-distributed tests
    cbv.setNewValue(null, true);
    cbv.setOldValue(value1, false);
    cbv.setIsDistributed(false);
    cbv.setIsLoad(false);
    cbv.setIsDestroy(true);
    cbv.setIsUpdate(false);
    // Test non-transactional localDestroy, expecting afterDestroy,
    // non-distributed
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.localDestroy(key1, callBackArg);
    if (!isPR())
        // Account for cacheWriter not begin called
        vCw.localDestroyMakeup(1);
    assertTrue("Non-TX LocalDestroy Validation Assertion", cbv.passedValidation());
    cbv.suspendValidation(true);
    this.region.create(key1, value1);
    cbv.suspendValidation(false);
    // Test transactional localDestroy expecting afterDestroy,
    // non-distributed
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.localDestroy(key1, callBackArg);
    if (!isPR())
        // Account for cacheWriter not begin called
        vCw.localDestroyMakeup(1);
    this.txMgr.commit();
    assertTrue("TX LocalDestroy Validation Assertion", cbv.passedValidation());
    // Destroy => afterDestroy, distributed tests
    cbv.setNewValue(null, true);
    cbv.setOldValue(value1, false);
    cbv.setIsDistributed(true);
    cbv.setIsLoad(false);
    cbv.setIsDestroy(true);
    cbv.suspendValidation(true);
    this.region.create(key1, value1);
    cbv.suspendValidation(false);
    // Test non-transactional Destroy, expecting afterDestroy,
    // distributed
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.destroy(key1, callBackArg);
    assertTrue("Non-TX Destroy Validation Assertion", cbv.passedValidation());
    cbv.suspendValidation(true);
    this.region.create(key1, value1);
    cbv.suspendValidation(false);
    // Test transactional Destroy, expecting afterDestroy,
    // distributed
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.destroy(key1, callBackArg);
    this.txMgr.commit();
    assertTrue("TX Destroy Validation Assertion", cbv.passedValidation());
    // localInvalidate => afterInvalidate, non-distributed tests
    cbv.setNewValue(null, true);
    cbv.setOldValue(value1, false);
    cbv.setIsDistributed(false);
    cbv.setIsLoad(false);
    cbv.setIsInvalidate(true);
    cbv.setIsDestroy(false);
    cbv.suspendValidation(true);
    this.region.create(key1, value1);
    cbv.suspendValidation(false);
    // Test non-transactional localInvalidate, expecting afterInvalidate
    // non-distributed
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.localInvalidate(key1, callBackArg);
    assertTrue("Non-TX LocalInvalidate Validation Assertion", cbv.passedValidation());
    // Account for cacheWriter not begin called
    vCw.localDestroyMakeup(1);
    cbv.suspendValidation(true);
    this.region.put(key1, value1);
    cbv.suspendValidation(false);
    // Test transactional localInvalidate, expecting afterInvalidate
    // non-distributed
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.localInvalidate(key1, callBackArg);
    this.txMgr.commit();
    assertTrue("TX LocalInvalidate Validation Assertion", cbv.passedValidation());
    // Account for cacheWriter not begin called
    vCw.localDestroyMakeup(1);
    cbv.suspendValidation(true);
    this.region.localDestroy(key1);
    // Invalidate => afterInvalidate, distributed tests
    cbv.setNewValue(null, true);
    cbv.setOldValue(value1, false);
    cbv.setIsDistributed(true);
    cbv.setIsLoad(false);
    cbv.suspendValidation(true);
    this.region.create(key1, value1);
    cbv.suspendValidation(false);
    // Test non-transactional Invalidate, expecting afterInvalidate
    // distributed
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.invalidate(key1, callBackArg);
    // Account for cacheWriter not begin called
    vCw.localDestroyMakeup(1);
    assertTrue("Non-TX Invalidate Validation Assertion", cbv.passedValidation());
    cbv.suspendValidation(true);
    this.region.put(key1, value1);
    cbv.suspendValidation(false);
    // Test transactional Invalidate, expecting afterInvalidate
    // distributed
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.invalidate(key1, callBackArg);
    this.txMgr.commit();
    // Account for cacheWriter not begin called
    vCw.localDestroyMakeup(1);
    assertTrue("TX Invalidate Validation Assertion", cbv.passedValidation());
    cbv.suspendValidation(true);
    this.region.localDestroy(key1);
    cbv.suspendValidation(false);
    // Create load Event tests
    int loaderValCheck = 0;
    cbv.setNewValue(loaderValCheck++, false);
    cbv.setCallBackArg(null);
    cbv.setOldValue(null, false);
    cbv.setIsDistributed(true);
    cbv.setIsCreate(true);
    cbv.setIsUpdate(false);
    cbv.setIsLoad(true);
    // Test non-transactional load, expecting afterCreate distributed
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.get(key1);
    assertTrue("Non-TX Invalidate Validation Assertion", cbv.passedValidation());
    vCl.validate();
    vCw.validate();
    cbv.suspendValidation(true);
    this.region.localDestroy(key1);
    cbv.suspendValidation(false);
    // Test transactional load, expecting afterCreate distributed
    vCl.reset();
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setNewValue(loaderValCheck++, false);
    cbv.setExpectedCount(appCallCount++);
    this.region.get(key1);
    this.txMgr.rollback();
    assertTrue("TX Invalidate Validation Assertion", cbv.passedValidation());
    vCw.validate();
    vCl.validateNoEvents();
    vCl.setExpectedCount(vCl.getCallCount() + 1);
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setNewValue(loaderValCheck++, false);
    cbv.setExpectedCount(appCallCount++);
    this.region.get(key1);
    vCw.validate();
    vCw.reset();
    this.txMgr.commit();
    vCw.validateNoEvents();
    assertTrue("TX Invalidate Validation Assertion", cbv.passedValidation());
    vCl.validate();
    cbv.suspendValidation(true);
    this.region.localDestroy(key1);
    cbv.suspendValidation(false);
    // Update load Event tests
    cbv.suspendValidation(true);
    this.region.create(key1, null);
    cbv.suspendValidation(false);
    assertTrue(this.region.containsKey(key1));
    assertTrue(!this.region.containsValueForKey(key1));
    cbv.setNewValue(loaderValCheck++, false);
    cbv.setOldValue(null, false);
    cbv.setIsDistributed(true);
    cbv.setCallBackArg(null);
    cbv.setIsCreate(false);
    cbv.setIsUpdate(true);
    cbv.setIsLoad(true);
    // Test non-transactional load, expecting afterUpdate distributed
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.get(key1);
    assertTrue("Non-TX Invalidate Validation Assertion", cbv.passedValidation());
    vCw.validate();
    vCl.validate();
    cbv.suspendValidation(true);
    this.region.invalidate(key1);
    cbv.suspendValidation(false);
    assertTrue(this.region.containsKey(key1));
    assertTrue(!this.region.containsValueForKey(key1));
    // Test transactional load, expecting afterUpdate distributed
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    cbv.setNewValue(loaderValCheck++, false);
    this.region.get(key1);
    vCw.validate();
    vCw.reset();
    this.txMgr.commit();
    vCw.validateNoEvents();
    vCl.validate();
    cbv.suspendValidation(true);
    this.region.invalidate(key1);
    cbv.suspendValidation(false);
    vCl.reset();
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    cbv.setNewValue(loaderValCheck++, false);
    this.region.get(key1);
    this.txMgr.rollback();
    assertTrue("TX Invalidate Validation Assertion", cbv.passedValidation());
    vCw.validate();
    vCl.validateNoEvents();
}
Also used : RegionEvent(org.apache.geode.cache.RegionEvent) LoaderHelper(org.apache.geode.cache.LoaderHelper) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) EntryEvent(org.apache.geode.cache.EntryEvent) CacheLoader(org.apache.geode.cache.CacheLoader) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 29 with LoaderHelper

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

the class CacheLoaderTestCase method testCacheLoader.

/////////////////////// Test Methods ///////////////////////
@Test
public void testCacheLoader() throws CacheException {
    final String name = this.getUniqueName();
    final Object key = this.getUniqueName();
    final Object value = new Integer(42);
    final Object arg = "ARG";
    final String exception = "EXCEPTION";
    TestCacheLoader loader = new TestCacheLoader() {

        public Object load2(LoaderHelper helper) throws CacheLoaderException {
            assertEquals(key, helper.getKey());
            assertEquals(name, helper.getRegion().getName());
            try {
                RegionAttributes attrs = helper.getRegion().getAttributes();
                if (attrs.getScope().isDistributed()) {
                    assertNull(helper.netSearch(false));
                    assertNull(helper.netSearch(true));
                }
            } catch (TimeoutException ex) {
                Assert.fail("Why did I time out?", ex);
            }
            Object argument = helper.getArgument();
            if (argument != null) {
                if (argument.equals(exception)) {
                    String s = "Test Exception";
                    throw new CacheLoaderException(s);
                } else {
                    assertEquals(arg, argument);
                }
            }
            return value;
        }
    };
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    factory.setCacheLoader(loader);
    Region region = createRegion(name, factory.create());
    loader.wasInvoked();
    Region.Entry entry = region.getEntry(key);
    assertNull(entry);
    region.create(key, null);
    entry = region.getEntry(key);
    assertNotNull(entry);
    assertNull(entry.getValue());
    assertEquals(value, region.get(key));
    assertTrue(loader.wasInvoked());
    assertEquals(value, region.getEntry(key).getValue());
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) Region(org.apache.geode.cache.Region) TimeoutException(org.apache.geode.cache.TimeoutException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 30 with LoaderHelper

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

the class CacheLoaderTestCase method testCacheLoaderNull.

// public void testCacheLoaderWithNetSearch() throws CacheException {
// final String name = this.getUniqueName();
// final Object key = this.getUniqueName();
// final Object value = new Integer(42);
// final Object arg = "ARG";
// final String exception = "EXCEPTION";
//
// final TestCacheLoader loader = new TestCacheLoader() {
// public Object load2(LoaderHelper helper)
// throws CacheLoaderException {
//
// assertIndexDetailsEquals(key, helper.getKey());
// assertIndexDetailsEquals(name, helper.getRegion().getName());
//
// try {
// RegionAttributes attrs =
// helper.getRegion().getAttributes();
// if (attrs.getScope().isDistributed()) {
// Object result = helper.netSearch(false);
// assertIndexDetailsEquals(value, result);
// return result;
// }
//
// } catch (TimeoutException ex) {
// fail("Why did I time out?", ex);
// }
// return value;
// }
// };
//
// Region region =
// createRegion(name);
// loader.wasInvoked();
//
// Region.Entry entry = region.getEntry(key);
// assertNull(entry);
// region.create(key, null);
//
// entry = region.getEntry(key);
// assertNotNull(entry);
// assertNull(entry.getValue());
//
// Host host = Host.getHost(0);
// VM vm0 = host.getVM(0);
// vm0.invoke(new CacheSerializableRunnable("set remote value") {
// public void run2() throws CacheException {
//// final TestCacheLoader remoteloader = new TestCacheLoader() {
//// public Object load2(LoaderHelper helper)
//// throws CacheLoaderException {
////
//// assertIndexDetailsEquals(key, helper.getKey());
//// assertIndexDetailsEquals(name, helper.getRegion().getName());
//// return value;
//// }
//// };
////
//// AttributesFactory factory =
//// new AttributesFactory(getRegionAttributes());
//// factory.setCacheLoader(remoteloader);
// Region rgn = createRegion(name);
// rgn.put(key, value);
// flushIfNecessary(rgn);
// }
// });
//
//
// assertIndexDetailsEquals(value, region.get(key));
// assertTrue(loader.wasInvoked());
// assertIndexDetailsEquals(value, region.getEntry(key).getValue());
// }
/**
   * Tests what happens when a {@link CacheLoader} returns <code>null</code> from its
   * {@link CacheLoader#load load} method.
   */
@Test
public void testCacheLoaderNull() throws CacheException {
    TestCacheLoader loader = new TestCacheLoader() {

        public Object load2(LoaderHelper helper) throws CacheLoaderException {
            return null;
        }
    };
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    factory.setCacheLoader(loader);
    String name = this.getUniqueName();
    Region region = createRegion(name, factory.create());
    loader.wasInvoked();
    assertNull(region.get("KEY"));
    assertTrue(loader.wasInvoked());
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) Region(org.apache.geode.cache.Region) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

LoaderHelper (org.apache.geode.cache.LoaderHelper)56 Test (org.junit.Test)47 Region (org.apache.geode.cache.Region)45 AttributesFactory (org.apache.geode.cache.AttributesFactory)33 CacheException (org.apache.geode.cache.CacheException)32 CacheLoader (org.apache.geode.cache.CacheLoader)32 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)32 VM (org.apache.geode.test.dunit.VM)29 Host (org.apache.geode.test.dunit.Host)26 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)26 LocalRegion (org.apache.geode.internal.cache.LocalRegion)22 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)19 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)18 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)15 TimeoutException (org.apache.geode.cache.TimeoutException)13 StoredObject (org.apache.geode.internal.offheap.StoredObject)13 CacheWriterException (org.apache.geode.cache.CacheWriterException)11 EntryEvent (org.apache.geode.cache.EntryEvent)10 RegionAttributes (org.apache.geode.cache.RegionAttributes)9 AttributesMutator (org.apache.geode.cache.AttributesMutator)7