[AndroidTest]添加RxJava背压测试

This commit is contained in:
renwj
2022-02-14 11:38:24 +08:00
parent c17927fb72
commit 2c5264fd3c
2 changed files with 99 additions and 42 deletions

View File

@@ -0,0 +1,57 @@
package com.mogo.functions.test
import android.os.Debug
import android.util.Log
import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import com.mogo.eagle.core.function.main.MainLauncherActivity
import io.reactivex.Flowable
import io.reactivex.plugins.RxJavaPlugins
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeUnit.MILLISECONDS
@RunWith(AndroidJUnit4::class)
@LargeTest
class RxJavaBackPressureTest {
lateinit var launch: ActivityScenario<MainLauncherActivity>
@Before
fun before() {
launch = ActivityScenario.launch(MainLauncherActivity::class.java)
RxJavaPlugins.setErrorHandler {
Log.e("RxJava2", it.message, it)
}
}
@Test
fun testIntervalBackPressure() = runBlocking(Dispatchers.Default) {
val subscription = Flowable.interval(50, MILLISECONDS).doOnNext {
Log.d("RxJava2", "-- do action --")
}.subscribeOn(Schedulers.computation()).observeOn(Schedulers.io()).subscribe {
Thread.sleep(2000)
}
repeat(10) {
delay(TimeUnit.SECONDS.toMillis(5))
try {
Debug.dumpHprofData(Date().toFileName())
} catch (t: Throwable) {
t.printStackTrace()
}
}
delay(TimeUnit.HOURS.toMillis(1))
subscription.dispose()
}
private fun Date.toFileName(): String = SimpleDateFormat("yyyyMMdd_HH_mm_ss", Locale.getDefault()).format(this) + ".hprof"
}