2 * Copyright 2017-present Open Networking Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package io.atomix.utils.concurrent;
18 import org.junit.Test;
20 import java.util.concurrent.CompletableFuture;
21 import java.util.concurrent.atomic.AtomicInteger;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
27 * Ordered completable future test.
29 public class OrderedFutureTest {
32 * Tests ordered completion of future callbacks.
35 public void testOrderedCompletion() throws Throwable {
36 CompletableFuture<String> future = new OrderedFuture<>();
37 AtomicInteger order = new AtomicInteger();
38 future.whenComplete((r, e) -> assertEquals(1, order.incrementAndGet()));
39 future.whenComplete((r, e) -> assertEquals(2, order.incrementAndGet()));
40 future.handle((r, e) -> {
41 assertEquals(3, order.incrementAndGet());
42 assertEquals("foo", r);
45 future.thenRun(() -> assertEquals(3, order.incrementAndGet()));
46 future.thenAccept(r -> {
47 assertEquals(5, order.incrementAndGet());
48 assertEquals("foo", r);
50 future.thenApply(r -> {
51 assertEquals(6, order.incrementAndGet());
52 assertEquals("foo", r);
55 future.whenComplete((r, e) -> {
56 assertEquals(7, order.incrementAndGet());
57 assertEquals("foo", r);
59 future.complete("foo");
63 * Tests ordered failure of future callbacks.
65 public void testOrderedFailure() throws Throwable {
66 CompletableFuture<String> future = new OrderedFuture<>();
67 AtomicInteger order = new AtomicInteger();
68 future.whenComplete((r, e) -> assertEquals(1, order.incrementAndGet()));
69 future.whenComplete((r, e) -> assertEquals(2, order.incrementAndGet()));
70 future.handle((r, e) -> {
71 assertEquals(3, order.incrementAndGet());
74 future.thenRun(() -> fail());
75 future.thenAccept(r -> fail());
76 future.exceptionally(e -> {
77 assertEquals(3, order.incrementAndGet());
80 future.completeExceptionally(new RuntimeException("foo"));
84 * Tests calling callbacks that are added after completion.
86 public void testAfterComplete() throws Throwable {
87 CompletableFuture<String> future = new OrderedFuture<>();
88 future.whenComplete((result, error) -> assertEquals("foo", result));
89 future.complete("foo");
90 AtomicInteger count = new AtomicInteger();
91 future.whenComplete((result, error) -> {
92 assertEquals("foo", result);
93 assertEquals(1, count.incrementAndGet());
95 future.thenAccept(result -> {
96 assertEquals("foo", result);
97 assertEquals(2, count.incrementAndGet());
99 assertEquals(2, count.get());