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

Commit 7d7960ad authored by Chalard Jean's avatar Chalard Jean
Browse files

Improve testable utils.

Test: m gts && gts-tradefed run gts --module GtsNetworkStackHostTestCases
Bug: 150643374
Change-Id: I06255a217dc69c2f29af23efb93d639b699229f3
Merged-In: Ib1c4d8814d3199b512a552ea4c92fedad42594a0
(cherry picked from commit 1da24b86cc99f22b80bf1a640cadd9d5fb9d9199)
parent 12e86a09
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -196,7 +196,13 @@ class ArrayTrackRecord<E> : TrackRecord<E> {
        /**
         * @return the current value of the mark.
         */
        val mark get() = readHead.also { checkThread() }
        var mark
            get() = readHead.also { checkThread() }
            set(v: Int) = rewind(v)
        fun rewind(v: Int) {
            checkThread()
            readHead = v
        }

        private fun checkThread() = check(Thread.currentThread() == owningThread) {
            "Must be called by the thread that created this object"
+16 −3
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import kotlin.test.assertTrue
import kotlin.test.fail

object NULL_NETWORK : Network(-1)
object ANY_NETWORK : Network(-2)

private val Int.capabilityName get() = NetworkCapabilities.capabilityNameOf(this)

@@ -100,7 +101,8 @@ open class RecorderCallback private constructor(
        }
    }

    protected val history = backingRecord.newReadHead()
    val history = backingRecord.newReadHead()
    val mark get() = history.mark

    override fun onAvailable(network: Network) {
        history.add(Available(network))
@@ -172,17 +174,28 @@ open class TestableNetworkCallback private constructor(
        if (null != cb) fail("Expected no callback but got $cb")
    }

    // Expects a callback of the specified type on the specified network within the timeout.
    // If no callback arrives, or a different callback arrives, fail. Returns the callback.
    inline fun <reified T : CallbackEntry> expectCallback(
        network: Network,
        network: Network = ANY_NETWORK,
        timeoutMs: Long = defaultTimeoutMs
    ): T = pollForNextCallback(timeoutMs).let {
        if (it !is T || it.network != network) {
        if (it !is T || (ANY_NETWORK !== network && it.network != network)) {
            fail("Unexpected callback : $it, expected ${T::class} with Network[$network]")
        } else {
            it
        }
    }

    // Expects a callback of the specified type matching the predicate within the timeout.
    // Any callback that doesn't match the predicate will be skipped. Fails only if
    // no matching callback is received within the timeout.
    inline fun <reified T : CallbackEntry> eventuallyExpect(
        timeoutMs: Long = defaultTimeoutMs,
        from: Int = mark,
        crossinline predicate: (T) -> Boolean = { true }
    ) = history.poll(timeoutMs, from) { it is T && predicate(it) } as T

    fun expectCallbackThat(
        timeoutMs: Long = defaultTimeoutMs,
        valid: (CallbackEntry) -> Boolean
+43 −9
Original line number Diff line number Diff line
@@ -18,18 +18,23 @@ package com.android.testutils

import android.net.netstats.provider.NetworkStatsProvider
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.test.fail

private const val DEFAULT_TIMEOUT_MS = 200L

open class TestableNetworkStatsProvider : NetworkStatsProvider() {
open class TestableNetworkStatsProvider(
    val defaultTimeoutMs: Long = DEFAULT_TIMEOUT_MS
) : NetworkStatsProvider() {
    sealed class CallbackType {
        data class OnRequestStatsUpdate(val token: Int) : CallbackType()
        data class OnSetLimit(val iface: String?, val quotaBytes: Long) : CallbackType()
        data class OnSetAlert(val quotaBytes: Long) : CallbackType()
    }

    private val history = ArrayTrackRecord<CallbackType>().ReadHead()
    val history = ArrayTrackRecord<CallbackType>().newReadHead()
    // See ReadHead#mark
    val mark get() = history.mark

    override fun onRequestStatsUpdate(token: Int) {
        history.add(CallbackType.OnRequestStatsUpdate(token))
@@ -43,20 +48,49 @@ open class TestableNetworkStatsProvider : NetworkStatsProvider() {
        history.add(CallbackType.OnSetAlert(quotaBytes))
    }

    fun expectOnRequestStatsUpdate(token: Int) {
        assertEquals(CallbackType.OnRequestStatsUpdate(token), history.poll(DEFAULT_TIMEOUT_MS))
    fun expectOnRequestStatsUpdate(token: Int, timeout: Long = defaultTimeoutMs) {
        assertEquals(CallbackType.OnRequestStatsUpdate(token), history.poll(timeout))
    }

    fun expectOnSetLimit(iface: String?, quotaBytes: Long) {
        assertEquals(CallbackType.OnSetLimit(iface, quotaBytes), history.poll(DEFAULT_TIMEOUT_MS))
    fun expectOnSetLimit(iface: String?, quotaBytes: Long, timeout: Long = defaultTimeoutMs) {
        assertEquals(CallbackType.OnSetLimit(iface, quotaBytes), history.poll(timeout))
    }

    fun expectOnSetAlert(quotaBytes: Long) {
        assertEquals(CallbackType.OnSetAlert(quotaBytes), history.poll(DEFAULT_TIMEOUT_MS))
    fun expectOnSetAlert(quotaBytes: Long, timeout: Long = defaultTimeoutMs) {
        assertEquals(CallbackType.OnSetAlert(quotaBytes), history.poll(timeout))
    }

    fun pollForNextCallback(timeout: Long = defaultTimeoutMs) =
        history.poll(timeout) ?: fail("Did not receive callback after ${timeout}ms")

    inline fun <reified T : CallbackType> expectCallback(
        timeout: Long = defaultTimeoutMs,
        predicate: (T) -> Boolean = { true }
    ): T {
        return pollForNextCallback(timeout).also { assertTrue(it is T && predicate(it)) } as T
    }

    // Expects a callback of the specified type matching the predicate within the timeout.
    // Any callback that doesn't match the predicate will be skipped. Fails only if
    // no matching callback is received within the timeout.
    // TODO : factorize the code for this with the identical call in TestableNetworkCallback.
    // There should be a common superclass doing this generically.
    // TODO : have a better error message to have this fail. Right now the failure when no
    // matching callback arrives comes from the casting to a non-nullable T.
    // TODO : in fact, completely removing this method and have clients use
    // history.poll(timeout, index, predicate) directly might be simpler.
    inline fun <reified T : CallbackType> eventuallyExpect(
        timeoutMs: Long = defaultTimeoutMs,
        from: Int = mark,
        crossinline predicate: (T) -> Boolean = { true }
    ) = history.poll(timeoutMs, from) { it is T && predicate(it) } as T

    fun drainCallbacks() {
        history.mark = history.size
    }

    @JvmOverloads
    fun assertNoCallback(timeout: Long = DEFAULT_TIMEOUT_MS) {
    fun assertNoCallback(timeout: Long = defaultTimeoutMs) {
        val cb = history.poll(timeout)
        cb?.let { fail("Expected no callback but got $cb") }
    }