Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4840a772 authored by Chalard Jean's avatar Chalard Jean Committed by Gerrit Code Review
Browse files

Merge "Add a test for concurrent access"

parents 8a132765 eccb17b4
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import java.util.concurrent.CyclicBarrier
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
import kotlin.system.measureTimeMillis
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
@@ -182,6 +183,33 @@ class TrackRecordTest {
        assertTrue(delay >= SHORT_TIMEOUT)
    }

    @Test
    fun testConcurrentPollDisallowed() {
        val failures = AtomicInteger(0)
        val readHead = ArrayTrackRecord<Int>().newReadHead()
        val barrier = CyclicBarrier(2)
        Thread {
            barrier.await(LONG_TIMEOUT, TimeUnit.MILLISECONDS) // barrier 1
            try {
                readHead.poll(LONG_TIMEOUT)
            } catch (e: ConcurrentModificationException) {
                failures.incrementAndGet()
                // Unblock the other thread
                readHead.add(0)
            }
        }.start()
        barrier.await() // barrier 1
        try {
            readHead.poll(LONG_TIMEOUT)
        } catch (e: ConcurrentModificationException) {
            failures.incrementAndGet()
            // Unblock the other thread
            readHead.add(0)
        }
        // One of the threads must have gotten an exception.
        assertEquals(failures.get(), 1)
    }

    @Test
    fun testPollWakesUp() {
        val record = ArrayTrackRecord<Int>()