Skip to content

Commit 29586ff

Browse files
committed
Expand async tests, minor async function updates
1 parent 0428bec commit 29586ff

File tree

3 files changed

+465
-69
lines changed

3 files changed

+465
-69
lines changed

driver-core/src/main/com/mongodb/internal/async/AsyncSupplier.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public interface AsyncSupplier<T> extends AsyncFunction<Void, T> {
3838
void unsafeFinish(SingleResultCallback<T> callback);
3939

4040
/**
41+
* This is the async variant of a supplier's get method.
4142
* This method must only be used when this AsyncSupplier corresponds
4243
* to a {@link java.util.function.Supplier} (and is therefore being
4344
* used within an async chain method lambda).
@@ -108,13 +109,13 @@ default AsyncRunnable thenConsume(final AsyncConsumer<T> consumer) {
108109

109110
/**
110111
* @param errorCheck A check, comparable to a catch-if/otherwise-rethrow
111-
* @param supplier The branch to execute if the error matches
112+
* @param errorFunction The branch to execute if the error matches
112113
* @return The composition of this, and the conditional branch
113114
*/
114115
default AsyncSupplier<T> onErrorIf(
115116
final Predicate<Throwable> errorCheck,
116-
final AsyncSupplier<T> supplier) {
117-
return (callback) -> this.unsafeFinish((r, e) -> {
117+
final AsyncFunction<Throwable, T> errorFunction) {
118+
return (callback) -> this.finish((r, e) -> {
118119
if (e == null) {
119120
callback.onResult(r, null);
120121
return;
@@ -128,7 +129,7 @@ default AsyncSupplier<T> onErrorIf(
128129
return;
129130
}
130131
if (errorMatched) {
131-
supplier.unsafeFinish(callback);
132+
errorFunction.unsafeFinish(e, callback);
132133
} else {
133134
callback.onResult(null, e);
134135
}

driver-core/src/main/com/mongodb/internal/async/SingleResultCallback.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.mongodb.internal.async;
1818

19+
import com.mongodb.connection.AsyncCompletionHandler;
1920
import com.mongodb.internal.async.function.AsyncCallbackFunction;
2021
import com.mongodb.lang.Nullable;
2122

@@ -34,4 +35,25 @@ public interface SingleResultCallback<T> {
3435
* @throws Error Never, on the best effort basis.
3536
*/
3637
void onResult(@Nullable T result, @Nullable Throwable t);
38+
39+
default AsyncCompletionHandler<T> toHandler() {
40+
return new AsyncCompletionHandler<T>() {
41+
@Override
42+
public void completed(@Nullable final T result) {
43+
onResult(result, null);
44+
}
45+
@Override
46+
public void failed(final Throwable t) {
47+
onResult(null, t);
48+
}
49+
};
50+
}
51+
52+
default void complete() {
53+
this.onResult(null, null);
54+
}
55+
56+
default void complete(final T result) {
57+
this.onResult(result, null);
58+
}
3759
}

0 commit comments

Comments
 (0)