3636import java .util .List ;
3737import java .util .concurrent .CancellationException ;
3838import java .util .concurrent .ExecutionException ;
39+ import java .util .concurrent .Executor ;
3940import java .util .concurrent .atomic .AtomicInteger ;
4041import org .junit .Test ;
4142
@@ -95,6 +96,31 @@ public String apply(Integer input) {
9596 assertThat (transformedFuture .get ()).isEqualTo ("6" );
9697 }
9798
99+ @ Test
100+ public void testTransformWithExecutor () throws Exception {
101+ SettableApiFuture <Integer > inputFuture = SettableApiFuture .<Integer >create ();
102+ final AtomicInteger counter = new AtomicInteger (0 );
103+ ApiFuture <String > transformedFuture =
104+ ApiFutures .transform (
105+ inputFuture ,
106+ new ApiFunction <Integer , String >() {
107+ @ Override
108+ public String apply (Integer input ) {
109+ return input .toString ();
110+ }
111+ },
112+ new Executor () {
113+ @ Override
114+ public void execute (Runnable command ) {
115+ counter .incrementAndGet ();
116+ command .run ();
117+ }
118+ });
119+ inputFuture .set (6 );
120+ assertThat (transformedFuture .get ()).isEqualTo ("6" );
121+ assertThat (counter .get ()).isEqualTo (1 );
122+ }
123+
98124 @ Test
99125 public void testAllAsList () throws Exception {
100126 SettableApiFuture <Integer > inputFuture1 = SettableApiFuture .<Integer >create ();
@@ -121,6 +147,30 @@ public ApiFuture<Integer> apply(Integer input) {
121147 assertThat (outputFuture .get ()).isEqualTo (1 );
122148 }
123149
150+ @ Test
151+ public void testTransformAsyncWithExecutor () throws Exception {
152+ ApiFuture <Integer > inputFuture = ApiFutures .immediateFuture (0 );
153+ final AtomicInteger counter = new AtomicInteger (0 );
154+ ApiFuture <Integer > outputFuture =
155+ ApiFutures .transformAsync (
156+ inputFuture ,
157+ new ApiAsyncFunction <Integer , Integer >() {
158+ @ Override
159+ public ApiFuture <Integer > apply (Integer input ) {
160+ return ApiFutures .immediateFuture (input + 1 );
161+ }
162+ },
163+ new Executor () {
164+ @ Override
165+ public void execute (Runnable command ) {
166+ counter .incrementAndGet ();
167+ command .run ();
168+ }
169+ });
170+ assertThat (outputFuture .get ()).isEqualTo (1 );
171+ assertThat (counter .get ()).isEqualTo (1 );
172+ }
173+
124174 @ Test
125175 public void testImmediateFailedFuture () throws InterruptedException {
126176 ApiFuture <String > future =
0 commit comments