[BadCase]代码提交

This commit is contained in:
renwenjie
2022-01-19 15:54:30 +08:00
parent 63b99ee896
commit 48d9baa3bb
29 changed files with 1304 additions and 107 deletions

View File

@@ -0,0 +1,145 @@
package com.mogo.functions.test
import androidx.lifecycle.lifecycleScope
import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import com.mogo.eagle.core.data.autopilot.AutoPilotRecordResult
import com.mogo.eagle.core.function.hmi.ui.MoGoHmiFragment
import com.mogo.eagle.core.function.main.MainLauncherActivity
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import kotlin.random.Random
@RunWith(AndroidJUnit4::class)
@LargeTest
class AutoPilotBadCaseTest {
lateinit var launch: ActivityScenario<MainLauncherActivity>
@Before
fun launch() {
launch = ActivityScenario.launch(MainLauncherActivity::class.java)
}
@ExperimentalCoroutinesApi @Test
fun showBadCaseEntrance1(): Unit = runBlocking(Dispatchers.Main) {
val f = ensureMoGoHmiFragmentShow()
var index = 0
(1 until 50)
.map { it }
.asFlow()
.onEach {
delay(TimeUnit.SECONDS.toMillis(5))
f.onAutopilotRecordResult(AutoPilotRecordResult().also {
it.diskFree = 100 + index
it.duration = 60.0
it.fileName = "/user/general/record_$index.log"
it.id = 10 + index
it.key = "yyy_$index"
it.stat = 100
it.type = 1
it.timestamp = SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(Date())
index++
})
}
.flowOn(Dispatchers.Default)
.collect()
delay(TimeUnit.HOURS.toMillis(2))
}
@ExperimentalCoroutinesApi @Test
fun showBadCaseEntrance2():Unit = runBlocking(Dispatchers.Main) {
val f = ensureMoGoHmiFragmentShow()
var index = 0
(1 until 50)
.map { it }
.asFlow()
.onEach {
if (index in 1..4) {
delay(TimeUnit.SECONDS.toMillis(15))
} else {
delay(Random(20).nextLong())
}
f.onAutopilotRecordResult(AutoPilotRecordResult().also {
it.diskFree = 100 + index
it.duration = 60.0
it.fileName = "/user/general/record_$index.log"
it.id = 10 + index
it.key = "yyy_$index"
it.stat = 100
it.type = 1
it.timestamp = SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(Date())
index++
})
if (index == 5) {
f.lifecycleScope.cancel()
}
}
.flowOn(Dispatchers.Default)
.collect()
delay(TimeUnit.HOURS.toMillis(2))
}
@ExperimentalCoroutinesApi @Test
fun showBadCaseEntrance3(): Unit = runBlocking(Dispatchers.Main) {
val f = ensureMoGoHmiFragmentShow()
var index = 0
(1 until 50)
.map { it }
.asFlow()
.onEach {
delay(TimeUnit.SECONDS.toMillis(20))
f.onAutopilotRecordResult(AutoPilotRecordResult().also {
it.diskFree = 100 + index
it.duration = 60.0
it.fileName = "/user/general/record_$index.log"
it.id = 10 + index
it.key = "yyy_$index"
it.stat = 100
it.type = 1
it.timestamp = SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(Date())
index++
})
}
.flowOn(Dispatchers.Default)
.collect()
delay(TimeUnit.HOURS.toMillis(2))
}
private suspend fun ensureMoGoHmiFragmentShow(): MoGoHmiFragment = suspendCancellableCoroutine {
launch.onActivity { itx ->
val executor = Executors.newSingleThreadScheduledExecutor()
executor.scheduleAtFixedRate({
var find =
itx.supportFragmentManager.fragments.find { it is MoGoHmiFragment } as? MoGoHmiFragment
while (find == null) {
find =
itx.supportFragmentManager.fragments.find { it is MoGoHmiFragment } as? MoGoHmiFragment
}
while (!find.isResumed) {
Thread.sleep(500)
}
it.resumeWith(Result.success(find))
try {
Thread.sleep(500)
executor.shutdownNow()
} catch (e: Throwable) {
e.printStackTrace()
}
}, 50, 500, TimeUnit.MILLISECONDS)
}
}
}