Skip to content

Commit

Permalink
Add updateExistingVar arg.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvgica committed Nov 4, 2023
1 parent 8c1a921 commit 8e23a89
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ import java.util.concurrent.ScheduledExecutorService
* @param handleInitializationError
* A PartialFunction used to recover from exceptions in the var initialization. If unspecified,
* the exception will fail the effect returned by `ready`.
* @param updateExistingVar
* If specified, will be used to update the var on all updates after initialization. Allows for
* the use of the current var when updating.
* @param varNameOverride
* A name for this variable, used in logging. If unspecified, the simple class name of T will be
* used.
Expand All @@ -46,6 +49,7 @@ class AutoUpdatingVar[U[_], R[_], T](periodic: Periodic[U, R])(
updateAttemptStrategy: AttemptStrategy,
blockUntilReadyTimeout: Option[Duration] = None,
handleInitializationError: PartialFunction[Throwable, U[T]] = PartialFunction.empty,
updateExistingVar: Option[T => U[T]] = None,
varNameOverride: Option[String] = None
)(implicit ct: ClassTag[T])
extends AutoCloseable {
Expand All @@ -71,7 +75,10 @@ class AutoUpdatingVar[U[_], R[_], T](periodic: Periodic[U, R])(
log,
"update var",
updateInterval.duration(newV),
() => updateVar,
updateExistingVar match {
case Some(u) => () => u(variable.get) // will always be set at this point
case None => () => updateVar
},
v => variable = Some(v),
v => updateInterval.duration(v),
updateAttemptStrategy
Expand Down Expand Up @@ -115,6 +122,7 @@ object AutoUpdatingVar {
updateAttemptStrategy: AttemptStrategy,
blockUntilReadyTimeout: Option[Duration] = None,
handleInitializationError: PartialFunction[Throwable, U[T]] = PartialFunction.empty,
updateExistingVar: Option[T => U[T]] = None,
varNameOverride: Option[String] = None
)(implicit ct: ClassTag[T]): AutoUpdatingVar[U, R, T] = {
new AutoUpdatingVar(periodic)(
Expand All @@ -123,6 +131,7 @@ object AutoUpdatingVar {
updateAttemptStrategy,
blockUntilReadyTimeout,
handleInitializationError,
updateExistingVar,
varNameOverride
)
}
Expand All @@ -140,6 +149,7 @@ object AutoUpdatingVar {
updateAttemptStrategy: AttemptStrategy,
blockUntilReadyTimeout: Option[Duration] = None,
handleInitializationError: PartialFunction[Throwable, T] = PartialFunction.empty,
updateExistingVar: Option[T => T] = None,
varNameOverride: Option[String] = None,
executorOverride: Option[ScheduledExecutorService] = None
)(implicit ct: ClassTag[T]): AutoUpdatingVar[Identity, Future, T] = {
Expand All @@ -151,6 +161,7 @@ object AutoUpdatingVar {
updateAttemptStrategy,
blockUntilReadyTimeout,
handleInitializationError,
updateExistingVar,
varNameOverride
)
}
Expand All @@ -168,6 +179,7 @@ object AutoUpdatingVar {
updateAttemptStrategy: AttemptStrategy,
blockUntilReadyTimeout: Option[Duration] = None,
handleInitializationError: PartialFunction[Throwable, Future[T]] = PartialFunction.empty,
updateExistingVar: Option[T => Future[T]] = None,
varNameOverride: Option[String] = None,
executorOverride: Option[ScheduledExecutorService] = None
)(implicit ct: ClassTag[T]): AutoUpdatingVar[Future, Future, T] = {
Expand All @@ -179,6 +191,7 @@ object AutoUpdatingVar {
updateAttemptStrategy,
blockUntilReadyTimeout,
handleInitializationError,
updateExistingVar,
varNameOverride
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ trait AutoUpdatingVarTestsFuture[U[_]] extends FunSuite {
implicit val per = periodic
testBasicsWithBlocking()

testUpdateExistingVar()

testAdjustsUpdateInterval()

testReturnsFailedReady()
Expand Down Expand Up @@ -94,6 +96,39 @@ trait AutoUpdatingVarTestsFuture[U[_]] extends FunSuite {
}
}

def testUpdateExistingVar(
)(implicit
loc: munit.Location,
periodic: () => Periodic[U, Future]
): Unit = {
FunFixture(
_ => {
val holder = new VarHolder
val v = new AutoUpdatingVar(periodic())(
holder.get,
UpdateInterval.Static(1.seconds),
AttemptStrategy.Infinite(1.second),
Some(1.second),
updateExistingVar = Some((i: Int) => { assertEquals(i, 1); pureU(100) })
)
(v, holder)
},
(f: (AutoCloseable, VarHolder)) => f._1.close()
)
.test("uses updateExistingVar after initialization if specified") { case (v, holder) =>
assert(v.ready.isCompleted)
assertEquals(v.ready.value, Some(Success(())))

assertEquals(v.latest, 1)

Thread.sleep(1100)

assertEquals(v.latest, 100)

assertEquals(evalU(holder.get), 2)
}
}

def testAdjustsUpdateInterval(
)(implicit
loc: munit.Location,
Expand Down

0 comments on commit 8e23a89

Please sign in to comment.