Skip to content

Commit 1b1aac9

Browse files
committed
Unit tests for AsyncResult callback behavior
Issue: SPR-14249
1 parent 5400bb9 commit 1b1aac9

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2002-2016 the original author or 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 org.springframework.scheduling.annotation;
18+
19+
import java.io.IOException;
20+
import java.util.HashSet;
21+
import java.util.Set;
22+
23+
import org.junit.Test;
24+
25+
import org.springframework.util.concurrent.ListenableFuture;
26+
import org.springframework.util.concurrent.ListenableFutureCallback;
27+
28+
import static org.junit.Assert.*;
29+
30+
/**
31+
* @author Juergen Hoeller
32+
*/
33+
public class AsyncResultTests {
34+
35+
@Test
36+
public void asyncResultWithCallbackAndValue() {
37+
String value = "val";
38+
final Set<String> values = new HashSet<>(1);
39+
ListenableFuture<String> future = AsyncResult.forValue(value);
40+
future.addCallback(new ListenableFutureCallback<String>() {
41+
@Override
42+
public void onSuccess(String result) {
43+
values.add(result);
44+
}
45+
@Override
46+
public void onFailure(Throwable ex) {
47+
fail("Failure callback not expected: " + ex);
48+
}
49+
});
50+
assertSame(value, values.iterator().next());
51+
}
52+
53+
@Test
54+
public void asyncResultWithCallbackAndException() {
55+
IOException ex = new IOException();
56+
final Set<Throwable> values = new HashSet<>(1);
57+
ListenableFuture<String> future = AsyncResult.forExecutionException(ex);
58+
future.addCallback(new ListenableFutureCallback<String>() {
59+
@Override
60+
public void onSuccess(String result) {
61+
fail("Success callback not expected: " + result);
62+
}
63+
@Override
64+
public void onFailure(Throwable ex) {
65+
values.add(ex);
66+
}
67+
});
68+
assertSame(ex, values.iterator().next());
69+
}
70+
71+
@Test
72+
public void asyncResultWithSeparateCallbacksAndValue() {
73+
String value = "val";
74+
final Set<String> values = new HashSet<>(1);
75+
ListenableFuture<String> future = AsyncResult.forValue(value);
76+
future.addCallback(values::add, (ex) -> fail("Failure callback not expected: " + ex));
77+
assertSame(value, values.iterator().next());
78+
}
79+
80+
@Test
81+
public void asyncResultWithSeparateCallbacksAndException() {
82+
IOException ex = new IOException();
83+
final Set<Throwable> values = new HashSet<>(1);
84+
ListenableFuture<String> future = AsyncResult.forExecutionException(ex);
85+
future.addCallback((result) -> fail("Success callback not expected: " + result), values::add);
86+
assertSame(ex, values.iterator().next());
87+
}
88+
89+
}

0 commit comments

Comments
 (0)