-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
283 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
org.slf4j.simpleLogger.logFile=./periodic-jdk/log/test.log | ||
org.slf4j.simpleLogger.showDateTime=true |
251 changes: 251 additions & 0 deletions
251
periodic-jdk/src/test/scala/ca/dvgi/periodic/jdk/JdkAutoUpdaterTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,251 @@ | ||
package ca.dvgi.periodic.jdk | ||
|
||
import ca.dvgi.periodic._ | ||
import scala.concurrent.duration._ | ||
import scala.util.Success | ||
import org.slf4j.LoggerFactory | ||
import scala.concurrent.Await | ||
// import java.util.concurrent.Executors | ||
|
||
class JdkAutoUpdaterTest extends munit.FunSuite { | ||
|
||
private val log = LoggerFactory.getLogger(getClass) | ||
|
||
case object TestException extends RuntimeException | ||
|
||
class VarHolder { | ||
private var v = 1 | ||
def get: Int = { | ||
log.info("getting") | ||
val r = v | ||
v = v + 1 | ||
r | ||
} | ||
} | ||
|
||
class VarErrorHolder { | ||
var attempts = 0 | ||
def get: Int = { | ||
attempts = attempts + 1 | ||
sys.error("test exception") | ||
} | ||
} | ||
|
||
FunFixture( | ||
_ => { | ||
val holder = new VarHolder | ||
val v = new AutoUpdatingVar(new JdkAutoUpdater[Int](Some(5.seconds)))( | ||
holder.get, | ||
UpdateInterval.Static(1.seconds), | ||
UpdateAttemptStrategy.Infinite(1.second) | ||
) | ||
(v, holder) | ||
}, | ||
(f: (AutoCloseable, VarHolder)) => f._1.close() | ||
) | ||
.test("periodically updates the var, blockng on start, and closes") { case (v, holder) => | ||
assert(v.ready.isCompleted) | ||
assertEquals(v.ready.value, Some(Success(()))) | ||
|
||
assertEquals(v.latest, 1) | ||
assertEquals(v.latest, 1) // value should still be cached | ||
|
||
Thread.sleep(1100) | ||
|
||
assertEquals(v.latest, 2) | ||
assertEquals(v.latest, 2) | ||
|
||
Thread.sleep(1000) | ||
|
||
assertEquals(v.latest, 3) | ||
assertEquals(v.latest, 3) | ||
|
||
v.close() | ||
|
||
Thread.sleep(1000) | ||
assertEquals(holder.get, 4) | ||
} | ||
|
||
FunFixture( | ||
_ => { | ||
val holder = new VarHolder | ||
val v = new AutoUpdatingVar(new JdkAutoUpdater[Int](Some(1.second)))( | ||
holder.get, | ||
UpdateInterval.Dynamic((i: Int) => i * 1.second), | ||
UpdateAttemptStrategy.Infinite(1.second) | ||
) | ||
(v, holder) | ||
}, | ||
(f: (AutoCloseable, VarHolder)) => f._1.close() | ||
) | ||
.test("adjusts the update interval based on the returned value") { case (v, _) => | ||
assert(v.ready.isCompleted) | ||
assertEquals(v.ready.value, Some(Success(()))) | ||
|
||
assertEquals(v.latest, 1) | ||
assertEquals(v.latest, 1) // value should still be cached | ||
|
||
Thread.sleep(1100) | ||
|
||
assertEquals(v.latest, 2) | ||
assertEquals(v.latest, 2) | ||
|
||
Thread.sleep(1000) | ||
|
||
assertEquals(v.latest, 2) // still 2 since update shouldn't have happened yet | ||
|
||
Thread.sleep(1000) | ||
|
||
assertEquals(v.latest, 3) | ||
} | ||
|
||
FunFixture( | ||
_ => { | ||
new AutoUpdatingVar(new JdkAutoUpdater[Int]())( | ||
throw TestException, | ||
UpdateInterval.Static(1.seconds), | ||
UpdateAttemptStrategy.Infinite(1.second) | ||
) | ||
}, | ||
(f: AutoCloseable) => f.close() | ||
).test("returns a failed future from ready if the first update fails") { v => | ||
intercept[TestException.type] { Await.result(v.ready, 1.second) } | ||
v.close | ||
} | ||
|
||
FunFixture( | ||
_ => { | ||
new AutoUpdatingVar(new JdkAutoUpdater[Int]())( | ||
{ | ||
Thread.sleep(1000) | ||
1 | ||
}, | ||
UpdateInterval.Static(1.seconds), | ||
UpdateAttemptStrategy.Infinite(1.second) | ||
) | ||
}, | ||
(f: AutoCloseable) => f.close() | ||
).test("throws an exception if latest called before var is initialized") { v => | ||
intercept[UnreadyAutoUpdatingVarException.type] { v.latest } | ||
} | ||
|
||
test( | ||
"returns a failed future from constructor if the first update fails and instructed to block" | ||
) { | ||
intercept[TestException.type] { | ||
new AutoUpdatingVar(new JdkAutoUpdater[Int](Some(1.second)))( | ||
throw TestException, | ||
UpdateInterval.Static(1.seconds), | ||
UpdateAttemptStrategy.Infinite(1.second) | ||
) | ||
} | ||
} | ||
|
||
// test( | ||
// "handles initialization errors" | ||
// ) { | ||
// case object TestException extends RuntimeException | ||
|
||
// val v = | ||
// new JdkAutoUpdatingVar( | ||
// throw TestException, | ||
// UpdateInterval.Static(1.seconds), | ||
// UpdateAttemptStrategy.Infinite(1.second), | ||
// Some(1.second), | ||
// { case _ => | ||
// 42 | ||
// } | ||
// ) | ||
|
||
// assertEquals(v.latest, 42) | ||
// v.close() | ||
// } | ||
|
||
// test("does infinite reattempts") { | ||
// val holder = new VarErrorHolder | ||
// val v = | ||
// new JdkAutoUpdatingVar( | ||
// holder.get, | ||
// UpdateInterval.Static(1.second), | ||
// UpdateAttemptStrategy.Infinite(1.second), | ||
// Some(1.second), | ||
// { case _ => | ||
// 42 | ||
// } | ||
// ) | ||
|
||
// assertEquals(v.latest, 42) | ||
// assertEquals(holder.attempts, 1) | ||
|
||
// Thread.sleep(1100) | ||
|
||
// assertEquals(v.latest, 42) | ||
// assertEquals(holder.attempts, 2) | ||
|
||
// Thread.sleep(1000) | ||
|
||
// assertEquals(v.latest, 42) | ||
// assertEquals(holder.attempts, 3) | ||
|
||
// v.close() | ||
// } | ||
|
||
// test("does finite reattempts") { | ||
// val holder = new VarErrorHolder | ||
// var terminated = false | ||
// val v = | ||
// new JdkAutoUpdatingVar( | ||
// holder.get, | ||
// UpdateInterval.Static(1.second), | ||
// UpdateAttemptStrategy | ||
// .Finite(1.second, 2, UpdateAttemptExhaustionBehavior.Custom(_ => terminated = true)), | ||
// Some(1.second), | ||
// { case _ => | ||
// 42 | ||
// } | ||
// ) | ||
|
||
// assertEquals(v.latest, 42) | ||
// assertEquals(holder.attempts, 1) | ||
// assertEquals(terminated, false) | ||
|
||
// Thread.sleep(1100) | ||
|
||
// assertEquals(v.latest, 42) | ||
// assertEquals(holder.attempts, 2) | ||
// assertEquals(terminated, false) | ||
|
||
// Thread.sleep(1000) | ||
|
||
// assertEquals(v.latest, 42) | ||
// assertEquals(holder.attempts, 3) | ||
// assertEquals(terminated, true) | ||
|
||
// Thread.sleep(1000) | ||
|
||
// assertEquals(holder.attempts, 3) | ||
|
||
// v.close() | ||
// } | ||
|
||
// test("can use an external SchedulerExecutorService") { | ||
// val holder = new VarHolder | ||
// val ses = Executors.newScheduledThreadPool(1) | ||
|
||
// val v = new JdkAutoUpdatingVar( | ||
// holder.get, | ||
// UpdateInterval.Static(2.seconds), | ||
// UpdateAttemptStrategy.Infinite(1.second), | ||
// Some(1.second), | ||
// executorOverride = Some(ses) | ||
// ) | ||
|
||
// assertEquals(v.latest, 1) | ||
|
||
// v.close() | ||
// assert(!ses.isShutdown()) | ||
|
||
// Thread.sleep(5000) | ||
// assertEquals(holder.get, 2) | ||
// } | ||
} |
Oops, something went wrong.