Skip to content

Commit 6e03d4d

Browse files
committed
Merge branch 'benjumanji-single-thread-ultimately' into 3.1.x
2 parents 2222aa3 + 0ec3649 commit 6e03d4d

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

.jvmopts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
-XX:+CMSClassUnloadingEnabled
66
-XX:+UseConcMarkSweepGC
77
-XX:+CMSIncrementalMode
8-
-XX:NewRatio=8 -XX:MaxPermSize=512M
8+
-XX:NewRatio=8
9+
-XX:MaxPermSize=512M

scalatest-test/src/test/scala/org/scalatest/concurrent/UltimatelySpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class UltimatelySpec extends AsyncFunSpec with Matchers with OptionValues {
201201
}
202202
}
203203
} map { _ =>
204-
(System.currentTimeMillis - startTime.get).toInt should be >= (1388)
204+
(System.currentTimeMillis - startTime.get).toInt should be >= (1388 - 10) // - 10 to give it a little wiggle room
205205
}
206206
}
207207

scalatest/src/main/scala/org/scalatest/concurrent/Ultimately.scala

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import org.scalactic.source
2525
import org.scalatest.exceptions.StackDepthException
2626
import scala.concurrent.{Future, Promise, ExecutionContext}
2727
import scala.util.{Success, Failure}
28+
import java.util.concurrent.{ Executors, TimeUnit, ThreadFactory }
29+
import java.util.concurrent.ScheduledExecutorService
2830

2931
/**
3032
* Trait that provides the <code>ultimately</code> construct, which periodically retries executing
@@ -280,6 +282,8 @@ import scala.util.{Success, Failure}
280282
*/
281283
trait Ultimately extends PatienceConfiguration {
282284

285+
286+
283287
/**
284288
* Invokes the passed by-name parameter repeatedly until it either succeeds, or a configured maximum
285289
* amount of time has passed, sleeping a configured interval between attempts.
@@ -427,7 +431,7 @@ trait Ultimately extends PatienceConfiguration {
427431
val promise = Promise[T]
428432

429433
val task =
430-
new TimerTask {
434+
new Runnable {
431435
override def run(): Unit = {
432436
val newFut = tryTryAgain(attempt + 1)
433437
newFut onComplete {
@@ -437,8 +441,7 @@ trait Ultimately extends PatienceConfiguration {
437441
}
438442
}
439443

440-
val timer = new Timer
441-
timer.schedule(task, chillTime)
444+
Ultimately.scheduler.schedule(task, chillTime, TimeUnit.MILLISECONDS)
442445
promise.future
443446
}
444447
else { // Timed out so return a failed Future
@@ -501,4 +504,17 @@ trait Ultimately extends PatienceConfiguration {
501504
* ...
502505
* </pre>
503506
*/
504-
object Ultimately extends Ultimately
507+
object Ultimately extends Ultimately {
508+
private lazy val scheduler: ScheduledExecutorService = {
509+
val threadFactory = new ThreadFactory {
510+
val inner = Executors.defaultThreadFactory()
511+
def newThread(runnable: Runnable) = {
512+
val thread = inner.newThread(runnable)
513+
thread.setDaemon(true)
514+
thread
515+
}
516+
}
517+
518+
Executors.newSingleThreadScheduledExecutor(threadFactory)
519+
}
520+
}

0 commit comments

Comments
 (0)