Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | */ |
| 4 | |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines.experimental.io |
| 6 | |
| 7 | import kotlinx.coroutines.experimental.* |
| 8 | import kotlinx.coroutines.experimental.channels.ClosedReceiveChannelException |
| 9 | import org.junit.After |
| 10 | import org.junit.Test |
| 11 | import java.io.IOException |
| 12 | import kotlin.coroutines.experimental.coroutineContext |
| 13 | |
| 14 | class ByteChannelCloseTest : TestBase() { |
| 15 | |
| 16 | private val from = ByteChannel(true) |
| 17 | private val to = ByteChannel(true) |
| 18 | |
| 19 | @After |
| 20 | fun tearDown() { |
| 21 | from.close(CancellationException()) |
| 22 | to.close(CancellationException()) |
| 23 | } |
| 24 | |
| 25 | @Test |
| 26 | fun testCloseWithCause() = runBlocking { |
| 27 | expect(1) |
| 28 | |
| 29 | launch(coroutineContext) { |
| 30 | expect(2) |
| 31 | |
| 32 | try { |
| 33 | from.copyAndClose(to) // should suspend and then throw IOException |
| 34 | expectUnreached() |
| 35 | } catch (expected: IOException) { |
| 36 | expect(4) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | yield() |
| 41 | expect(3) |
| 42 | |
| 43 | from.close(IOException()) |
| 44 | yield() |
| 45 | |
| 46 | expect(5) |
| 47 | |
| 48 | try { |
| 49 | to.readInt() |
| 50 | expectUnreached() |
| 51 | } catch (expected: IOException) { |
| 52 | finish(6) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | fun testCancelWithCause() = runBlocking { |
| 58 | expect(1) |
| 59 | |
| 60 | launch(coroutineContext) { |
| 61 | expect(2) |
| 62 | |
| 63 | try { |
| 64 | from.copyAndClose(to) // should suspend and then throw IOException |
| 65 | expectUnreached() |
| 66 | } catch (expected: IOException) { |
| 67 | expect(4) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | yield() |
| 72 | expect(3) |
| 73 | |
| 74 | from.cancel(IOException()) |
| 75 | yield() |
| 76 | |
| 77 | expect(5) |
| 78 | |
| 79 | try { |
| 80 | to.readInt() |
| 81 | expectUnreached() |
| 82 | } catch (expected: IOException) { |
| 83 | finish(6) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | fun testCloseWithoutCause() = runBlocking { |
| 89 | expect(1) |
| 90 | |
| 91 | launch(coroutineContext) { |
| 92 | expect(2) |
| 93 | from.copyAndClose(to) |
| 94 | expect(4) |
| 95 | } |
| 96 | |
| 97 | yield() |
| 98 | expect(3) |
| 99 | |
| 100 | from.close() |
| 101 | yield() |
| 102 | |
| 103 | expect(5) |
| 104 | require(to.isClosedForWrite) |
| 105 | |
| 106 | try { |
| 107 | to.readInt() |
| 108 | expectUnreached() |
| 109 | } catch (expected: ClosedReceiveChannelException) { |
| 110 | finish(6) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | @Test |
| 115 | fun testCancelWithoutCause() = runBlocking { |
| 116 | expect(1) |
| 117 | |
| 118 | launch(coroutineContext) { |
| 119 | expect(2) |
| 120 | try { |
| 121 | from.copyAndClose(to) |
| 122 | expectUnreached() |
| 123 | } catch (e: CancellationException) { |
| 124 | expect(4) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | yield() |
| 129 | expect(3) |
| 130 | |
| 131 | from.cancel() |
| 132 | yield() |
| 133 | |
| 134 | expect(5) |
| 135 | require(to.isClosedForWrite) |
| 136 | |
| 137 | try { |
| 138 | to.readInt() |
| 139 | expectUnreached() |
| 140 | } catch (expected: CancellationException) { |
| 141 | finish(6) |
| 142 | } |
| 143 | } |
| 144 | } |