1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.systemui.util.kotlin 18 19 import android.testing.AndroidTestingRunner 20 import androidx.test.filters.SmallTest 21 import com.android.systemui.SysuiTestCase 22 import com.google.common.truth.Truth.assertThat 23 import kotlinx.coroutines.CompletableDeferred 24 import kotlinx.coroutines.async 25 import kotlinx.coroutines.awaitCancellation 26 import kotlinx.coroutines.runBlocking 27 import org.junit.Test 28 import org.junit.runner.RunWith 29 30 @SmallTest 31 @RunWith(AndroidTestingRunner::class) 32 class RaceSuspendTest : SysuiTestCase() { 33 @Test 34 fun raceSimple() = runBlocking { 35 val winner = CompletableDeferred<Int>() 36 val result = async { 37 race( 38 { winner.await() }, 39 { awaitCancellation() }, 40 ) 41 } 42 winner.complete(1) 43 assertThat(result.await()).isEqualTo(1) 44 } 45 46 @Test 47 fun raceImmediate() = runBlocking { 48 assertThat( 49 race<Int>( 50 { 1 }, 51 { 2 }, 52 ) 53 ).isEqualTo(1) 54 } 55 } 56