Skip to content

Commit

Permalink
Make it possible to build a Completion from a callback (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvest authored Dec 28, 2024
2 parents 64073ba + 2815a23 commit 0ba5af1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/main/java/stormpot/Completion.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.concurrent.Flow;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinPool.ManagedBlocker;
import java.util.function.Consumer;

/**
* A Completion represents some task that is going to be completed at some
Expand Down Expand Up @@ -54,6 +55,20 @@ static Completion from(CompletionStage<?> stage) {
stage.whenComplete((v, e) -> completion.complete());
return completion;
}

/**
* Create a completion using the given callback handler.
* The {@link Consumer} argument will be given a {@link Runnable} instance,
* which will complete the {@link Completion} when the {@link Runnable#run()} method is called.
*
* @param completionCallback The handler controlling the completion.
* @return A {@link Completion} that completes when the given {@link Runnable#run()} method is called.
*/
static Completion from(Consumer<Runnable> completionCallback) {
StackCompletion completion = new StackCompletion();
completionCallback.accept(completion::complete);
return completion;
}

/**
* Causes the current thread to wait until the completion is finished,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/stormpot/internal/StackCompletion.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/**
* An implementation of {@link Completion} based on a wait-free stack.
* This implementation also supports a callback mechanism that trigger every time
* a and {@link #await(Timeout)} or {@link #block()} call is about to block.
* an {@link #await(Timeout)} or {@link #block()} call is about to block.
*/
public final class StackCompletion implements Completion {
private static final Node END = new Node("END");
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/stormpot/tests/StackCompletionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Flow;
import java.util.concurrent.Future;
Expand All @@ -48,7 +47,6 @@ class StackCompletionTest {
static final Timeout longTimeout = new Timeout(5, TimeUnit.MINUTES);

protected Completion completion;
protected CompletionStage<Object> stage;

@BeforeEach
void setUp() {
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/stormpot/tests/StackCompletionWithCallbackTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright © 2011-2024 Chris Vest ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package stormpot.tests;

import org.junit.jupiter.api.BeforeEach;
import stormpot.Completion;

public class StackCompletionWithCallbackTest extends StackCompletionTest {
protected Runnable callback;

@BeforeEach
@Override
void setUp() {
completion = Completion.from(cb -> this.callback = cb);
}

@Override
void complete() {
callback.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
import stormpot.Completion;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

public class StackCompletionWithCompletionStageTest extends StackCompletionTest {
protected CompletionStage<Object> stage;

@BeforeEach
@Override
void setUp() {
Expand Down

0 comments on commit 0ba5af1

Please sign in to comment.