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

Commit 980e842c authored by Chalard Jean's avatar Chalard Jean Committed by Automerger Merge Worker
Browse files

Merge "Add a test for concurrent access" am: 4840a772

Original change: https://android-review.googlesource.com/c/platform/packages/modules/NetworkStack/+/1533036

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: Iafc8b4bf4717353011714c06dbeae5e90cf2e9c5
parents 2c43e2b4 4840a772
Loading
Loading
Loading
Loading
+28 −0
Original line number Original line Diff line number Diff line
@@ -30,6 +30,7 @@ import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.runners.JUnit4
import java.util.concurrent.CyclicBarrier
import java.util.concurrent.CyclicBarrier
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
import kotlin.system.measureTimeMillis
import kotlin.system.measureTimeMillis
import kotlin.test.assertEquals
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertFailsWith
@@ -182,6 +183,33 @@ class TrackRecordTest {
        assertTrue(delay >= SHORT_TIMEOUT)
        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
    @Test
    fun testPollWakesUp() {
    fun testPollWakesUp() {
        val record = ArrayTrackRecord<Int>()
        val record = ArrayTrackRecord<Int>()