• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Dagger Authors.
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 
17 package dagger.functional.producers.cancellation;
18 
19 import static com.google.common.base.Preconditions.checkNotNull;
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import com.google.common.util.concurrent.ListenableFuture;
23 import dagger.producers.Producer;
24 import dagger.producers.ProducerModule;
25 import dagger.producers.Produces;
26 import javax.inject.Named;
27 
28 @SuppressWarnings("unused") // not actually using dependencies
29 @ProducerModule
30 final class CancellationSubcomponentModule {
31 
32   private final ProducerTester tester;
33 
CancellationSubcomponentModule(ProducerTester tester)34   CancellationSubcomponentModule(ProducerTester tester) {
35     this.tester = checkNotNull(tester);
36   }
37 
38   @Produces
39   @Named("subLeaf")
produceSubLeaf()40   ListenableFuture<String> produceSubLeaf() {
41     return tester.start("subLeaf");
42   }
43 
44   @Produces
45   @Named("subTask1")
produceSubTask1( @amed"subLeaf") String subLeaf, @Named("qux") String qux)46   ListenableFuture<String> produceSubTask1(
47       @Named("subLeaf") String subLeaf, @Named("qux") String qux) {
48     return tester.start("subTask1");
49   }
50 
51   @Produces
52   @Named("subTask2")
produceSubTask2(@amed"foo") String foo, Producer<String> dependency)53   ListenableFuture<String> produceSubTask2(@Named("foo") String foo, Producer<String> dependency) {
54     ListenableFuture<String> dependencyFuture = dependency.get();
55     assertThat(dependencyFuture.cancel(true)).isTrue();
56     assertThat(dependencyFuture.isCancelled()).isTrue();
57     return tester.start("subTask2");
58   }
59 
60   @Produces
61   @Named("subEntryPoint")
produceSubEntryPoint( @amed"subTask1") String subTask1, @Named("subTask2") String subTask2)62   ListenableFuture<String> produceSubEntryPoint(
63       @Named("subTask1") String subTask1, @Named("subTask2") String subTask2) {
64     return tester.start("subEntryPoint");
65   }
66 }
67