home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (CacheCleanupTest.groovy)

This example Groovy source code file (CacheCleanupTest.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

anchor, cachecleanuptest, cyclicbarrier, cyclicbarrier, groovytestcase, lru, lrucache, lrucache, object, simulating, simulating, softreference, softreference, threading, threads, unlimitedconcurrentcache

The Groovy CacheCleanupTest.groovy source code

package org.codehaus.groovy.runtime.memoize

import java.lang.ref.SoftReference
import java.util.concurrent.CyclicBarrier
import java.util.concurrent.TimeUnit

/**
 * @author Vaclav Pech
 */
public class CacheCleanupTest extends GroovyTestCase {
    private static final Object ANCHOR = "I'm never gonna go"
    
    public void testUnlimitedCache() {
        checkCache(new UnlimitedConcurrentCache())
        checkCache(new LRUCache(10))
    }

    private def checkCache(MemoizeCache cache) {
        assert cache.cache.size() == 0
        cache.put('key1', new SoftReference(ANCHOR))
        cache.put('key2', new SoftReference(ANCHOR))
        assert cache.cache.size() == 2
        cache.put('key3', new SoftReference(null))  //Simulating evicted objects
        cache.put('key4', new SoftReference(null))
        cache.cleanUpNullReferences()
        assert cache.cache.size() == 2
    }

    public void testUnlimitedCacheConcurrently() {
        checkCacheConcurrently(new UnlimitedConcurrentCache())
        checkCacheConcurrently(new LRUCache(2000))
        checkCacheConcurrently(new LRUCache(50))  //testing a cache that removes old elements
    }

    private def checkCacheConcurrently(MemoizeCache cache) {
        assert cache.cache.size() == 0
        cache.put('key1', new SoftReference(ANCHOR))
        cache.put('key2', new SoftReference(ANCHOR))
        assert cache.cache.size() == 2
        for (i in (3..1000)) {
            cache.put("key${i}", new SoftReference(null))  //Simulating evicted objects
            cache.get('key1')  //touch the non-null cache entries to keep them hot to prevent a potential LRU algorithm from evicting them
            cache.get('key2')
        }

        final CyclicBarrier barrier = new CyclicBarrier(11)
        10.times {
            Thread.start {
                barrier.await()
                cache.cleanUpNullReferences()
                barrier.await()
            }
        }
        barrier.await(30, TimeUnit.SECONDS)  //start threads
        barrier.await(30, TimeUnit.SECONDS)  //wait for threads to finish

        assert cache.cache.size() == 2
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy CacheCleanupTest.groovy source code file:



my book on functional programming

 

new blog posts

 

Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.