Skip to content

Commit d170614

Browse files
committed
Update the docs for await* in -reactive and -jdk9
1 parent c05b4e8 commit d170614

File tree

4 files changed

+171
-75
lines changed

4 files changed

+171
-75
lines changed

reactive/kotlinx-coroutines-jdk9/src/Await.kt

+38-34
Original file line numberDiff line numberDiff line change
@@ -4,78 +4,82 @@
44

55
package kotlinx.coroutines.jdk9
66

7+
import kotlinx.coroutines.Job
78
import java.util.concurrent.*
89
import org.reactivestreams.FlowAdapters
910
import kotlinx.coroutines.reactive.*
1011

1112
/**
12-
* Awaits for the first value from the given publisher without blocking a thread and
13-
* returns the resulting value or throws the corresponding exception if this publisher had produced error.
13+
* Awaits the first value from the given publisher without blocking the thread and returns the resulting value, or, if
14+
* the publisher has produced an error, throws the corresponding exception.
1415
*
1516
* This suspending function is cancellable.
16-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
17-
* immediately resumes with [CancellationException].
17+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
18+
* function immediately cancels its [Flow.Subscription] and resumes with [CancellationException].
1819
*
19-
* @throws NoSuchElementException if publisher does not emit any value
20+
* @throws NoSuchElementException if the publisher does not emit any value
2021
*/
21-
public suspend fun <T> Flow.Publisher<T>.awaitFirst(): T = FlowAdapters.toPublisher(this).awaitFirst()
22+
public suspend fun <T> Flow.Publisher<T>.awaitFirst(): T =
23+
FlowAdapters.toPublisher(this).awaitFirst()
2224

2325
/**
24-
* Awaits for the first value from the given observable or the [default] value if none is emitted without blocking a
25-
* thread and returns the resulting value or throws the corresponding exception if this observable had produced error.
26+
* Awaits the first value from the given observable, or returns the [default] value if none is emitted, without blocking
27+
* the thread, and returns the resulting value, or, if this observable has produced an error, throws the corresponding
28+
* exception.
2629
*
2730
* This suspending function is cancellable.
28-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
29-
* immediately resumes with [CancellationException].
31+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
32+
* function immediately cancels its [Flow.Subscription] and resumes with [CancellationException].
3033
*/
3134
public suspend fun <T> Flow.Publisher<T>.awaitFirstOrDefault(default: T): T =
32-
FlowAdapters.toPublisher(this).awaitFirstOrDefault(default)
35+
FlowAdapters.toPublisher(this).awaitFirstOrDefault(default)
3336

3437
/**
35-
* Awaits for the first value from the given observable or `null` value if none is emitted without blocking a
36-
* thread and returns the resulting value or throws the corresponding exception if this observable had produced error.
38+
* Awaits the first value from the given observable, or returns `null` if none is emitted, without blocking the thread,
39+
* and returns the resulting value, or, if this observable has produced an error, throws the corresponding exception.
3740
*
3841
* This suspending function is cancellable.
39-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
40-
* immediately resumes with [CancellationException].
42+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
43+
* function immediately cancels its [Flow.Subscription] and resumes with [CancellationException].
4144
*/
4245
public suspend fun <T> Flow.Publisher<T>.awaitFirstOrNull(): T? =
43-
FlowAdapters.toPublisher(this).awaitFirstOrNull()
46+
FlowAdapters.toPublisher(this).awaitFirstOrNull()
4447

4548
/**
46-
* Awaits for the first value from the given observable or call [defaultValue] to get a value if none is emitted without blocking a
47-
* thread and returns the resulting value or throws the corresponding exception if this observable had produced error.
49+
* Awaits the first value from the given observable, or calls [defaultValue] to get a value if none is emitted, without
50+
* blocking the thread, and returns the resulting value, or, if this observable has produced an error, throws the
51+
* corresponding exception.
4852
*
4953
* This suspending function is cancellable.
50-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
51-
* immediately resumes with [CancellationException].
54+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
55+
* function immediately cancels its [Flow.Subscription] and resumes with [CancellationException].
5256
*/
5357
public suspend fun <T> Flow.Publisher<T>.awaitFirstOrElse(defaultValue: () -> T): T =
54-
FlowAdapters.toPublisher(this).awaitFirstOrElse(defaultValue)
58+
FlowAdapters.toPublisher(this).awaitFirstOrElse(defaultValue)
5559

5660
/**
57-
* Awaits for the last value from the given publisher without blocking a thread and
58-
* returns the resulting value or throws the corresponding exception if this publisher had produced error.
61+
* Awaits the last value from the given publisher without blocking the thread and
62+
* returns the resulting value, or, if this publisher has produced an error, throws the corresponding exception.
5963
*
6064
* This suspending function is cancellable.
61-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
62-
* immediately resumes with [CancellationException].
65+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
66+
* function immediately cancels its [Flow.Subscription] and resumes with [CancellationException].
6367
*
64-
* @throws NoSuchElementException if publisher does not emit any value
68+
* @throws NoSuchElementException if the publisher does not emit any value
6569
*/
6670
public suspend fun <T> Flow.Publisher<T>.awaitLast(): T =
67-
FlowAdapters.toPublisher(this).awaitLast()
71+
FlowAdapters.toPublisher(this).awaitLast()
6872

6973
/**
70-
* Awaits for the single value from the given publisher without blocking a thread and
71-
* returns the resulting value or throws the corresponding exception if this publisher had produced error.
74+
* Awaits the single value from the given publisher without blocking the thread and returns the resulting value, or,
75+
* if this publisher has produced an error, throws the corresponding exception.
7276
*
7377
* This suspending function is cancellable.
74-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
75-
* immediately resumes with [CancellationException].
78+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
79+
* function immediately cancels its [Flow.Subscription] and resumes with [CancellationException].
7680
*
77-
* @throws NoSuchElementException if publisher does not emit any value
78-
* @throws IllegalArgumentException if publisher emits more than one value
81+
* @throws NoSuchElementException if the publisher does not emit any value
82+
* @throws IllegalArgumentException if the publisher emits more than one value
7983
*/
8084
public suspend fun <T> Flow.Publisher<T>.awaitSingle(): T =
81-
FlowAdapters.toPublisher(this).awaitSingle()
85+
FlowAdapters.toPublisher(this).awaitSingle()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.coroutines.jdk9
6+
7+
import kotlinx.coroutines.*
8+
import kotlinx.coroutines.CancellationException
9+
import org.junit.*
10+
import java.util.concurrent.*
11+
12+
class AwaitTest: TestBase() {
13+
14+
/** Tests that calls to [awaitFirst] (and, thus, to the rest of these functions) throw [CancellationException] and
15+
* unsubscribe from the publisher when their [Job] is cancelled. */
16+
@Test
17+
fun testAwaitCancellation() = runTest {
18+
expect(1)
19+
val publisher = Flow.Publisher<Int> { s ->
20+
s.onSubscribe(object : Flow.Subscription {
21+
override fun request(n: Long) {
22+
expect(3)
23+
}
24+
25+
override fun cancel() {
26+
expect(5)
27+
}
28+
})
29+
}
30+
val job = launch(start = CoroutineStart.UNDISPATCHED) {
31+
try {
32+
expect(2)
33+
publisher.awaitFirst()
34+
} catch (e: CancellationException) {
35+
expect(6)
36+
throw e
37+
}
38+
}
39+
expect(4)
40+
job.cancelAndJoin()
41+
finish(7)
42+
}
43+
44+
}

reactive/kotlinx-coroutines-reactive/src/Await.kt

+46-41
Original file line numberDiff line numberDiff line change
@@ -10,85 +10,89 @@ import org.reactivestreams.Subscriber
1010
import org.reactivestreams.Subscription
1111
import java.lang.IllegalStateException
1212
import java.util.*
13+
import kotlin.NoSuchElementException
1314
import kotlin.coroutines.*
1415

1516
/**
16-
* Awaits for the first value from the given publisher without blocking a thread and
17-
* returns the resulting value or throws the corresponding exception if this publisher had produced error.
17+
* Awaits the first value from the given publisher without blocking the thread and returns the resulting value, or, if
18+
* the publisher has produced an error, throws the corresponding exception.
1819
*
1920
* This suspending function is cancellable.
20-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
21-
* immediately resumes with [CancellationException].
21+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
22+
* function immediately cancels its [Subscription] and resumes with [CancellationException].
2223
*
23-
* @throws NoSuchElementException if publisher does not emit any value
24+
* @throws NoSuchElementException if the publisher does not emit any value
2425
*/
2526
public suspend fun <T> Publisher<T>.awaitFirst(): T = awaitOne(Mode.FIRST)
2627

2728
/**
28-
* Awaits for the first value from the given observable or the [default] value if none is emitted without blocking a
29-
* thread and returns the resulting value or throws the corresponding exception if this observable had produced error.
29+
* Awaits the first value from the given observable, or returns the [default] value if none is emitted, without blocking
30+
* the thread, and returns the resulting value, or, if this observable has produced an error, throws the corresponding
31+
* exception.
3032
*
3133
* This suspending function is cancellable.
32-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
33-
* immediately resumes with [CancellationException].
34+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
35+
* function immediately cancels its [Subscription] and resumes with [CancellationException].
3436
*/
3537
public suspend fun <T> Publisher<T>.awaitFirstOrDefault(default: T): T = awaitOne(Mode.FIRST_OR_DEFAULT, default)
3638

3739
/**
38-
* Awaits for the first value from the given observable or `null` value if none is emitted without blocking a
39-
* thread and returns the resulting value or throws the corresponding exception if this observable had produced error.
40+
* Awaits the first value from the given observable, or returns `null` if none is emitted, without blocking the thread,
41+
* and returns the resulting value, or, if this observable has produced an error, throws the corresponding exception.
4042
*
4143
* This suspending function is cancellable.
42-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
43-
* immediately resumes with [CancellationException].
44+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
45+
* function immediately cancels its [Subscription] and resumes with [CancellationException].
4446
*/
4547
public suspend fun <T> Publisher<T>.awaitFirstOrNull(): T? = awaitOne(Mode.FIRST_OR_DEFAULT)
4648

4749
/**
48-
* Awaits for the first value from the given observable or call [defaultValue] to get a value if none is emitted without blocking a
49-
* thread and returns the resulting value or throws the corresponding exception if this observable had produced error.
50+
* Awaits the first value from the given observable, or calls [defaultValue] to get a value if none is emitted, without
51+
* blocking the thread, and returns the resulting value, or, if this observable has produced an error, throws the
52+
* corresponding exception.
5053
*
5154
* This suspending function is cancellable.
52-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
53-
* immediately resumes with [CancellationException].
55+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
56+
* function immediately cancels its [Subscription] and resumes with [CancellationException].
5457
*/
5558
public suspend fun <T> Publisher<T>.awaitFirstOrElse(defaultValue: () -> T): T = awaitOne(Mode.FIRST_OR_DEFAULT) ?: defaultValue()
5659

5760
/**
58-
* Awaits for the last value from the given publisher without blocking a thread and
59-
* returns the resulting value or throws the corresponding exception if this publisher had produced error.
61+
* Awaits the last value from the given publisher without blocking the thread and
62+
* returns the resulting value, or, if this publisher has produced an error, throws the corresponding exception.
6063
*
6164
* This suspending function is cancellable.
62-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
63-
* immediately resumes with [CancellationException].
65+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
66+
* function immediately cancels its [Subscription] and resumes with [CancellationException].
6467
*
65-
* @throws NoSuchElementException if publisher does not emit any value
68+
* @throws NoSuchElementException if the publisher does not emit any value
6669
*/
6770
public suspend fun <T> Publisher<T>.awaitLast(): T = awaitOne(Mode.LAST)
6871

6972
/**
70-
* Awaits for the single value from the given publisher without blocking a thread and
71-
* returns the resulting value or throws the corresponding exception if this publisher had produced error.
73+
* Awaits the single value from the given publisher without blocking the thread and returns the resulting value, or,
74+
* if this publisher has produced an error, throws the corresponding exception.
7275
*
7376
* This suspending function is cancellable.
74-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
75-
* immediately resumes with [CancellationException].
77+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
78+
* function immediately cancels its [Subscription] and resumes with [CancellationException].
7679
*
77-
* @throws NoSuchElementException if publisher does not emit any value
78-
* @throws IllegalArgumentException if publisher emits more than one value
80+
* @throws NoSuchElementException if the publisher does not emit any value
81+
* @throws IllegalArgumentException if the publisher emits more than one value
7982
*/
8083
public suspend fun <T> Publisher<T>.awaitSingle(): T = awaitOne(Mode.SINGLE)
8184

8285
/**
83-
* Awaits for the single value from the given publisher or the [default] value if none is emitted without blocking a thread and
84-
* returns the resulting value or throws the corresponding exception if this publisher had produced error.
86+
* Awaits the single value from the given observable, or returns the [default] value if none is emitted, without
87+
* blocking the thread, and returns the resulting value, or, if this observable has produced an error, throws the
88+
* corresponding exception.
8589
*
8690
* This suspending function is cancellable.
87-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
88-
* immediately resumes with [CancellationException].
91+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
92+
* function immediately cancels its [Subscription] and resumes with [CancellationException].
8993
*
90-
* @throws NoSuchElementException if publisher does not emit any value
91-
* @throws IllegalArgumentException if publisher emits more than one value
94+
* @throws NoSuchElementException if the publisher does not emit any value
95+
* @throws IllegalArgumentException if the publisher emits more than one value
9296
*/
9397
public suspend fun <T> Publisher<T>.awaitSingleOrDefault(default: T): T = awaitOne(Mode.SINGLE_OR_DEFAULT, default)
9498

@@ -108,17 +112,18 @@ public suspend fun <T> Publisher<T>.awaitSingleOrNull(): T? = try {
108112
}
109113

110114
/**
111-
* Awaits for the single value from the given publisher or call [defaultValue] to get a value if none is emitted without blocking a thread and
112-
* returns the resulting value or throws the corresponding exception if this publisher had produced error.
115+
* Awaits the single value from the given observable, or calls [defaultValue] to get a value if none is emitted, without
116+
* blocking the thread, and returns the resulting value, or, if this observable has produced an error, throws the
117+
* corresponding exception.
113118
*
114119
* This suspending function is cancellable.
115-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
116-
* immediately resumes with [CancellationException].
120+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
121+
* function immediately cancels its [Subscription] and resumes with [CancellationException].
117122
*
118-
* @throws NoSuchElementException if publisher does not emit any value
119-
* @throws IllegalArgumentException if publisher emits more than one value
123+
* @throws IllegalArgumentException if the publisher emits more than one value
120124
*/
121-
public suspend fun <T> Publisher<T>.awaitSingleOrElse(defaultValue: () -> T): T = awaitOne(Mode.SINGLE_OR_DEFAULT) ?: defaultValue()
125+
public suspend fun <T> Publisher<T>.awaitSingleOrElse(defaultValue: () -> T): T =
126+
awaitOne(Mode.SINGLE_OR_DEFAULT) ?: defaultValue()
122127

123128
// ------------------------ private ------------------------
124129

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.coroutines.reactive
6+
7+
import kotlinx.coroutines.*
8+
import org.junit.*
9+
import org.reactivestreams.*
10+
11+
class AwaitTest: TestBase() {
12+
13+
/** Tests that calls to [awaitFirst] (and, thus, to the rest of these functions) throw [CancellationException] and
14+
* unsubscribe from the publisher when their [Job] is cancelled. */
15+
@Test
16+
fun testAwaitCancellation() = runTest {
17+
expect(1)
18+
val publisher = Publisher<Int> { s ->
19+
s.onSubscribe(object: Subscription {
20+
override fun request(n: Long) {
21+
expect(3)
22+
}
23+
24+
override fun cancel() {
25+
expect(5)
26+
}
27+
})
28+
}
29+
val job = launch(start = CoroutineStart.UNDISPATCHED) {
30+
try {
31+
expect(2)
32+
publisher.awaitFirst()
33+
} catch (e: CancellationException) {
34+
expect(6)
35+
throw e
36+
}
37+
}
38+
expect(4)
39+
job.cancelAndJoin()
40+
finish(7)
41+
}
42+
43+
}

0 commit comments

Comments
 (0)