Package com.volantis.cache.impl

Examples of com.volantis.cache.impl.InternalCache


        CacheBuilder builder = factory.createCacheBuilder();
        builder.setClock(clockMock);
        builder.setObjectProvider(providerMock);
        builder.setMaxCount(Integer.MAX_VALUE);
        InternalCache cache = (InternalCache) builder.buildCache();
        Group root = cache.getRootGroup();

        // =====================================================================
        //   Set Expectations
        // =====================================================================

        addProviderExpectation(key1, null,
                new ProviderResult(value1, root, true, null));

        clockMock.expects.getCurrentTime().returns(Time.inMilliSeconds(1010));

        // =====================================================================
        //   Test Expectations
        // =====================================================================

        Object object;

        // Check the cache integrity.
        ensureCacheIntegrity(cache);

        object = cache.retrieve(key1);
        assertSame(value1, object);

        // Check the cache integrity.
        ensureCacheIntegrity(cache);

        object = cache.retrieve(key1);
        assertSame(value1, object);

        // Check the cache integrity.
        ensureCacheIntegrity(cache);
View Full Code Here


    public void testRetrieveWithProvider() throws Exception {

        CacheBuilder builder = factory.createCacheBuilder();
        builder.setClock(clockMock);
        builder.setMaxCount(Integer.MAX_VALUE);
        InternalCache cache = (InternalCache) builder.buildCache();
        Group root = cache.getRootGroup();

        // =====================================================================
        //   Set Expectations
        // =====================================================================

        addProviderExpectation(key1, null,
                new ProviderResult(value1, root, true, null));

        // =====================================================================
        //   Test Expectations
        // =====================================================================
        Object object;

        // Check the cache integrity.
        ensureCacheIntegrity(cache);

        object = cache.retrieve(key1, providerMock);
        assertSame(value1, object);

        // Check the cache integrity.
        ensureCacheIntegrity(cache);

        object = cache.retrieve(key1, providerMock);
        assertSame(value1, object);

        // Check the cache integrity.
        ensureCacheIntegrity(cache);
View Full Code Here

        CacheBuilder builder = factory.createCacheBuilder();
        builder.setClock(clockMock);
        builder.setMaxCount(2);
        builder.setObjectProvider(providerMock);
        InternalCache cache = (InternalCache) builder.buildCache();
        final Group root = cache.getRootGroup();

        // =====================================================================
        //   Set Expectations
        // =====================================================================

        expectations.add(new OrderedExpectations() {
            public void add() {
                addProviderExpectation(key1, null,
                        new ProviderResult(value1, root, true, null));
                addProviderExpectation(key2, null,
                        new ProviderResult(value2, root, true, null));

                addProviderExpectation(key3, null,
                        new ProviderResult(value3, root, true, null));
                removalListenerMock.fuzzy
                        .entryRemoved(EXPECTED_CACHE_ENTRY)
                        .does(new ExpectCacheEntry(key1, value1));

                addProviderExpectation(key1, null,
                        new ProviderResult(value1, root, true, null));
                removalListenerMock.fuzzy
                        .entryRemoved(EXPECTED_CACHE_ENTRY)
                        .does(new ExpectCacheEntry(key3, value3));
            }
        });

        // =====================================================================
        //   Test Expectations
        // =====================================================================

        root.addRemovalListener(removalListenerMock);

        Object object;

        // Check the cache integrity.
        ensureCacheIntegrity(cache);

        object = cache.retrieve(key1);
        assertSame(value1, object);

        // Check the cache integrity.
        ensureCacheIntegrity(cache);

        object = cache.retrieve(key2);
        assertSame(value2, object);

        // Check the cache integrity.
        ensureCacheIntegrity(cache);

        // At this point cache contains (key2, key1).

        // Retrieving this should discard the entry for key1.
        object = cache.retrieve(key3);
        assertSame(value3, object);

        // Check the cache integrity.
        ensureCacheIntegrity(cache);

        // At this point cache contains (key3, key2).

        object = cache.retrieve(key2);
        assertSame(value2, object);

        // Check the cache integrity.
        ensureCacheIntegrity(cache);

        // At this point cache contains (key2, key3).

        // Retrieving this should discard the entry for key3.
        object = cache.retrieve(key1);
        assertSame(value1, object);

        // Check the cache integrity.
        ensureCacheIntegrity(cache);
View Full Code Here

        CacheBuilder builder = factory.createCacheBuilder();
        builder.setClock(clockMock);
        builder.setExpirationChecker(expirationCheckerMock);
        builder.setMaxCount(2);
        builder.setObjectProvider(providerMock);
        InternalCache cache = (InternalCache) builder.buildCache();

        GroupBuilder groupBuilder = factory.createGroupBuilder();
        groupBuilder.setMaxCount(2);

        clockMock.expects.getCurrentTime().returns(Time.inMilliSeconds(2000));

        final Group root = cache.getRootGroup();
        final Group other = root.addGroup("group1", groupBuilder);

        // =====================================================================
        //   Set Expectations
        // =====================================================================

        expectations.add(new OrderedExpectations() {
            public void add() {
                addProviderExpectation(key1, null,
                        new ProviderResult(value1, root, true, null));

                // First time expiration checker reports that entry has not
                // expired.
                expirationCheckerMock.fuzzy
                        .hasExpired(clockMock, EXPECTED_CACHE_ENTRY)
                        .returns(false);

                // Second time expiration checker reports that entry has
                // expired.
                expirationCheckerMock.fuzzy
                        .hasExpired(clockMock, EXPECTED_CACHE_ENTRY)
                        .returns(true);

                // Second request retrieves different value but entry still
                // has old value in it. Different value is in different group.
                addProviderExpectation(key1, value1,
                        new ProviderResult(value2, other, true, null));
            }
        });

        // =====================================================================
        //   Test Expectations
        // =====================================================================


        Object value;

        // Check the cache integrity.
        ensureCacheIntegrity(cache);

        value = cache.retrieve(key1);
        assertEquals(value1, value);

        // Check the cache integrity.
        ensureCacheIntegrity(cache);

        // Retrieve it again, this should invoke the expiration checker which
        // reports that it has not expired.
        value = cache.retrieve(key1);
        assertEquals(value1, value);

        // Check the cache integrity.
        ensureCacheIntegrity(cache);

        // Retrieve it again, this should invoke the expiration checker which
        // now reports that it has expired so the object will be retrieved
        // again and will return a different value.
        value = cache.retrieve(key1);
        assertEquals(value2, value);

        // Check the cache integrity.
        ensureCacheIntegrity(cache);
    }
View Full Code Here

        CacheBuilder builder = factory.createCacheBuilder();
        builder.setClock(clockMock);
        builder.setMaxCount(2);
        builder.setObjectProvider(providerMock);
        InternalCache cache = (InternalCache) builder.buildCache();

        // =====================================================================
        //   Set Expectations
        // =====================================================================

        IllegalStateException throwable = new IllegalStateException("expected");
        addProviderExpectation(key1, null, throwable);

        // =====================================================================
        //   Test Expectations
        // =====================================================================

        // Check the cache integrity.
        ensureCacheIntegrity(cache);

        // Retrieve the object, this will fail and should result in an
        // exception being thrown.
        try {
            cache.retrieve(key1);
            fail("Did not cause exception");
        } catch (IllegalStateException expected) {
            assertSame(throwable, expected);
        }

        // Check the cache integrity.
        ensureCacheIntegrity(cache);

        // Retrieve it again, this should not invoke the provider again.
        try {
            cache.retrieve(key1);
            fail("Did not cause exception");
        } catch (ExtendedRuntimeException expected) {
            assertSame(throwable, expected.getCause());
        }
View Full Code Here

        CacheBuilder builder = factory.createCacheBuilder();
        builder.setClock(clockMock);
        builder.setExpirationChecker(expirationCheckerMock);
        builder.setMaxCount(2);
        builder.setObjectProvider(providerMock);
        InternalCache cache = (InternalCache) builder.buildCache();
        final Group root = cache.getRootGroup();

        // =====================================================================
        //   Set Expectations
        // =====================================================================

        final IllegalStateException throwable =
                new IllegalStateException("expected");

        expectations.add(new OrderedExpectations() {
            public void add() {
                addProviderExpectation(key1, null,
                        new ProviderResult(value1, root, true, null));

                // Expiration check for second request reports that entry has
                // expired.
                expirationCheckerMock.fuzzy
                        .hasExpired(clockMock, EXPECTED_CACHE_ENTRY)
                        .returns(true);

                // Second provider request fails but the entry has the old
                // value.
                addProviderExpectation(key1, value1, throwable);
            }
        });


        // =====================================================================
        //   Test Expectations
        // =====================================================================

        Object value;

        // Check the cache integrity.
        ensureCacheIntegrity(cache);

        // Retrieve the object, this will work and should result in the entry
        // being added to the root group.
        value = cache.retrieve(key1);
        assertEquals(value1, value);

        // Check the cache integrity.
        ensureCacheIntegrity(cache);

        // Retrieve it again, this should invoke the provider again as the
        // entry has expired. Although this fails it should not remove the
        // entry from the cache as it already belongs to a group.
        try {
            value = cache.retrieve(key1);
        } catch (IllegalStateException expected) {
            assertSame(throwable, expected);
        }

        // Check the cache integrity.
        ensureCacheIntegrity(cache);

        // Retrieve it again, this should not invoke the provider as the entry
        // is in the cache and has not expired but it should still error.
        try {
            value = cache.retrieve(key1);
        } catch (ExtendedRuntimeException expected) {
            assertSame(throwable, expected.getCause());
        }

        // Check the cache integrity.
View Full Code Here

        CacheBuilder builder = factory.createCacheBuilder();
        builder.setClock(clockMock);
        builder.setMaxCount(2);
        builder.setObjectProvider(providerMock);
        InternalCache cache = (InternalCache) builder.buildCache();

        // =====================================================================
        //   Set Expectations
        // =====================================================================

        providerMock.fuzzy.retrieve(clockMock, key1, EXPECTED_CACHE_ENTRY)
                .returns(null);

        // =====================================================================
        //   Test Expectations
        // =====================================================================

        // Retrieve the object, this will fail and should result in an
        // exception being thrown.
        try {
            cache.retrieve(key1);
            fail("Did not cause exception");
        } catch (IllegalStateException expected) {
            assertEquals("Result from provider is null", expected.getMessage());
        }
    }
View Full Code Here

        CacheBuilder builder = factory.createCacheBuilder();
        builder.setClock(clockMock);
        builder.setMaxCount(2);
        builder.setObjectProvider(providerMock);
        InternalCache cache = (InternalCache) builder.buildCache();

        Group root = cache.getRootGroup();

        // =====================================================================
        //   Test Expectations
        // =====================================================================

        // Expect a request that initially assumes that the entry is cacheable
        // but returns a result that indicates that it is not.
        addProviderExpectation(key1, null,
                new ProviderResult(value1, root, false, null));

        // Retrieve the object, this will work and mark the entry as
        // uncacheable.
        Object value = cache.retrieve(key1);
        assertSame(value1, value);

        // Expect a request for an uncacheable entry that fails with an error.
        Throwable throwable = new Error();
        addUncacheableProviderExpectation(key1, throwable);

        // Retrieve the object, this will fail and should result in an
        // exception being thrown. However, it must not mark the entry as
        // in error because that would be equivalent to caching the result
        // and the entry is uncacheable.
        try {
            cache.retrieve(key1);
            fail("Did not cause exception");
        } catch (Error expected) {
            assertSame(throwable, expected);
        }

        // Expect a request for an uncacheable entry but return a result that
        // indicates that it is now cacheable.
        addUncacheableProviderExpectation(key1,
                new ProviderResult(value2, root, true, null));

        // Retrieve the object again, this will not fail this time and should
        // result in the object being returned.
        value = cache.retrieve(key1);
        assertSame(value2, value);

        // Retrieve the object again, this will use the cached version.
        value = cache.retrieve(key1);
        assertSame(value2, value);
    }
View Full Code Here

                String value = "value for (" + k + ")";
                return new ProviderResult(value, k.getGroup(), cacheable, "extensions");
            }
        });
        final InternalCache cache = (InternalCache) builder.buildCache();

        // Create 7 groups, of size 4,8,12,16,20,24,28 with two subgroups
        // one of which is 50% and one is 75% of the size of the parent group.
        GroupBuilder groupBuilder = factory.createGroupBuilder();
        Group rootGroup = cache.getRootGroup();
        for (int i = 0, g = 0; i < 7; i += 1) {
            int maxCount = i * GROUP_SIZE_MULTIPLE + 4;
            groupBuilder.setMaxCount(maxCount);
            Group group = rootGroup.addGroup("group" + i, groupBuilder);
            groups[g++] = group;

            for (int s = 0; s < 2; s += 1) {
                groupBuilder.setMaxCount((int) ((0.5 + s / 4.0) * maxCount));
                groups[g++] = group.addGroup("group" + i + "/" + s,
                        groupBuilder);
            }
        }

        TestRemovalListener listener = new TestRemovalListener();
        rootGroup.addRemovalListener(listener);

        // Create 10 threads, each of which will perform one of the following
        // actions at random.
        // 1) Retrieve an item from the store
        Thread[] threads = new Thread[20];
        MyRunnable[] runnables = new MyRunnable[threads.length];
        for (int i = 0; i < threads.length; i++) {
            MyRunnable runnable = new MyRunnable(i, groups, cache);
            Thread thread = new Thread(runnable);
            threads[i] = thread;
            runnables[i] = runnable;
        }

        StatisticsSnapshot snapshotBefore = rootGroup.getStatisticsSnapshot();

        for (int i = 0; i < threads.length; i++) {
            Thread thread = threads[i];
            thread.start();
        }

        boolean failed = false;
        for (int i = 0; i < threads.length; i++) {
            Thread thread = threads[i];
            MyRunnable runnable = runnables[i];
            thread.join();

            if (runnable.failed()) {
                String errors = runnable.getErrors();
                if (errors != null) {
                    if (logger.isDebugEnabled()) {
                        logger.debug(errors);
                    }
                }
                failed = true;
            }
        }
        if (failed) {
            fail("Stress test failed due to exceptions thrown by threads");
        }

        // Validate the internal structure of the cache.
        ensureCacheIntegrity(cache);

        StatisticsSnapshot snapshotAfter = rootGroup.getStatisticsSnapshot();
        StatisticsDelta delta = snapshotAfter.difference(snapshotBefore);
        System.out.println("Period:                 " + delta.getPeriod());
        System.out.println("Hit count:              " + delta.getHitCount());
        System.out.println("Missed / Added count:   " + delta.getMissedAddedCount());
        System.out.println("Removed count:          " + delta.getRemovedCount());
        System.out.println("Change count:           " +
                (delta.getMissedAddedCount() - delta.getRemovedCount()));
        System.out.println("Hit rate:               " + delta.getHitRate() + "%");

        System.out.println("Key count:              " + keys.size());

        cache.debugStructure(new IndentingWriter(System.out));

        // Make sure that the number of entries removed according to the
        // snapshot is equal to the number removed according to the listener.
        int notifiedRemovedCount = listener.getRemovedCount();
        System.out.println("Notified removed count: " + notifiedRemovedCount);
        assertEquals(delta.getRemovedCount(), notifiedRemovedCount);

        if (logger.isDebugEnabled()) {
            StringWriter writer = new StringWriter();
            IndentingWriter printer = new IndentingWriter(writer);
            cache.debugStructure(printer);
            logger.debug(writer.toString());
        }
    }
View Full Code Here

TOP

Related Classes of com.volantis.cache.impl.InternalCache

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.