Import atomix/{storage,utils}
[controller.git] / third-party / atomix / utils / src / test / java / io / atomix / utils / concurrent / OrderedFutureTest.java
1 /*
2  * Copyright 2017-present Open Networking Foundation
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package io.atomix.utils.concurrent;
17
18 import org.junit.Test;
19
20 import java.util.concurrent.CompletableFuture;
21 import java.util.concurrent.atomic.AtomicInteger;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
25
26 /**
27  * Ordered completable future test.
28  */
29 public class OrderedFutureTest {
30
31   /**
32    * Tests ordered completion of future callbacks.
33    */
34   @Test
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);
43       return "bar";
44     });
45     future.thenRun(() -> assertEquals(3, order.incrementAndGet()));
46     future.thenAccept(r -> {
47       assertEquals(5, order.incrementAndGet());
48       assertEquals("foo", r);
49     });
50     future.thenApply(r -> {
51       assertEquals(6, order.incrementAndGet());
52       assertEquals("foo", r);
53       return "bar";
54     });
55     future.whenComplete((r, e) -> {
56       assertEquals(7, order.incrementAndGet());
57       assertEquals("foo", r);
58     });
59     future.complete("foo");
60   }
61
62   /**
63    * Tests ordered failure of future callbacks.
64    */
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());
72       return "bar";
73     });
74     future.thenRun(() -> fail());
75     future.thenAccept(r -> fail());
76     future.exceptionally(e -> {
77       assertEquals(3, order.incrementAndGet());
78       return "bar";
79     });
80     future.completeExceptionally(new RuntimeException("foo"));
81   }
82
83   /**
84    * Tests calling callbacks that are added after completion.
85    */
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());
94     });
95     future.thenAccept(result -> {
96       assertEquals("foo", result);
97       assertEquals(2, count.incrementAndGet());
98     });
99     assertEquals(2, count.get());
100   }
101 }