|
| 1 | +package com.baeldung.kotlin |
| 2 | + |
| 3 | +import kotlinx.coroutines.experimental.* |
| 4 | +import org.junit.Test |
| 5 | +import java.util.concurrent.atomic.AtomicInteger |
| 6 | +import kotlin.coroutines.experimental.buildSequence |
| 7 | +import kotlin.system.measureTimeMillis |
| 8 | +import kotlin.test.assertEquals |
| 9 | +import kotlin.test.assertTrue |
| 10 | + |
| 11 | + |
| 12 | +class CoroutinesTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + fun givenBuildSequence_whenTakeNElements_thenShouldReturnItInALazyWay() { |
| 16 | + //given |
| 17 | + val fibonacciSeq = buildSequence { |
| 18 | + var a = 0 |
| 19 | + var b = 1 |
| 20 | + |
| 21 | + yield(1) |
| 22 | + |
| 23 | + while (true) { |
| 24 | + yield(a + b) |
| 25 | + |
| 26 | + val tmp = a + b |
| 27 | + a = b |
| 28 | + b = tmp |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + //when |
| 33 | + val res = fibonacciSeq.take(5).toList() |
| 34 | + |
| 35 | + //then |
| 36 | + assertEquals(res, listOf(1, 1, 2, 3, 5)) |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun givenLazySeq_whenTakeNElements_thenShouldReturnAllElements() { |
| 41 | + //given |
| 42 | + val lazySeq = buildSequence { |
| 43 | + print("START ") |
| 44 | + for (i in 1..5) { |
| 45 | + yield(i) |
| 46 | + print("STEP ") |
| 47 | + } |
| 48 | + print("END") |
| 49 | + } |
| 50 | + //when |
| 51 | + val res = lazySeq.take(10).toList() |
| 52 | + |
| 53 | + //then |
| 54 | + assertEquals(res, listOf(1, 2, 3, 4, 5)) |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun givenAsyncCoroutine_whenStartIt_thenShouldExecuteItInTheAsyncWay() { |
| 59 | + //given |
| 60 | + val res = mutableListOf<String>() |
| 61 | + |
| 62 | + //when |
| 63 | + runBlocking<Unit> { |
| 64 | + val promise = launch(CommonPool) { expensiveComputation(res) } |
| 65 | + res.add("Hello,") |
| 66 | + promise.join() |
| 67 | + } |
| 68 | + |
| 69 | + //then |
| 70 | + assertEquals(res, listOf("Hello,", "word!")) |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + suspend fun expensiveComputation(res: MutableList<String>) { |
| 75 | + delay(1000L) |
| 76 | + res.add("word!") |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun givenHugeAmountOfCoroutines_whenStartIt_thenShouldExecuteItWithoutOutOfMemory() { |
| 81 | + runBlocking<Unit> { |
| 82 | + //given |
| 83 | + val counter = AtomicInteger(0) |
| 84 | + val numberOfCoroutines = 100_000 |
| 85 | + |
| 86 | + //when |
| 87 | + val jobs = List(numberOfCoroutines) { |
| 88 | + launch(CommonPool) { |
| 89 | + delay(1L) |
| 90 | + counter.incrementAndGet() |
| 91 | + } |
| 92 | + } |
| 93 | + jobs.forEach { it.join() } |
| 94 | + |
| 95 | + //then |
| 96 | + assertEquals(counter.get(), numberOfCoroutines) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + fun givenCancellableJob_whenRequestForCancel_thenShouldQuit() { |
| 102 | + runBlocking<Unit> { |
| 103 | + //given |
| 104 | + val job = launch(CommonPool) { |
| 105 | + while (isActive) { |
| 106 | + println("is working") |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + delay(1300L) |
| 111 | + |
| 112 | + //when |
| 113 | + job.cancel() |
| 114 | + |
| 115 | + //then cancel successfully |
| 116 | + |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Test(expected = CancellationException::class) |
| 121 | + fun givenAsyncAction_whenDeclareTimeout_thenShouldFinishWhenTimedOut() { |
| 122 | + runBlocking<Unit> { |
| 123 | + withTimeout(1300L) { |
| 124 | + repeat(1000) { i -> |
| 125 | + println("Some expensive computation $i ...") |
| 126 | + delay(500L) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + fun givenHaveTwoExpensiveAction_whenExecuteThemAsync_thenTheyShouldRunConcurrently() { |
| 134 | + runBlocking<Unit> { |
| 135 | + val delay = 1000L |
| 136 | + val time = measureTimeMillis { |
| 137 | + //given |
| 138 | + val one = async(CommonPool) { someExpensiveComputation(delay) } |
| 139 | + val two = async(CommonPool) { someExpensiveComputation(delay) } |
| 140 | + |
| 141 | + //when |
| 142 | + runBlocking { |
| 143 | + one.await() |
| 144 | + two.await() |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + //then |
| 149 | + assertTrue(time < delay * 2) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + fun givenTwoExpensiveAction_whenExecuteThemLazy_thenTheyShouldNotConcurrently() { |
| 155 | + runBlocking<Unit> { |
| 156 | + val delay = 1000L |
| 157 | + val time = measureTimeMillis { |
| 158 | + //given |
| 159 | + val one = async(CommonPool, CoroutineStart.LAZY) { someExpensiveComputation(delay) } |
| 160 | + val two = async(CommonPool, CoroutineStart.LAZY) { someExpensiveComputation(delay) } |
| 161 | + |
| 162 | + //when |
| 163 | + runBlocking { |
| 164 | + one.await() |
| 165 | + two.await() |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + //then |
| 170 | + assertTrue(time > delay * 2) |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + suspend fun someExpensiveComputation(delayInMilliseconds: Long) { |
| 175 | + delay(delayInMilliseconds) |
| 176 | + } |
| 177 | + |
| 178 | + |
| 179 | +} |
0 commit comments