Skip to content

Commit 63829ef

Browse files
committed
Workaround #4524 by synchronizing tasks set
1 parent d283484 commit 63829ef

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

kernel-testkit/shared/src/main/scala/cats/effect/kernel/testkit/TestContext.scala

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import scala.concurrent.duration._
2424
import scala.util.{Random, Try}
2525
import scala.util.control.NonFatal
2626

27-
import java.util.{Base64, Comparator}
27+
import java.util.{Base64, Comparator, Collections}
2828
import java.util.concurrent.ConcurrentSkipListSet
2929
import java.util.concurrent.atomic.{AtomicLong, AtomicReference}
3030

@@ -60,11 +60,11 @@ import java.util.concurrent.atomic.{AtomicLong, AtomicReference}
6060
* }}}
6161
*/
6262
final class TestContext private (_seed: Long) extends ExecutionContext { self =>
63-
import TestContext.{ConcurrentState, Encoder, State, Task}
63+
import TestContext.{InternalState, Encoder, State, Task}
6464

6565
private[this] val random = new Random(_seed)
6666

67-
private[this] val stateRef = new ConcurrentState()
67+
private[this] val stateRef = new InternalState()
6868

6969
def execute(runnable: Runnable): Unit = {
7070
val current = stateRef
@@ -280,13 +280,21 @@ object TestContext {
280280
def apply(seed: String): TestContext =
281281
new TestContext(new String(Decoder.decode(seed)).toLong)
282282

283+
@deprecated("Not intended to be public", "3.7.1")
283284
final class ConcurrentState(
284285
val currentID: AtomicLong = new AtomicLong(),
285286
val currentNanos: AtomicLong = new AtomicLong(),
286287
val tasks: ConcurrentSkipListSet[Task] = new ConcurrentSkipListSet(Task.comparator),
287288
val lastReportedFailure: AtomicReference[Throwable] = new AtomicReference()
288289
)
289290

291+
private final class InternalState(
292+
val currentID: AtomicLong = new AtomicLong(),
293+
val currentNanos: AtomicLong = new AtomicLong(),
294+
val tasks: java.util.SortedSet[Task] = Collections.synchronizedSortedSet(new ConcurrentSkipListSet(Task.comparator)),
295+
val lastReportedFailure: AtomicReference[Throwable] = new AtomicReference()
296+
)
297+
290298
/**
291299
* Used internally by [[TestContext]], represents the internal state used for task scheduling
292300
* and execution.

0 commit comments

Comments
 (0)