aboutsummaryrefslogtreecommitdiff
path: root/guava-tests/test/com/google/common/util/concurrent/FluentFutureTest.java
blob: 48d5c8e5a0fc633d7f6485abeb3573ad5e0c76ea (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * Copyright (C) 2016 The Guava Authors
 *
 * 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 com.google.common.util.concurrent;

import static com.google.common.base.Verify.verify;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.util.concurrent.Futures.immediateFailedFuture;
import static com.google.common.util.concurrent.Futures.immediateFuture;
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
import static java.util.concurrent.Executors.newScheduledThreadPool;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.assertThrows;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.Function;
import com.google.common.util.concurrent.ForwardingListenableFuture.SimpleForwardingListenableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeoutException;
import junit.framework.TestCase;

/**
 * Tests for {@link FluentFuture}. The tests cover only the basics for the API. The actual logic is
 * tested in {@link FuturesTest}.
 */
@GwtCompatible(emulated = true)
public class FluentFutureTest extends TestCase {
  public void testFromFluentFuture() {
    FluentFuture<String> f = FluentFuture.from(SettableFuture.<String>create());
    assertThat(FluentFuture.from(f)).isSameInstanceAs(f);
  }

  public void testFromFluentFuturePassingAsNonFluent() {
    ListenableFuture<String> f = FluentFuture.from(SettableFuture.<String>create());
    assertThat(FluentFuture.from(f)).isSameInstanceAs(f);
  }

  public void testFromNonFluentFuture() throws Exception {
    ListenableFuture<String> f =
        new SimpleForwardingListenableFuture<String>(immediateFuture("a")) {};
    verify(!(f instanceof FluentFuture));
    assertThat(FluentFuture.from(f).get()).isEqualTo("a");
    // TODO(cpovirk): Test forwarding more extensively.
  }

  public void testAddCallback() {
    FluentFuture<String> f = FluentFuture.from(immediateFuture("a"));
    final boolean[] called = new boolean[1];
    f.addCallback(
        new FutureCallback<String>() {
          @Override
          public void onSuccess(String result) {
            called[0] = true;
          }

          @Override
          public void onFailure(Throwable t) {}
        },
        directExecutor());
    assertThat(called[0]).isTrue();
  }

  public void testCatching() throws Exception {
    FluentFuture<?> f =
        FluentFuture.from(immediateFailedFuture(new RuntimeException()))
            .catching(
                Throwable.class,
                new Function<Throwable, Class<?>>() {
                  @Override
                  public Class<?> apply(Throwable input) {
                    return input.getClass();
                  }
                },
                directExecutor());
    assertThat(f.get()).isEqualTo(RuntimeException.class);
  }

  public void testCatchingAsync() throws Exception {
    FluentFuture<?> f =
        FluentFuture.from(immediateFailedFuture(new RuntimeException()))
            .catchingAsync(
                Throwable.class,
                new AsyncFunction<Throwable, Class<?>>() {
                  @Override
                  public ListenableFuture<Class<?>> apply(Throwable input) {
                    return Futures.<Class<?>>immediateFuture(input.getClass());
                  }
                },
                directExecutor());
    assertThat(f.get()).isEqualTo(RuntimeException.class);
  }

  public void testTransform() throws Exception {
    FluentFuture<Integer> f =
        FluentFuture.from(immediateFuture(1))
            .transform(
                new Function<Integer, Integer>() {
                  @Override
                  public Integer apply(Integer input) {
                    return input + 1;
                  }
                },
                directExecutor());
    assertThat(f.get()).isEqualTo(2);
  }

  public void testTransformAsync() throws Exception {
    FluentFuture<Integer> f =
        FluentFuture.from(immediateFuture(1))
            .transformAsync(
                new AsyncFunction<Integer, Integer>() {
                  @Override
                  public ListenableFuture<Integer> apply(Integer input) {
                    return immediateFuture(input + 1);
                  }
                },
                directExecutor());
    assertThat(f.get()).isEqualTo(2);
  }

  @GwtIncompatible // withTimeout
  public void testWithTimeout() throws Exception {
    ScheduledExecutorService executor = newScheduledThreadPool(1);
    try {
      FluentFuture<?> f =
          FluentFuture.from(SettableFuture.create()).withTimeout(0, SECONDS, executor);
      ExecutionException e = assertThrows(ExecutionException.class, () -> f.get());
      assertThat(e).hasCauseThat().isInstanceOf(TimeoutException.class);
    } finally {
      executor.shutdown();
    }
  }
}