Skip to content

Commit

Permalink
fixed test on spawning exception from RuntimeAwait for PE
Browse files Browse the repository at this point in the history
  • Loading branch information
rssh committed Oct 1, 2023
1 parent 189ed01 commit 8f8d334
Showing 1 changed file with 51 additions and 17 deletions.
68 changes: 51 additions & 17 deletions jvm-loom-addon/src/test/scala/cpsloomtest/TestPE.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.util.concurrent.{CompletableFuture, ConcurrentHashMap, ConcurrentLin
import scala.util.*
import scala.collection.mutable.{Map, Queue}
import scala.collection.concurrent.TrieMap
import scala.concurrent.{Await, BlockContext, CanAwait, blocking}
import scala.concurrent.{Await, BlockContext, CanAwait, ExecutionException, blocking}
import org.junit.{Ignore, Test}

import java.util.concurrent.atomic.{AtomicInteger, AtomicLong}
Expand Down Expand Up @@ -39,20 +39,32 @@ object PoorManEffect {
def error[T](e: Throwable): PoorManEffect[T] = Error(e)

def map[A, B](fa: PoorManEffect[A])(f: A => B): PoorManEffect[B] = fa match
case Pure(t) => Pure(f(t))
case Pure(t) =>
inTry(Pure(f(t)))
case Error(e) => Error(e)
case Thunk(th) => Thunk((d) => map(th(d))(f))
case Thunk(th) => Thunk((d) => map(inTry(th(d)))(f))

def flatMap[A, B](fa: PoorManEffect[A])(f: A => PoorManEffect[B]): PoorManEffect[B] = fa match
case Pure(t) => f(t)
case Pure(t) =>
inTry(f(t))
case Error(e) => Error(e)
case Thunk(th) => Thunk(d => flatMap(th(d))(f))
case Thunk(th) => Thunk(d => flatMap(inTry(th(d)))(f))

def flatMapTry[A, B](fa: PoorManEffect[A])(f: Try[A] => PoorManEffect[B]): PoorManEffect[B] =
fa match
case Pure(t) => f(Success(t))
case Error(e) => f(Failure(e))
case Thunk(th) => Thunk(d => flatMapTry(th(d))(f))
case Pure(t) =>
inTry(f(Success(t)))
case Error(e) =>
inTry(f(Failure(e)))
case Thunk(th) =>
Thunk(d => flatMapTry(inTry(th(d)))(f))

def inTry[X](op: =>PoorManEffect[X]): PoorManEffect[X] =
try
op
catch
case NonFatal(ex) =>
Error(ex)

}

Expand Down Expand Up @@ -142,9 +154,12 @@ object PoorManEffect {
}
}
}
val retval = thunk
submitWaiter.synchronized {
submitWaiter.notifyAll()
val retval = try {
thunk
} finally {
submitWaiter.synchronized {
submitWaiter.notifyAll()
}
}
retval
}
Expand All @@ -159,7 +174,8 @@ object PoorManEffect {
val v = runQueue.dequeue()
v.pe match
case Pure(t) => setWaiterResult(v.id, Success(t))
case Error(e) => setWaiterResult(v.id, Failure(e))
case Error(e) =>
setWaiterResult(v.id, Failure(e))
case Thunk(th) =>
// here we can have call of block-context.
nThunksInProcess.incrementAndGet()
Expand Down Expand Up @@ -203,7 +219,12 @@ object PoorManEffect {
val runner = new Runner()
val id0 = runner.submit[A](pe)
val resultFuture = runner.listenSubmitted[A](id0)
runner.process()
try
runner.process()
catch
case NonFatal(ex) =>
println(s"Impossible, excwption from process: ${ex.getMessage}")
throw ex
runner.checkSubmitted[A](id0) match
case Some(Success(t)) => t.asInstanceOf[A]
case Some(Failure(e)) => throw e
Expand All @@ -220,7 +241,11 @@ object PoorManEffect {
throw new RuntimeException(s"process finished, but no result for id ${id0}")
} else {
blocking {
resultFuture.get()
try
resultFuture.get()
catch
case ex: ExecutionException =>
throw ex.getCause()
}
}

Expand All @@ -244,8 +269,13 @@ class PoorManEffectRuntimeAwait(rt:PoorManEffect.RunAPI) extends CpsRuntimeAwait
val cf = rt.listenSubmitted[A](id)
// here execution of main loop of runner.process will be moved to other virtual thread.
blocking{
val retval = cf.get()
rt.forgetSubmitted(id)
val retval = try
cf.get()
catch
case ex: ExecutionException =>
throw ex.getCause()
finally
rt.forgetSubmitted(id)
retval
}
}
Expand Down Expand Up @@ -314,7 +344,7 @@ class TestPE {
}

@Test
def testPMECatchExceptionFromAwait(): Int = {
def testPMECatchExceptionFromAwait(): Unit = {
val c = async[PoorManEffect] {
val list0 = MyList.create(1, 2, 3, 4, 5)
try {
Expand All @@ -325,6 +355,10 @@ class TestPE {
case ex: RuntimeException =>
assert(ex.getMessage() == "test")
2
case ex: Throwable =>
println("Catch non-tuntime exception")
ex.printStackTrace()
throw ex
}
}
val r = PoorManEffect.run(c)
Expand Down

0 comments on commit 8f8d334

Please sign in to comment.