File tree 1 file changed +35
-0
lines changed
kotlinx-coroutines-core/jvm/test
1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -64,6 +64,27 @@ class RunBlockingJvmTest : TestBase() {
64
64
finish(5 )
65
65
}
66
66
67
+ /* *
68
+ * Tests that [runBlockingNonInterruptible] is going to run its job to completion even if it gets interrupted
69
+ * or if thread switches occur.
70
+ */
71
+ @Test
72
+ fun testNonInterruptibleRunBlocking () {
73
+ startInSeparateThreadAndInterrupt { mayInterrupt ->
74
+ val v = runBlockingNonInterruptible {
75
+ mayInterrupt()
76
+ repeat(10 ) {
77
+ expect(it + 1 )
78
+ delay(1 )
79
+ }
80
+ 42
81
+ }
82
+ assertEquals(42 , v)
83
+ expect(11 )
84
+ }
85
+ finish(12 )
86
+ }
87
+
67
88
private fun startInSeparateThreadAndInterrupt (action : (mayInterrupt: () -> Unit ) -> Unit ) {
68
89
val latch = CountDownLatch (1 )
69
90
val thread = thread {
@@ -73,4 +94,18 @@ class RunBlockingJvmTest : TestBase() {
73
94
thread.interrupt()
74
95
thread.join()
75
96
}
97
+
98
+ private fun <T > runBlockingNonInterruptible (action : suspend () -> T ): T {
99
+ val deferred = CompletableDeferred <T >()
100
+ try {
101
+ runBlocking {
102
+ withContext(NonCancellable ) {
103
+ deferred.completeWith(runCatching { action() })
104
+ }
105
+ }
106
+ } catch (_: InterruptedException ) {
107
+ Thread .currentThread().interrupt() // restore the interrupted flag
108
+ }
109
+ return deferred.getCompleted()
110
+ }
76
111
}
You can’t perform that action at this time.
0 commit comments