Skip to content

Commit

Permalink
added test for calling finalizers in try/catch
Browse files Browse the repository at this point in the history
  • Loading branch information
rssh committed Oct 7, 2024
1 parent 8897812 commit ec0409e
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions shared/src/test/scala/cps/pe/TestPEAsyncFinalizer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cps.pe

import org.junit.{Test,Ignore}
import cps.*

import cps.util.FutureCompleter
import cps.testconfig.given

import scala.concurrent.ExecutionContext.Implicits.global

class TestPEAsyncFinalizer {




@Test
def testLinearAsyncFinalizer() = {

given cps.macros.flags.PrintCode.type = cps.macros.flags.PrintCode

@volatile var x = 0
@volatile var y = 0
@volatile var nMainCalls = 0
@volatile var nFinalizerCalls = 0
val run = async[PureEffect] {
try {
x = 2
await(PureEffect.delay(1))
nMainCalls = nMainCalls + 1
} finally {
nFinalizerCalls = nFinalizerCalls + 1
y = await(PureEffect.delay(2))
if (x == 2) then
x = 3
else
x = 1
}
}

val c = run.map { _ =>
assert(nFinalizerCalls == 1)
assert(nMainCalls == 1)
assert(x == 3)
assert(y == 2)
}

val future = c.unsafeRunFuture()
FutureCompleter(future)

}


}

0 comments on commit ec0409e

Please sign in to comment.