Roman Elizarov | 6d54802 | 2018-01-12 20:21:19 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 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 kotlinx.coroutines.experimental |
| 18 | |
| 19 | import kotlin.test.* |
| 20 | |
| 21 | class MessageQueueTest { |
| 22 | private var scheduled = false |
| 23 | private val processed = mutableListOf<Int>() |
| 24 | |
| 25 | private val queue = object : MessageQueue() { |
| 26 | override fun schedule() { |
| 27 | assertFalse(scheduled) |
| 28 | scheduled = true |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | inner class Box(val i: Int): Runnable { |
| 33 | override fun run() { |
| 34 | processed += i |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | inner class ReBox(val i: Int): Runnable { |
| 39 | override fun run() { |
| 40 | processed += i |
| 41 | queue.enqueue(Box(10 + i)) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | @Test |
| 46 | fun testBasic() { |
| 47 | assertTrue(queue.isEmpty) |
| 48 | queue.enqueue(Box(1)) |
| 49 | assertFalse(queue.isEmpty) |
| 50 | assertTrue(scheduled) |
| 51 | queue.enqueue(Box(2)) |
| 52 | assertFalse(queue.isEmpty) |
| 53 | scheduled = false |
| 54 | queue.process() |
| 55 | assertEquals(listOf(1, 2), processed) |
| 56 | assertFalse(scheduled) |
| 57 | assertTrue(queue.isEmpty) |
| 58 | } |
| 59 | |
| 60 | @Test fun testRescheduleFromProcess() { |
| 61 | assertTrue(queue.isEmpty) |
| 62 | queue.enqueue(ReBox(1)) |
| 63 | assertFalse(queue.isEmpty) |
| 64 | assertTrue(scheduled) |
| 65 | queue.enqueue(ReBox(2)) |
| 66 | assertFalse(queue.isEmpty) |
| 67 | scheduled = false |
| 68 | queue.process() |
| 69 | assertEquals(listOf(1, 2, 11, 12), processed) |
| 70 | assertFalse(scheduled) |
| 71 | assertTrue(queue.isEmpty) |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | fun testResizeAndWrap() { |
| 76 | repeat(10) { phase -> |
| 77 | val n = 10 * (phase + 1) |
| 78 | assertTrue(queue.isEmpty) |
| 79 | repeat(n) { |
| 80 | queue.enqueue(Box(it)) |
| 81 | assertFalse(queue.isEmpty) |
| 82 | assertTrue(scheduled) |
| 83 | } |
| 84 | var countYields = 0 |
| 85 | while (scheduled) { |
| 86 | scheduled = false |
| 87 | queue.process() |
| 88 | countYields++ |
| 89 | } |
| 90 | assertEquals(List(n) { it }, processed) |
| 91 | assertEquals((n + queue.yieldEvery - 1) / queue.yieldEvery, countYields) |
| 92 | processed.clear() |
| 93 | } |
| 94 | } |
| 95 | } |