Skip to content

Commit de0d39e

Browse files
committed
Make reachable test robust
Make the test robust if the reference is cleared early. The utility assertion about reachability is OK with null, which just looks like a cleared ref.
1 parent a3791d4 commit de0d39e

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

test/junit/scala/collection/IteratorTest.scala

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ class IteratorTest {
631631

632632
@Test def `flatMap is memory efficient in previous element`(): Unit = {
633633
import java.lang.ref._
634+
import scala.util.chaining._
634635
// Array.iterator holds onto array reference; by contrast, iterating over List walks tail.
635636
// Avoid reaching seq1 through test class. Avoid testing Array.iterator.
636637
class C extends Iterable[String] {
@@ -641,11 +642,7 @@ class IteratorTest {
641642

642643
def hasNext = i < ss.length
643644

644-
def next() =
645-
if (hasNext) {
646-
val res = ss(i); i += 1; res
647-
}
648-
else Iterator.empty.next()
645+
def next() = if (hasNext) ss(i).tap(_ => i += 1) else Iterator.empty.next()
649646
}
650647

651648
def apply(i: Int) = ss(i)
@@ -654,13 +651,13 @@ class IteratorTest {
654651
val seq2 = List("third")
655652
val it0: Iterator[Int] = Iterator(1, 2)
656653
lazy val it: Iterator[String] = it0.flatMap {
657-
case 1 => seq1.get
654+
case 1 => Option(seq1.get).getOrElse(Nil)
658655
case _ => check(); seq2
659656
}
660657

661658
def check() = assertNotReachable(seq1.get, it)(())
662659

663-
def checkHasElement() = assertNotReachable(seq1.get.apply(1), it)(())
660+
def checkHasElement() = assertNotReachable(Option(seq1.get).map(_.apply(1)).orNull, it)(())
664661

665662
assert(it.hasNext)
666663
assertEquals("first", it.next())

0 commit comments

Comments
 (0)