<commit_before>package org.codehaus.groovy.runtime.memoize

import java.lang.ref.ReferenceQueue
import java.lang.ref.SoftReference

/**
 * @author <NAME>
 */
public class CacheCleanupCollectedSoftReferencesTest extends GroovyTestCase {   


    public void testCollectedCacheValuesAreEnqueued() {

        Closure cl = { 
            new Integer(it + 1) 
        }

        UnlimitedConcurrentCache cache = new UnlimitedConcurrentCache()
        Closure memoizedClosure = Memoize.buildSoftReferenceMemoizeFunction(0, cache, cl)

        assert cache.cache.size() == 0
        memoizedClosure.call(1)
        assert cache.cache.size() == 1

        forceSoftReferencesRecollection()

        def softReference = cache.get([1])
        checkSoftReferenceAreSoftlyReachable(softReference)
        checkCollectedSoftReferenceAreEnqueued(softReference)

    }


    public void testCollectedCacheValuesAreRemovedFromCache() {

        Closure cl = { 
            new Integer(it + 1) 
        }

        UnlimitedConcurrentCache cache = new UnlimitedConcurrentCache()
        Closure memoizedClosure = Memoize.buildSoftReferenceMemoizeFunction(0, cache, cl)

        assert cache.cache.size() == 0
        memoizedClosure.call(1)
        assert cache.cache.size() == 1

        forceSoftReferencesRecollection()

        assert cache.cache.size() == 1

        // As there is not a cleanup thread polling the ReferenceQueue, 
        // a call() invocation is needed to fire the cleaning up of null references.
        memoizedClosure.call(2)

        assert cache.cache.size() == 1 : 'collected SoftReferences should be removed from cache'
    }


    private void checkSoftReferenceAreSoftlyReachable(softReference) {

        assert softReference.get() == null : 
            'cache values should be softly reachable and collected before an OOME' 

    }<commit_msg>deactivated tests because they are successful only sometimes on the build server<commit_after>package org.codehaus.groovy.runtime.memoize

import java.lang.ref.ReferenceQueue
import java.lang.ref.SoftReference

/**
 * @author <NAME>
 */
public class CacheCleanupCollectedSoftReferencesTest extends GroovyTestCase {   


    // TODO re-enable this test once CI server can safely handle it
    public void manual_testCollectedCacheValuesAreEnqueued() {

        Closure cl = { 
            new Integer(it + 1) 
        }

        UnlimitedConcurrentCache cache = new UnlimitedConcurrentCache()
        Closure memoizedClosure = Memoize.buildSoftReferenceMemoizeFunction(0, cache, cl)

        assert cache.cache.size() == 0
        memoizedClosure.call(1)
        assert cache.cache.size() == 1

        forceSoftReferencesRecollection()

        def softReference = cache.get([1])
        checkSoftReferenceAreSoftlyReachable(softReference)
        checkCollectedSoftReferenceAreEnqueued(softReference)

    }


    // TODO re-enable this test once CI server can safely handle it
    public void manual_testCollectedCacheValuesAreRemovedFromCache() {

        Closure cl = { 
            new Integer(it + 1) 
        }

        UnlimitedConcurrentCache cache = new UnlimitedConcurrentCache()
        Closure memoizedClosure = Memoize.buildSoftReferenceMemoizeFunction(0, cache, cl)

        assert cache.cache.size() == 0
        memoizedClosure.call(1)
        assert cache.cache.size() == 1

        forceSoftReferencesRecollection()

        assert cache.cache.size() == 1

        // As there is not a cleanup thread polling the ReferenceQueue, 
        // a call() invocation is needed to fire the cleaning up of null references.
        memoizedClosure.call(2)

        assert cache.cache.size() == 1 : 'collected SoftReferences should be removed from cache'
    }


    private void checkSoftReferenceAreSoftlyReachable(softReference) {

        assert softReference.get() == null : 
            'cache values should be softly reachable and collected before an OOME' 

    }
{'fraction_non_alphanumeric': 0.07405528785188942, 'fraction_numerical': 0.007101191985797616, 'mean_word_length': 2.8590998043052838, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6725195', 'n_tokens_mistral': 1132, 'n_tokens_neox': 1025, 'n_words': 277}