Skip to content

Commit 92548c1

Browse files
committed
improve docs
1 parent e70e375 commit 92548c1

2 files changed

Lines changed: 37 additions & 11 deletions

File tree

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,30 @@ public void run(final SingleResultCallback<Void> callback) {
6363
* {@link #body}{@code .}{@link AsyncCallbackRunnable#run(SingleResultCallback) run}.
6464
* The initiated iteration may be executed either synchronously or asynchronously with the method that initiated it:
6565
* <ul>
66-
* <li>synchronous execution—completion of the initiated iteration happens-before the method completion;</li>
67-
* <li>asynchronous execution—the aforementioned relation does not exist.</li>
66+
* <li>synchronous execution—completion of the initiated iteration is guaranteed to happen-before the method completion;
67+
* <ul>
68+
* <li>Note that the formulations
69+
* <ol>
70+
* <li>"completion of the initiated iteration is guaranteed to happen-before the method completion"</li>
71+
* <li>"completion of the initiated iteration happens-before the method completion"</li>
72+
* </ol>
73+
* are different: the former is about the program while the latter is about the execution, and follows from the former.
74+
* For us the former is useful.
75+
* </li>
76+
* </ul>
77+
* </li>
78+
* <li>asynchronous execution—the aforementioned guarantee does not exist.
79+
* <ul>
80+
* <li>Note that the formulations
81+
* <ol>
82+
* <li>"the aforementioned guarantee does not exist"</li>
83+
* <li>"the aforementioned relation does not exist"</li>
84+
* </ol>
85+
* are different: the former is about the program while the latter is about the execution, and follows from the former.
86+
* For us the former is useful.
87+
* </li>
88+
* </ul>
89+
* </li>
6890
* </ul>
6991
*
7092
* <p>If another iteration is needed, it is initiated from the callback passed to

driver-core/src/test/unit/com/mongodb/internal/async/VakoTest.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ private enum IterationExecutionType {
7878
SYNC_SAME_THREAD,
7979
SYNC_DIFFERENT_THREAD,
8080
ASYNC,
81-
MIXED_SYNC_SAME_AND_ASYNC
81+
MIXED_SYNC_SAME_THREAD_AND_ASYNC
8282
}
8383

8484
@ParameterizedTest()
8585
@CsvSource({
8686
"1_000_000, 0, SYNC_SAME_THREAD, 0, false",
8787
// "1_000_000, 0, SYNC_DIFFERENT_THREAD, 0, false",
8888
"1_000_000, 0, ASYNC, 0, false",
89-
"1_000_000, 0, MIXED_SYNC_SAME_AND_ASYNC, 0, false",
89+
"1_000_000, 0, MIXED_SYNC_SAME_THREAD_AND_ASYNC, 0, false",
9090
"4, 0, ASYNC, 4, true",
9191
"4, 4, ASYNC, 0, true",
9292
})
@@ -105,10 +105,10 @@ void testThenRunDoWhileLoop(
105105
CompletableFuture<Void> join = new CompletableFuture<>();
106106
asyncLoop(new Counter(counterInitialValue, verbose),
107107
blockSyncPartOfIterationTotalDuration, executionType, delayAsyncExecutionTotalDuration, verbose,
108-
(r, t) -> {
109-
System.err.printf("test callback completed callStackDepth=%s, r=%s, t=%s%n",
110-
Thread.currentThread().getStackTrace().length, r, exceptionToString(t));
111-
complete(join, r, t);
108+
(r, t) -> {
109+
System.err.printf("test callback completed callStackDepth=%s, r=%s, t=%s%n",
110+
Thread.currentThread().getStackTrace().length, r, exceptionToString(t));
111+
complete(join, r, t);
112112
});
113113
System.err.printf("\tasyncLoop method completed in %s%n", start.elapsed());
114114
join.get();
@@ -162,7 +162,7 @@ private static void asyncPartOfIteration(
162162
delayAsyncExecutionTotalDuration.dividedBy(counter.initial()).toNanos(), TimeUnit.NANOSECONDS);
163163
break;
164164
}
165-
case MIXED_SYNC_SAME_AND_ASYNC: {
165+
case MIXED_SYNC_SAME_THREAD_AND_ASYNC: {
166166
if (ThreadLocalRandom.current().nextBoolean()) {
167167
asyncPartOfIteration.run();
168168
} else {
@@ -180,11 +180,13 @@ private static void asyncPartOfIteration(
180180
private static final class Counter {
181181
private final int initial;
182182
private int current;
183+
private boolean doneReturnedTrue;
183184
private final boolean verbose;
184185

185186
Counter(final int initial, final boolean verbose) {
186187
this.initial = initial;
187188
this.current = initial;
189+
this.doneReturnedTrue = false;
188190
this.verbose = verbose;
189191
}
190192

@@ -197,14 +199,16 @@ void countDown() {
197199
int previous = current;
198200
int decremented = --current;
199201
if (verbose || decremented % 100_000 == 0) {
200-
System.err.printf("counted %d->%d callStackDepth=%d %n",
201-
previous, decremented, Thread.currentThread().getStackTrace().length);
202+
System.err.printf("counted %d->%d tid=%d callStackDepth=%d %n",
203+
previous, decremented, Thread.currentThread().getId(), Thread.currentThread().getStackTrace().length);
202204
}
203205
}
204206

205207
boolean done() {
206208
if (current == 0) {
209+
com.mongodb.assertions.Assertions.assertFalse(doneReturnedTrue);
207210
System.err.printf("counting done callStackDepth=%d %n", Thread.currentThread().getStackTrace().length);
211+
doneReturnedTrue = true;
208212
return true;
209213
}
210214
return false;

0 commit comments

Comments
 (0)