|
24 | 24 | import org.apache.commons.lang3.StringUtils;
|
25 | 25 | import org.junit.Before;
|
26 | 26 | import org.junit.Test;
|
| 27 | +import rx.Completable; |
27 | 28 | import rx.Observable;
|
28 | 29 | import rx.Observer;
|
| 30 | +import rx.Single; |
29 | 31 | import rx.Subscriber;
|
30 | 32 | import rx.functions.Action1;
|
31 |
| -import rx.subjects.ReplaySubject; |
| 33 | +import rx.functions.Func0; |
32 | 34 |
|
33 | 35 | import static com.netflix.hystrix.contrib.javanica.test.common.CommonUtils.getHystrixCommandByKey;
|
34 | 36 | import static org.junit.Assert.assertEquals;
|
@@ -90,6 +92,82 @@ public void call(User user) {
|
90 | 92 | assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.SUCCESS));
|
91 | 93 | }
|
92 | 94 |
|
| 95 | + @Test |
| 96 | + public void testGetCompletableUser(){ |
| 97 | + userService.getCompletableUser("1", "name: "); |
| 98 | + com.netflix.hystrix.HystrixInvokableInfo getUserCommand = getHystrixCommandByKey("getCompletableUser"); |
| 99 | + assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.SUCCESS)); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testGetCompletableUserWithRegularFallback() { |
| 104 | + Completable completable = userService.getCompletableUserWithRegularFallback(null, "name: "); |
| 105 | + completable.<User>toObservable().subscribe(new Action1<User>() { |
| 106 | + @Override |
| 107 | + public void call(User user) { |
| 108 | + assertEquals("default_id", user.getId()); |
| 109 | + } |
| 110 | + }); |
| 111 | + com.netflix.hystrix.HystrixInvokableInfo getUserCommand = getHystrixCommandByKey("getCompletableUserWithRegularFallback"); |
| 112 | + assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.FAILURE)); |
| 113 | + assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.FALLBACK_SUCCESS)); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testGetCompletableUserWithRxFallback() { |
| 118 | + Completable completable = userService.getCompletableUserWithRxFallback(null, "name: "); |
| 119 | + completable.<User>toObservable().subscribe(new Action1<User>() { |
| 120 | + @Override |
| 121 | + public void call(User user) { |
| 122 | + assertEquals("default_id", user.getId()); |
| 123 | + } |
| 124 | + }); |
| 125 | + com.netflix.hystrix.HystrixInvokableInfo getUserCommand = getHystrixCommandByKey("getCompletableUserWithRxFallback"); |
| 126 | + assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.FAILURE)); |
| 127 | + assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.FALLBACK_SUCCESS)); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testGetSingleUser() { |
| 132 | + final String id = "1"; |
| 133 | + Single<User> user = userService.getSingleUser(id, "name: "); |
| 134 | + user.subscribe(new Action1<User>() { |
| 135 | + @Override |
| 136 | + public void call(User user) { |
| 137 | + assertEquals(id, user.getId()); |
| 138 | + } |
| 139 | + }); |
| 140 | + com.netflix.hystrix.HystrixInvokableInfo getUserCommand = getHystrixCommandByKey("getSingleUser"); |
| 141 | + assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.SUCCESS)); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void testGetSingleUserWithRegularFallback(){ |
| 146 | + Single<User> user = userService.getSingleUserWithRegularFallback(null, "name: "); |
| 147 | + user.subscribe(new Action1<User>() { |
| 148 | + @Override |
| 149 | + public void call(User user) { |
| 150 | + assertEquals("default_id", user.getId()); |
| 151 | + } |
| 152 | + }); |
| 153 | + com.netflix.hystrix.HystrixInvokableInfo getUserCommand = getHystrixCommandByKey("getSingleUserWithRegularFallback"); |
| 154 | + assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.FAILURE)); |
| 155 | + assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.FALLBACK_SUCCESS)); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testGetSingleUserWithRxFallback(){ |
| 160 | + Single<User> user = userService.getSingleUserWithRxFallback(null, "name: "); |
| 161 | + user.subscribe(new Action1<User>() { |
| 162 | + @Override |
| 163 | + public void call(User user) { |
| 164 | + assertEquals("default_id", user.getId()); |
| 165 | + } |
| 166 | + }); |
| 167 | + com.netflix.hystrix.HystrixInvokableInfo getUserCommand = getHystrixCommandByKey("getSingleUserWithRxFallback"); |
| 168 | + assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.FAILURE)); |
| 169 | + assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.FALLBACK_SUCCESS)); |
| 170 | + } |
93 | 171 |
|
94 | 172 | @Test
|
95 | 173 | public void testGetUserWithRegularFallback() {
|
@@ -163,6 +241,59 @@ public Observable<User> getUser(final String id, final String name) {
|
163 | 241 | return createObservable(id, name);
|
164 | 242 | }
|
165 | 243 |
|
| 244 | + @HystrixCommand |
| 245 | + public Completable getCompletableUser(final String id, final String name) { |
| 246 | + validate(id, name, "getCompletableUser has failed"); |
| 247 | + return createObservable(id, name).toCompletable(); |
| 248 | + } |
| 249 | + |
| 250 | + @HystrixCommand(fallbackMethod = "completableUserRegularFallback") |
| 251 | + public Completable getCompletableUserWithRegularFallback(final String id, final String name) { |
| 252 | + return getCompletableUser(id, name); |
| 253 | + } |
| 254 | + |
| 255 | + @HystrixCommand(fallbackMethod = "completableUserRxFallback") |
| 256 | + public Completable getCompletableUserWithRxFallback(final String id, final String name) { |
| 257 | + return getCompletableUser(id, name); |
| 258 | + } |
| 259 | + |
| 260 | + public User completableUserRegularFallback(final String id, final String name) { |
| 261 | + return new User("default_id", "default_name"); |
| 262 | + } |
| 263 | + |
| 264 | + public Completable completableUserRxFallback(final String id, final String name) { |
| 265 | + return Completable.fromCallable(new Func0<User>() { |
| 266 | + @Override |
| 267 | + public User call() { |
| 268 | + return new User("default_id", "default_name"); |
| 269 | + } |
| 270 | + }); |
| 271 | + } |
| 272 | + |
| 273 | + @HystrixCommand |
| 274 | + public Single<User> getSingleUser(final String id, final String name) { |
| 275 | + validate(id, name, "getSingleUser has failed"); |
| 276 | + return createObservable(id, name).toSingle(); |
| 277 | + } |
| 278 | + |
| 279 | + @HystrixCommand(fallbackMethod = "singleUserRegularFallback") |
| 280 | + public Single<User> getSingleUserWithRegularFallback(final String id, final String name) { |
| 281 | + return getSingleUser(id, name); |
| 282 | + } |
| 283 | + |
| 284 | + @HystrixCommand(fallbackMethod = "singleUserRxFallback") |
| 285 | + public Single<User> getSingleUserWithRxFallback(final String id, final String name) { |
| 286 | + return getSingleUser(id, name); |
| 287 | + } |
| 288 | + |
| 289 | + User singleUserRegularFallback(final String id, final String name) { |
| 290 | + return new User("default_id", "default_name"); |
| 291 | + } |
| 292 | + |
| 293 | + Single<User> singleUserRxFallback(final String id, final String name) { |
| 294 | + return createObservable("default_id", "default_name").toSingle(); |
| 295 | + } |
| 296 | + |
166 | 297 | @HystrixCommand(fallbackMethod = "regularFallback", observableExecutionMode = ObservableExecutionMode.LAZY)
|
167 | 298 | public Observable<User> getUserRegularFallback(final String id, final String name) {
|
168 | 299 | validate(id, name, "getUser has failed");
|
|
0 commit comments