Fix a bug in poll(from)

Test: new test in this patch
Flag: EXEMPT test-only
Change-Id: I00d367ce9db47e3fd30d27816b68280ab418f988
This commit is contained in:
Chalard Jean 2025-05-07 13:46:17 +09:00
commit f0252b1cf4
2 changed files with 20 additions and 1 deletions

View file

@ -184,6 +184,25 @@ class TrackRecordTest {
assertTrue(delay >= SHORT_TIMEOUT)
}
@Test
fun testPollFrom() {
val readHead = ArrayTrackRecord<Int>().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)

View file

@ -315,7 +315,7 @@ class ArrayTrackRecord<E> : TrackRecord<E> {
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
}