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