Scott Murphy Heiberg created GROOVY-12674:
---------------------------------------------

             Summary: memoizeAtMost, memoizeAtLeast and memoizeBetween closures 
cannot be serialized, unlike memoize closures
                 Key: GROOVY-12674
                 URL: https://issues.apache.org/jira/browse/GROOVY-12674
             Project: Groovy
          Issue Type: Bug
    Affects Versions: 5.0.8, 4.0.33, 6.0.0, 7.0.0-alpha-1
            Reporter: Scott Murphy Heiberg


{code:groovy}
def roundTrip(Closure c) {
    def bytes = new ByteArrayOutputStream()
    bytes.withObjectOutputStream { it.writeObject(c) }
    new 
ByteArrayInputStream(bytes.toByteArray()).withObjectInputStream(getClass().classLoader)
 { it.readObject() }
}
def base = { x -> x * 2 }.dehydrate()
assert roundTrip(base.memoize())(4) == 8      // works
roundTrip(base.memoizeAtMost(2))               // NotSerializableException: 
org.codehaus.groovy.runtime.memoize.LRUCache
roundTrip(base.memoizeAtLeast(0))              // NotSerializableException: 
org.codehaus.groovy.runtime.memoize.NullProtectionStorage
roundTrip(base.memoizeAtLeast(2))              // NotSerializableException: 
java.lang.ref.ReferenceQueue
def between = base.memoizeBetween(1, 3)
between(1)
roundTrip(between)                             // NotSerializableException: 
java.lang.ref.SoftReference
{code}
*Actual:* every variant except memoize() throws NotSerializableException, 
whether or not it has been called.

*Expected:* they serialize like memoize() does and still memoize after 
deserialization, with the same limits.

Cause: LRUCache is not Serializable. The soft reference variants hold a 
ReferenceQueue, a NullProtectionStorage or LRUProtectionStorage, and 
SoftReference cache values, none of which can be serialized.

Verified on 4.0.33, 5.0.8, 6.0.0 and master.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to