Roman Elizarov | 9d61b3e | 2017-04-19 18:32:11 +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 org.hamcrest.core.IsEqual |
| 20 | import org.junit.After |
| 21 | import org.junit.Assert |
| 22 | import org.junit.Test |
| 23 | import java.util.concurrent.ExecutorService |
| 24 | import java.util.concurrent.Executors |
| 25 | import java.util.concurrent.ThreadFactory |
Roman Elizarov | ca9d5be | 2017-04-20 19:23:18 +0300 | [diff] [blame] | 26 | import java.util.concurrent.atomic.AtomicInteger |
Roman Elizarov | 9d61b3e | 2017-04-19 18:32:11 +0300 | [diff] [blame] | 27 | import kotlin.coroutines.experimental.CoroutineContext |
| 28 | |
| 29 | class WithTimeoutThreadDispatchTest : TestBase() { |
| 30 | var executor: ExecutorService? = null |
| 31 | |
| 32 | @After |
| 33 | fun tearDown() { |
| 34 | executor?.shutdown() |
| 35 | } |
| 36 | |
| 37 | @Test |
| 38 | fun testCancellationDispatchScheduled() { |
| 39 | checkCancellationDispatch { |
| 40 | executor = Executors.newScheduledThreadPool(1, it) |
| 41 | executor!!.asCoroutineDispatcher() |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | @Test |
| 46 | fun testCancellationDispatchNonScheduled() { |
| 47 | checkCancellationDispatch { |
| 48 | executor = Executors.newSingleThreadExecutor(it) |
| 49 | executor!!.asCoroutineDispatcher() |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | fun testCancellationDispatchCustomNoDelay() { |
Roman Elizarov | ca9d5be | 2017-04-20 19:23:18 +0300 | [diff] [blame] | 55 | // it also checks that there is at most once scheduled request in flight (no spurious concurrency) |
| 56 | var error: String? = null |
Roman Elizarov | 9d61b3e | 2017-04-19 18:32:11 +0300 | [diff] [blame] | 57 | checkCancellationDispatch { |
| 58 | executor = Executors.newSingleThreadExecutor(it) |
Roman Elizarov | ca9d5be | 2017-04-20 19:23:18 +0300 | [diff] [blame] | 59 | val scheduled = AtomicInteger(0) |
Roman Elizarov | 9d61b3e | 2017-04-19 18:32:11 +0300 | [diff] [blame] | 60 | object : CoroutineDispatcher() { |
| 61 | override fun dispatch(context: CoroutineContext, block: Runnable) { |
Roman Elizarov | ca9d5be | 2017-04-20 19:23:18 +0300 | [diff] [blame] | 62 | if (scheduled.incrementAndGet() > 1) error = "Two requests are scheduled concurrently" |
| 63 | executor!!.execute { |
| 64 | scheduled.decrementAndGet() |
| 65 | block.run() |
| 66 | } |
Roman Elizarov | 9d61b3e | 2017-04-19 18:32:11 +0300 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } |
Roman Elizarov | ca9d5be | 2017-04-20 19:23:18 +0300 | [diff] [blame] | 70 | error?.let { error(it) } |
Roman Elizarov | 9d61b3e | 2017-04-19 18:32:11 +0300 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | private fun checkCancellationDispatch(factory: (ThreadFactory) -> CoroutineDispatcher) = runBlocking { |
| 74 | expect(1) |
| 75 | var thread: Thread? = null |
| 76 | val dispatcher = factory(ThreadFactory { Thread(it).also { thread = it } }) |
| 77 | run(dispatcher) { |
| 78 | expect(2) |
| 79 | Assert.assertThat(Thread.currentThread(), IsEqual(thread)) |
| 80 | try { |
| 81 | withTimeout(100) { |
| 82 | try { |
| 83 | expect(3) |
| 84 | delay(1000) |
| 85 | expectUnreached() |
| 86 | } catch (e: CancellationException) { |
| 87 | expect(4) |
| 88 | Assert.assertThat(Thread.currentThread(), IsEqual(thread)) |
Roman Elizarov | ca9d5be | 2017-04-20 19:23:18 +0300 | [diff] [blame] | 89 | throw e // rethrow |
Roman Elizarov | 9d61b3e | 2017-04-19 18:32:11 +0300 | [diff] [blame] | 90 | } |
Roman Elizarov | 9d61b3e | 2017-04-19 18:32:11 +0300 | [diff] [blame] | 91 | } |
| 92 | } catch (e: CancellationException) { |
Roman Elizarov | ca9d5be | 2017-04-20 19:23:18 +0300 | [diff] [blame] | 93 | expect(5) |
Roman Elizarov | 9d61b3e | 2017-04-19 18:32:11 +0300 | [diff] [blame] | 94 | Assert.assertThat(Thread.currentThread(), IsEqual(thread)) |
| 95 | } |
Roman Elizarov | ca9d5be | 2017-04-20 19:23:18 +0300 | [diff] [blame] | 96 | expect(6) |
Roman Elizarov | 9d61b3e | 2017-04-19 18:32:11 +0300 | [diff] [blame] | 97 | } |
Roman Elizarov | ca9d5be | 2017-04-20 19:23:18 +0300 | [diff] [blame] | 98 | finish(7) |
Roman Elizarov | 9d61b3e | 2017-04-19 18:32:11 +0300 | [diff] [blame] | 99 | } |
| 100 | } |