1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.hc.core5.concurrent;
28
29 import static org.hamcrest.MatcherAssert.assertThat;
30
31 import java.util.concurrent.Future;
32
33 import org.hamcrest.CoreMatchers;
34 import org.junit.jupiter.api.Assertions;
35 import org.junit.jupiter.api.Test;
36
37 public class TestComplexFuture {
38
39 @Test
40 public void testCancelled() throws Exception {
41 final ComplexFuture<Object> future = new ComplexFuture<>(null);
42
43 final Future<Object> dependency1 = new BasicFuture<>(null);
44 future.setDependency(dependency1);
45
46 Assertions.assertFalse(future.isDone());
47
48 future.cancel();
49 assertThat(future.isCancelled(), CoreMatchers.is(true));
50 assertThat(dependency1.isCancelled(), CoreMatchers.is(true));
51
52 final Future<Object> dependency2 = new BasicFuture<>(null);
53 future.setDependency(dependency2);
54 assertThat(dependency2.isCancelled(), CoreMatchers.is(true));
55 }
56
57 @Test
58 public void testCompleted() throws Exception {
59 final ComplexFuture<Object> future = new ComplexFuture<>(null);
60
61 final Future<Object> dependency1 = new BasicFuture<>(null);
62 future.setDependency(dependency1);
63
64 Assertions.assertFalse(future.isDone());
65
66 future.completed(Boolean.TRUE);
67 assertThat(future.isCancelled(), CoreMatchers.is(false));
68 assertThat(dependency1.isCancelled(), CoreMatchers.is(false));
69
70 final Future<Object> dependency2 = new BasicFuture<>(null);
71 future.setDependency(dependency2);
72 assertThat(dependency2.isCancelled(), CoreMatchers.is(true));
73 }
74
75 @Test
76 public void testCancelledWithCallback() throws Exception {
77 final ComplexFuture<Object> future = new ComplexFuture<>(null);
78
79 final Future<Object> dependency1 = new BasicFuture<>(new FutureContribution<Object>(future) {
80
81 @Override
82 public void completed(final Object result) {
83 }
84
85 });
86 future.setDependency(dependency1);
87
88 Assertions.assertFalse(future.isDone());
89
90 future.cancel();
91 assertThat(future.isCancelled(), CoreMatchers.is(true));
92 assertThat(dependency1.isCancelled(), CoreMatchers.is(true));
93
94 final Future<Object> dependency2 = new BasicFuture<>(null);
95 future.setDependency(dependency2);
96 assertThat(dependency2.isCancelled(), CoreMatchers.is(true));
97 }
98
99 @Test
100 public void testCanceledAndFailed() {
101 final ComplexFuture<Object> future = new ComplexFuture<>(null);
102 assertThat(future.cancel(), CoreMatchers.is(true));
103 assertThat(future.failed(new Exception()), CoreMatchers.is(false));
104 }
105
106 }