From f0252b1cf4e8612fede21fb2d4915c55c092c18c Mon Sep 17 00:00:00 2001 From: Chalard Jean Date: Wed, 7 May 2025 13:46:17 +0900 Subject: [PATCH] Fix a bug in poll(from) Test: new test in this patch Flag: EXEMPT test-only Change-Id: I00d367ce9db47e3fd30d27816b68280ab418f988 --- .../net/module/util/TrackRecordTest.kt | 19 +++++++++++++++++++ .../android/net/module/util/TrackRecord.kt | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) 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 }