-
Notifications
You must be signed in to change notification settings - Fork 3
/
TaskSchedulerTest.kt
97 lines (90 loc) · 2.78 KB
/
TaskSchedulerTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.lancewu.taskscheduler
import android.util.Log
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.lancewu.taskscheduler.graph.DirectedAcyclicGraph
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import org.junit.Test
import org.junit.runner.RunWith
import java.lang.RuntimeException
/**
* Created by LanceWu on 2022/8/10.<br></br>
* 测试
*/
@RunWith(AndroidJUnit4::class)
class TaskSchedulerTest {
companion object {
const val TAG = "TaskSchedulerTest"
}
@Test
fun test_TaskScheduler() {
runBlocking {
val task1 = TaskScheduler.Task("1").apply {
doOnScheduled = {
delay(1000)
log("任务执行完成:$this")
}
}
val task2 = TaskScheduler.Task("2").apply {
doOnScheduled = {
delay(1000)
log("任务执行完成:$this")
}
}
val task3 = TaskScheduler.Task("3").apply {
doOnScheduled = {
delay(2000)
log("任务执行完成:$this")
}
}
val task4 = TaskScheduler.Task("4").apply {
doOnScheduled = {
delay(1000)
log("任务执行完成:$this")
}
}
val task5 = TaskScheduler.Task("5").apply {
doOnScheduled = {
delay(1000)
log("任务执行完成:$this")
}
}
val graph = DirectedAcyclicGraph.Builder<TaskScheduler.Task>()
.addEdge(task1, task2)
.addEdge(task1, task3)
.addEdge(task2, task4)
.addEdge(task3, task4)
.addNode(task5)
.build()
val taskScheduler = TaskScheduler(MainScope(), graph)
taskScheduler.schedule(task1)
.schedule(task2)
.schedule(task3)
.schedule(task4)
.schedule(task5)
delay(5000)
}
}
@Test
fun test_CyclicDependencies() {
// 有环,抛出异常
var exception: Exception? = null
try {
DirectedAcyclicGraph.Builder<String>()
.addEdge("1", "2")
.addEdge("1", "3")
.addEdge("2", "4")
.addEdge("3", "4")
.addEdge("4", "1")
.build()
} catch (e: Exception) {
e.printStackTrace()
exception = e
}
assert(exception is RuntimeException)
}
private fun log(message: String) {
Log.e(TAG, message)
}
}