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 java.util.concurrent.atomic.AtomicLong
23 import kotlinx.coroutines.CoroutineStart
24 import kotlinx.coroutines.Dispatchers
25 import kotlinx.coroutines.launch
26 import kotlinx.coroutines.runBlocking
27 import kotlinx.coroutines.withContext
28 import org.junit.Assert.assertTrue
29 import org.junit.Ignore
30 import org.junit.Test
31 import org.junit.runner.RunWith
32 
33 @SmallTest
34 @RunWith(AndroidTestingRunner::class)
35 class IpcSerializerTest : SysuiTestCase() {
36 
37     private val serializer = IpcSerializer()
38 
39     @Ignore("b/253046405")
40     @Test
41     fun serializeManyIncomingIpcs(): Unit = runBlocking(Dispatchers.Main.immediate) {
42         val processor = launch(start = CoroutineStart.LAZY) { serializer.process() }
43         withContext(Dispatchers.IO) {
44             val lastEvaluatedTime = AtomicLong(System.currentTimeMillis())
45             // First, launch many serialization requests in parallel
46             repeat(100_000) {
47                 launch(Dispatchers.Unconfined) {
48                     val enqueuedTime = System.currentTimeMillis()
49                     serializer.runSerialized {
50                         val last = lastEvaluatedTime.getAndSet(enqueuedTime)
51                         assertTrue(
52                             "expected $last less than or equal to $enqueuedTime ",
53                             last <= enqueuedTime,
54                         )
55                     }
56                 }
57             }
58             // Then, process them all in the order they came in.
59             processor.start()
60         }
61         // All done, stop processing
62         processor.cancel()
63     }
64 
65     @Test(timeout = 5000)
66     fun serializeOnOneThread_doesNotDeadlock() = runBlocking {
67         val job = launch { serializer.process() }
68         repeat(100) {
69             serializer.runSerializedBlocking { }
70         }
71         job.cancel()
72     }
73 }
74