|
30 | 30 | import rx.Single;
|
31 | 31 | import rx.Subscriber;
|
32 | 32 | import rx.functions.Action1;
|
33 |
| -import rx.subjects.ReplaySubject; |
| 33 | +import rx.functions.Func0; |
34 | 34 |
|
35 | 35 | import static com.netflix.hystrix.contrib.javanica.test.common.CommonUtils.getHystrixCommandByKey;
|
36 | 36 | import static org.junit.Assert.assertEquals;
|
@@ -93,19 +93,82 @@ public void call(User user) {
|
93 | 93 | }
|
94 | 94 |
|
95 | 95 | @Test
|
96 |
| - public void testCompletable(){ |
97 |
| - userService.getUserCompletable("1", "name: "); |
98 |
| - com.netflix.hystrix.HystrixInvokableInfo getUserCommand = getHystrixCommandByKey("getUserCompletable"); |
| 96 | + public void testGetCompletableUser(){ |
| 97 | + userService.getCompletableUser("1", "name: "); |
| 98 | + com.netflix.hystrix.HystrixInvokableInfo getUserCommand = getHystrixCommandByKey("getCompletableUser"); |
99 | 99 | assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.SUCCESS));
|
100 | 100 | }
|
101 | 101 |
|
102 | 102 | @Test
|
103 |
| - public void testSingle(){ |
104 |
| - userService.getUserSingle("1", "name: "); |
105 |
| - com.netflix.hystrix.HystrixInvokableInfo getUserCommand = getHystrixCommandByKey("getUserSingle"); |
| 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"); |
106 | 141 | assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.SUCCESS));
|
107 | 142 | }
|
108 | 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 | + } |
| 171 | + |
109 | 172 | @Test
|
110 | 173 | public void testGetUserWithRegularFallback() {
|
111 | 174 | final User exUser = new User("def", "def");
|
@@ -179,17 +242,58 @@ public Observable<User> getUser(final String id, final String name) {
|
179 | 242 | }
|
180 | 243 |
|
181 | 244 | @HystrixCommand
|
182 |
| - public Completable getUserCompletable(final String id, final String name) { |
183 |
| - validate(id, name, "getUser has failed"); |
| 245 | + public Completable getCompletableUser(final String id, final String name) { |
| 246 | + validate(id, name, "getCompletableUser has failed"); |
184 | 247 | return createObservable(id, name).toCompletable();
|
185 | 248 | }
|
186 | 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 | + |
187 | 273 | @HystrixCommand
|
188 |
| - public Single getUserSingle(final String id, final String name) { |
189 |
| - validate(id, name, "getUser has failed"); |
| 274 | + public Single<User> getSingleUser(final String id, final String name) { |
| 275 | + validate(id, name, "getSingleUser has failed"); |
190 | 276 | return createObservable(id, name).toSingle();
|
191 | 277 | }
|
192 | 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 | + |
193 | 297 | @HystrixCommand(fallbackMethod = "regularFallback", observableExecutionMode = ObservableExecutionMode.LAZY)
|
194 | 298 | public Observable<User> getUserRegularFallback(final String id, final String name) {
|
195 | 299 | validate(id, name, "getUser has failed");
|
|
0 commit comments