15
15
*/
16
16
package rx .subscriptions ;
17
17
18
- import static org .junit .Assert .assertEquals ;
19
- import static org .junit .Assert .assertFalse ;
20
- import static org .junit .Assert .assertTrue ;
21
- import static org .junit .Assert .fail ;
18
+ import org .junit .Test ;
19
+ import rx .Subscription ;
20
+ import rx .exceptions .CompositeException ;
22
21
23
22
import java .util .ArrayList ;
24
23
import java .util .List ;
25
24
import java .util .concurrent .CountDownLatch ;
26
25
import java .util .concurrent .atomic .AtomicInteger ;
27
26
28
- import org .junit .Test ;
29
-
30
- import rx . Subscription ;
31
- import rx . exceptions . CompositeException ;
27
+ import static org .junit .Assert . assertEquals ;
28
+ import static org . junit . Assert . assertFalse ;
29
+ import static org . junit . Assert . assertTrue ;
30
+ import static org . junit . Assert . fail ;
32
31
33
32
public class CompositeSubscriptionTest {
34
33
@@ -324,17 +323,18 @@ public void run() {
324
323
// we should have only unsubscribed once
325
324
assertEquals (1 , counter .get ());
326
325
}
326
+
327
327
@ Test
328
328
public void testTryRemoveIfNotIn () {
329
329
CompositeSubscription csub = new CompositeSubscription ();
330
-
330
+
331
331
CompositeSubscription csub1 = new CompositeSubscription ();
332
332
CompositeSubscription csub2 = new CompositeSubscription ();
333
-
333
+
334
334
csub .add (csub1 );
335
335
csub .remove (csub1 );
336
336
csub .add (csub2 );
337
-
337
+
338
338
csub .remove (csub1 ); // try removing again
339
339
}
340
340
@@ -344,4 +344,121 @@ public void testAddingNullSubscriptionIllegal() {
344
344
csub .add (null );
345
345
}
346
346
347
+ @ Test
348
+ public void testAddAll () {
349
+ final AtomicInteger counter = new AtomicInteger ();
350
+ CompositeSubscription s = new CompositeSubscription ();
351
+ s .addAll (new Subscription () {
352
+ @ Override
353
+ public void unsubscribe () {
354
+ counter .incrementAndGet ();
355
+ }
356
+
357
+ @ Override
358
+ public boolean isUnsubscribed () {
359
+ return false ;
360
+ }
361
+ }, new Subscription () {
362
+ @ Override
363
+ public void unsubscribe () {
364
+ counter .incrementAndGet ();
365
+ }
366
+
367
+ @ Override
368
+ public boolean isUnsubscribed () {
369
+ return false ;
370
+ }
371
+ }, new Subscription () {
372
+ @ Override
373
+ public void unsubscribe () {
374
+ counter .incrementAndGet ();
375
+ }
376
+
377
+ @ Override
378
+ public boolean isUnsubscribed () {
379
+ return false ;
380
+ }
381
+ }, new Subscription () {
382
+ @ Override
383
+ public void unsubscribe () {
384
+ counter .incrementAndGet ();
385
+ }
386
+
387
+ @ Override
388
+ public boolean isUnsubscribed () {
389
+ return false ;
390
+ }
391
+ });
392
+
393
+ s .unsubscribe ();
394
+
395
+ assertEquals (4 , counter .get ());
396
+ }
397
+
398
+ @ Test (timeout = 1000 )
399
+ public void testAddAllConcurrent () throws InterruptedException {
400
+ final AtomicInteger counter = new AtomicInteger ();
401
+ final CompositeSubscription s = new CompositeSubscription ();
402
+
403
+ final int count = 10 ;
404
+ final CountDownLatch start = new CountDownLatch (1 );
405
+ final CountDownLatch end = new CountDownLatch (10 );
406
+ final List <Thread > threads = new ArrayList <Thread >();
407
+ for (int i = 0 ; i < count ; i ++) {
408
+ final Thread t = new Thread () {
409
+ @ Override
410
+ public void run () {
411
+ try {
412
+ start .await ();
413
+ s .addAll (new Subscription () {
414
+ @ Override
415
+ public void unsubscribe () {
416
+ counter .incrementAndGet ();
417
+ }
418
+
419
+ @ Override
420
+ public boolean isUnsubscribed () {
421
+ return false ;
422
+ }
423
+ }, new Subscription () {
424
+ @ Override
425
+ public void unsubscribe () {
426
+ counter .incrementAndGet ();
427
+ }
428
+
429
+ @ Override
430
+ public boolean isUnsubscribed () {
431
+ return false ;
432
+ }
433
+ }, new Subscription () {
434
+ @ Override
435
+ public void unsubscribe () {
436
+ counter .incrementAndGet ();
437
+ }
438
+
439
+ @ Override
440
+ public boolean isUnsubscribed () {
441
+ return false ;
442
+ }
443
+ });
444
+ end .countDown ();
445
+ } catch (final InterruptedException e ) {
446
+ fail (e .getMessage ());
447
+ }
448
+ }
449
+ };
450
+ t .start ();
451
+ threads .add (t );
452
+ }
453
+
454
+ start .countDown ();
455
+ end .await ();
456
+ s .unsubscribe ();
457
+ for (final Thread t : threads ) {
458
+ t .join ();
459
+ }
460
+
461
+ assertEquals (30 , counter .get ());
462
+ }
463
+
347
464
}
0 commit comments