jsuereth/scala-arm — Issue #8: Managed resource not closed if 'return' statement is inside managed code block
Author: roeoender
Created: 2011-12-20 21:10:53+00:00
State: open

See test case:

import resource._
import org.junit.runner.RunWith
import org.scalatest.Suite
import org.scalatest.junit.JUnitRunner
import org.scalatest.Assertions

@RunWith(classOf[JUnitRunner])
class ScalaArmTest extends Suite with Assertions {
def testNoReturnThisWillPass {
val c1 = new Counter
someFunNoRet1(c1)
assert(c1.c === 1)
}

def testWithReturn1ThisWillFail {
val c1 = new Counter
someFunWithRet1(c1)
assert(c1.c === 1)
}

def testWithReturn2ThisWillAlsoFail {
val c1 = new Counter
someFunWithRet2(c1)
assert(c1.c === 1)
}

class Counter {
var c : Int = 0;
def increment = c = c + 1
}

class SomethingToClose(counter : Counter) {
def close() {
counter.increment
println("closing")
}
}

def someFunNoRet1(counter : Counter): Boolean = {
managed(new SomethingToClose(counter)) acquireAndGet {
s =>
println("in someFunNoRet")
}
true // note outside "managed"
}

def someFunWithRet1(counter : Counter): Boolean = {
managed(new SomethingToClose(counter)) acquireAndGet {
s =>
println("in someFunWithRet1")
return true
}
}
def someFunWithRet2(counter : Counter): Boolean = {
for (s <- managed(new SomethingToClose(counter))) {
println("in someFunWithRet2")
return true
}
return false
}
}