Skip to content

Commit a977abd

Browse files
committed
Reachable check is more short-circuited
1 parent de0d39e commit a977abd

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/testkit/scala/tools/testkit/AssertUtil.scala

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,24 @@ object AssertUtil {
107107
*/
108108
def assertNotReachable[A <: AnyRef](a: => A, roots: AnyRef*)(body: => Unit): Unit = {
109109
val wkref = new WeakReference(a)
110-
def refs(root: AnyRef): mutable.Set[AnyRef] = {
110+
// fail if following strong references from root discovers referent. Quit if ref is empty.
111+
def assertNoRef(root: AnyRef): Unit = {
111112
val seen = new IdentityHashMap[AnyRef, Unit]
112113
def loop(o: AnyRef): Unit =
113114
if (wkref.nonEmpty && o != null && !seen.containsKey(o)) {
114115
seen.put(o, ())
116+
assertTrue(s"Root $root held reference $o", o ne wkref.get)
115117
for {
116118
f <- o.getClass.allFields
117119
if !Modifier.isStatic(f.getModifiers)
118120
if !f.getType.isPrimitive
119121
if !classOf[Reference[_]].isAssignableFrom(f.getType)
120-
} loop(f follow o)
122+
} loop(f.follow(o))
121123
}
122124
loop(root)
123-
seen.keySet.asScala
124125
}
125126
body
126-
for (r <- roots if wkref.nonEmpty) {
127-
assertFalse(s"Root $r held reference", refs(r) contains wkref.get)
128-
}
127+
roots.foreach(assertNoRef)
129128
}
130129

131130
/** Assert no new threads, with some margin for arbitrary threads to exit. */

test/junit/scala/collection/IteratorTest.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,8 @@ class IteratorTest {
652652
val it0: Iterator[Int] = Iterator(1, 2)
653653
lazy val it: Iterator[String] = it0.flatMap {
654654
case 1 => Option(seq1.get).getOrElse(Nil)
655-
case _ => check(); seq2
655+
case 2 => check(); seq2
656+
case _ => ???
656657
}
657658

658659
def check() = assertNotReachable(seq1.get, it)(())

0 commit comments

Comments
 (0)