18
18
import static org .hamcrest .CoreMatchers .not ;
19
19
import static org .hamcrest .MatcherAssert .assertThat ;
20
20
import static org .mockito .Matchers .any ;
21
+ import static org .mockito .Matchers .isA ;
21
22
import static org .mockito .Mockito .doThrow ;
22
23
import static org .mockito .Mockito .inOrder ;
23
24
import static org .mockito .Mockito .mock ;
24
25
import static org .mockito .Mockito .never ;
25
26
import static org .mockito .Mockito .times ;
26
27
import static org .mockito .Mockito .verify ;
27
28
import static org .mockito .Mockito .verifyNoMoreInteractions ;
29
+ import static org .mockito .Mockito .verifyZeroInteractions ;
28
30
import static org .mockito .Mockito .when ;
29
31
import static org .testng .Assert .assertEquals ;
30
32
34
36
import org .asynchttpclient .BoundRequestBuilder ;
35
37
import org .asynchttpclient .HttpResponseStatus ;
36
38
import org .asynchttpclient .Response ;
39
+ import org .asynchttpclient .extras .rxjava .UnsubscribedException ;
37
40
import org .asynchttpclient .handler .ProgressAsyncHandler ;
38
41
import org .mockito .InOrder ;
39
42
import org .testng .annotations .Test ;
40
43
41
44
import java .util .Arrays ;
42
45
import java .util .List ;
46
+ import java .util .concurrent .Future ;
43
47
import java .util .concurrent .TimeUnit ;
48
+ import java .util .concurrent .atomic .AtomicReference ;
44
49
45
50
import rx .Single ;
46
51
import rx .exceptions .CompositeException ;
@@ -82,6 +87,8 @@ public void testSuccessfulCompletion() throws Exception {
82
87
} catch (final Throwable t ) {
83
88
bridge .onThrowable (t );
84
89
}
90
+
91
+ return mock (Future .class );
85
92
} , () -> handler );
86
93
87
94
final TestSubscriber <Object > subscriber = new TestSubscriber <>();
@@ -132,6 +139,8 @@ public void testSuccessfulCompletionWithProgress() throws Exception {
132
139
} catch (final Throwable t ) {
133
140
bridge .onThrowable (t );
134
141
}
142
+
143
+ return mock (Future .class );
135
144
} , () -> handler );
136
145
137
146
final TestSubscriber <Object > subscriber = new TestSubscriber <>();
@@ -186,6 +195,8 @@ public void testErrorPropagation() throws Exception {
186
195
} catch (final Throwable t ) {
187
196
bridge .onThrowable (t );
188
197
}
198
+
199
+ return mock (Future .class );
189
200
} , () -> handler );
190
201
191
202
final TestSubscriber <Object > subscriber = new TestSubscriber <>();
@@ -209,6 +220,7 @@ public void testErrorInOnCompletedPropagation() throws Exception {
209
220
final Single <?> underTest = AsyncHttpSingle .create (bridge -> {
210
221
try {
211
222
bridge .onCompleted ();
223
+ return mock (Future .class );
212
224
} catch (final Throwable t ) {
213
225
throw new AssertionError (t );
214
226
}
@@ -237,6 +249,7 @@ public void testErrorInOnThrowablePropagation() throws Exception {
237
249
final Single <?> underTest = AsyncHttpSingle .create (bridge -> {
238
250
try {
239
251
bridge .onThrowable (processingException );
252
+ return mock (Future .class );
240
253
} catch (final Throwable t ) {
241
254
throw new AssertionError (t );
242
255
}
@@ -281,4 +294,23 @@ public State onStatusReceived(HttpResponseStatus status) {
281
294
subscriber .assertValue (null );
282
295
}
283
296
297
+ @ Test (groups = "standalone" )
298
+ public void testUnsubscribe () throws Exception {
299
+ final AsyncHandler <Object > handler = mock (AsyncHandler .class );
300
+ final Future <?> future = mock (Future .class );
301
+ final AtomicReference <AsyncHandler <?>> bridgeRef = new AtomicReference <>();
302
+
303
+ final Single <?> underTest = AsyncHttpSingle .create (bridge -> {
304
+ bridgeRef .set (bridge );
305
+ return future ;
306
+ } , () -> handler );
307
+
308
+ underTest .subscribe ().unsubscribe ();
309
+ verify (future ).cancel (true );
310
+ verifyZeroInteractions (handler );
311
+
312
+ assertThat (bridgeRef .get ().onStatusReceived (null ), is (AsyncHandler .State .ABORT ));
313
+ verify (handler ).onThrowable (isA (UnsubscribedException .class ));
314
+ verifyNoMoreInteractions (handler );
315
+ }
284
316
}
0 commit comments