Skip to content

Commit 9c3ecb6

Browse files
committed
Fix the failed test case
1 parent 86cc5cf commit 9c3ecb6

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

baseLib/src/androidTest/java/me/ycdev/android/lib/common/async/AsyncTaskQueueTest.kt

+19-14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import androidx.test.filters.SmallTest
99
import com.google.common.truth.Truth.assertThat
1010
import java.util.concurrent.CountDownLatch
1111
import java.util.concurrent.TimeUnit
12+
import me.ycdev.android.lib.common.utils.MainHandler
1213
import me.ycdev.android.lib.common.utils.ThreadUtils
1314
import org.junit.Test
1415
import org.junit.runner.RunWith
@@ -112,7 +113,7 @@ class AsyncTaskQueueTest {
112113
val latch = CountDownLatch(count)
113114
val countTask = CountTask(Runnable { latch.countDown() })
114115
for (i in 0 until count) {
115-
taskQueue.addTask(countTask, 200)
116+
taskQueue.addTask(200, countTask)
116117
}
117118

118119
latch.await()
@@ -126,7 +127,7 @@ class AsyncTaskQueueTest {
126127
taskQueue.setWorkerThreadAutoQuitDelay(AsyncTaskQueue.WORKER_THREAD_AUTO_QUIT_DELAY_MIN)
127128

128129
val countTask = CountTask(null)
129-
taskQueue.addTask(countTask, 200)
130+
taskQueue.addTask(200, countTask)
130131
taskQueue.removeTask(countTask)
131132

132133
SystemClock.sleep(500)
@@ -148,15 +149,18 @@ class AsyncTaskQueueTest {
148149
val countTask = CountTask(null)
149150
val count = 100
150151
for (i in 0 until count) {
151-
taskQueue.addTask(countTask, 100)
152+
taskQueue.addTask(100, countTask)
152153
}
153154
taskQueue.removeTask(countTask)
154155

155-
// now, let the task go
156-
guardLatch.countDown()
156+
// make sure all messages in main looper had been processed
157+
MainHandler.post {
158+
// now, let the guard task go
159+
guardLatch.countDown()
160+
}
157161

158162
// add one more task
159-
taskQueue.addTask(countTask, 100)
163+
taskQueue.addTask(100, countTask)
160164

161165
// make sure the previous tasks will be executed
162166
waitForTasksDone(taskQueue, 100)
@@ -176,7 +180,7 @@ class AsyncTaskQueueTest {
176180
val countTask = CountTask(Runnable { latch.countDown() })
177181
for (i in 0 until count) {
178182
taskQueue.removeTask(countTask)
179-
taskQueue.addTask(countTask, 200)
183+
taskQueue.addTask(200, countTask)
180184
}
181185

182186
// make sure the previous tasks will be executed
@@ -191,26 +195,27 @@ class AsyncTaskQueueTest {
191195
val guardTask = Runnable {
192196
guardLatch.countDown()
193197
}
194-
taskQueue.addTask(guardTask, delay)
198+
taskQueue.addTask(delay, guardTask)
195199
guardLatch.await()
196200
}
197201

198202
@Test
199203
@LargeTest
200204
@Throws(InterruptedException::class)
201205
fun setWorkerThreadAutoQuitDelay_normal() {
202-
val autoQuitDelay = (20 * 1000).toLong()
206+
val autoQuitDelay = (10 * 1000).toLong()
203207
val taskQueue = AsyncTaskQueue(TAG)
204208
taskQueue.setWorkerThreadAutoQuitDelay(autoQuitDelay)
205209

206210
val latch = CountDownLatch(1)
207-
taskQueue.addTask(Runnable { latch.countDown() }, 100)
211+
taskQueue.addTask(100, Runnable { latch.countDown() })
208212
latch.await()
209213

210214
// check if task thread already quited
211215
assertThat(taskQueue.taskHandler).isNotNull()
212-
SystemClock.sleep(autoQuitDelay + 100)
213-
assertThat(taskQueue.taskHandler).isNull()
216+
while (taskQueue.taskHandler != null) {
217+
SystemClock.sleep(100)
218+
}
214219
}
215220

216221
@Test
@@ -221,7 +226,7 @@ class AsyncTaskQueueTest {
221226
taskQueue.setWorkerThreadAutoQuitDelay(0)
222227

223228
val latch = CountDownLatch(1)
224-
taskQueue.addTask(Runnable { latch.countDown() }, 100)
229+
taskQueue.addTask(100, Runnable { latch.countDown() })
225230
latch.await()
226231

227232
// check if task thread already quited
@@ -312,7 +317,7 @@ class AsyncTaskQueueTest {
312317
assertThat(latch.count).isEqualTo(taskCount - i)
313318
latch.countDown()
314319
}
315-
taskQueue.addTask(task, taskDelay)
320+
taskQueue.addTask(taskDelay, task)
316321
}
317322
}
318323
}

baseLib/src/main/java/me/ycdev/android/lib/common/async/AsyncTaskQueue.kt

+5-2
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,16 @@ class AsyncTaskQueue(private val name: String) {
8787
}
8888
}
8989

90-
@JvmOverloads
91-
fun addTask(task: Runnable, delay: Long = 0L) {
90+
fun addTask(delay: Long, task: Runnable) {
9291
if (DEV_LOG) Timber.tag(TAG).d("addTask: %s, delay: %d", task, delay)
9392
val params = TaskParams(task, delay)
9493
mainHandler.obtainMessage(MSG_MAIN_NEW_TASK, params).sendToTarget()
9594
}
9695

96+
fun addTask(task: Runnable) {
97+
addTask(0L, task)
98+
}
99+
97100
fun removeTask(task: Runnable) {
98101
if (DEV_LOG) Timber.tag(TAG).d("removeTask: %s", task)
99102
mainHandler.obtainMessage(MSG_MAIN_REMOVE_TASK, task).sendToTarget()

0 commit comments

Comments
 (0)