diff --git a/staticlibs/tests/unit/src/com/android/net/module/util/TrackRecordTest.kt b/staticlibs/tests/unit/src/com/android/net/module/util/TrackRecordTest.kt index 8e320d0d4e..d18ae9cf29 100644 --- a/staticlibs/tests/unit/src/com/android/net/module/util/TrackRecordTest.kt +++ b/staticlibs/tests/unit/src/com/android/net/module/util/TrackRecordTest.kt @@ -184,6 +184,25 @@ class TrackRecordTest { assertTrue(delay >= SHORT_TIMEOUT) } + @Test + fun testPollFrom() { + val readHead = ArrayTrackRecord().newReadHead() + readHead.add(3) + readHead.add(6) + readHead.add(7) + readHead.add(10) + assertEquals(0, readHead.mark) + assertEquals(3, readHead.poll(timeoutMs = 1, pos = 0) { it == 3 }) + assertEquals(1, readHead.mark) + assertEquals(7, readHead.poll(timeoutMs = 1, pos = 0) { it == 7 }) + assertEquals(3, readHead.mark) + // Do not advance the read head since the found element is after the current head + assertEquals(6, readHead.poll(timeoutMs = 1, pos = 0) { it == 6 }) + assertEquals(3, readHead.mark) + assertNull(readHead.poll(timeoutMs = 1, pos = 1) { it == 3 }) + assertEquals(4, readHead.mark) + } + @Test fun testConcurrentPollDisallowed() { val failures = AtomicInteger(0) diff --git a/staticlibs/testutils/hostdevice/com/android/net/module/util/TrackRecord.kt b/staticlibs/testutils/hostdevice/com/android/net/module/util/TrackRecord.kt index 541fccad1f..123efd7283 100644 --- a/staticlibs/testutils/hostdevice/com/android/net/module/util/TrackRecord.kt +++ b/staticlibs/testutils/hostdevice/com/android/net/module/util/TrackRecord.kt @@ -315,7 +315,7 @@ class ArrayTrackRecord : TrackRecord { val index = pollForIndexReadLocked(timeoutMs, pos, predicate) when { index < 0 -> readHead = size - index > readHead -> readHead = index + 1 + index >= readHead -> readHead = index + 1 // else do nothing, the element was found prior to the read head so // don't move the read head }