Skip to content

Commit f74504f

Browse files
authored
2.x: Unify race test loop counts and invocations (ReactiveX#5857)
1 parent 12c0e30 commit f74504f

File tree

126 files changed

+580
-623
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+580
-623
lines changed

src/test/java/io/reactivex/TestHelper.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@
5050
*/
5151
public enum TestHelper {
5252
;
53+
54+
/**
55+
* Number of times to loop a {@link #race(Runnable, Runnable)} invocation
56+
* by default.
57+
*/
58+
public static final int RACE_DEFAULT_LOOPS = 2500;
59+
60+
/**
61+
* Number of times to loop a {@link #race(Runnable, Runnable)} invocation
62+
* in tests with race conditions requiring more runs to check.
63+
*/
64+
public static final int RACE_LONG_LOOPS = 10000;
65+
5366
/**
5467
* Mocks a subscriber and prepares it to request Long.MAX_VALUE.
5568
* @param <T> the value type
@@ -344,6 +357,8 @@ public void onComplete() {
344357
* <p>The method blocks until both have run to completion.
345358
* @param r1 the first runnable
346359
* @param r2 the second runnable
360+
* @see #RACE_DEFAULT_LOOPS
361+
* @see #RACE_LONG_LOOPS
347362
*/
348363
public static void race(final Runnable r1, final Runnable r2) {
349364
race(r1, r2, Schedulers.single());
@@ -355,6 +370,8 @@ public static void race(final Runnable r1, final Runnable r2) {
355370
* @param r1 the first runnable
356371
* @param r2 the second runnable
357372
* @param s the scheduler to use
373+
* @see #RACE_DEFAULT_LOOPS
374+
* @see #RACE_LONG_LOOPS
358375
*/
359376
public static void race(final Runnable r1, final Runnable r2, Scheduler s) {
360377
final AtomicInteger count = new AtomicInteger(2);

src/test/java/io/reactivex/disposables/CompositeDisposableTest.java

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import io.reactivex.TestHelper;
2626
import io.reactivex.exceptions.CompositeException;
2727
import io.reactivex.functions.Action;
28-
import io.reactivex.schedulers.Schedulers;
2928

3029
public class CompositeDisposableTest {
3130

@@ -443,7 +442,7 @@ public void delete() {
443442

444443
@Test
445444
public void disposeRace() {
446-
for (int i = 0; i < 500; i++) {
445+
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
447446
final CompositeDisposable cd = new CompositeDisposable();
448447

449448
Runnable run = new Runnable() {
@@ -453,13 +452,13 @@ public void run() {
453452
}
454453
};
455454

456-
TestHelper.race(run, run, Schedulers.io());
455+
TestHelper.race(run, run);
457456
}
458457
}
459458

460459
@Test
461460
public void addRace() {
462-
for (int i = 0; i < 500; i++) {
461+
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
463462
final CompositeDisposable cd = new CompositeDisposable();
464463

465464
Runnable run = new Runnable() {
@@ -469,13 +468,13 @@ public void run() {
469468
}
470469
};
471470

472-
TestHelper.race(run, run, Schedulers.io());
471+
TestHelper.race(run, run);
473472
}
474473
}
475474

476475
@Test
477476
public void addAllRace() {
478-
for (int i = 0; i < 500; i++) {
477+
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
479478
final CompositeDisposable cd = new CompositeDisposable();
480479

481480
Runnable run = new Runnable() {
@@ -485,13 +484,13 @@ public void run() {
485484
}
486485
};
487486

488-
TestHelper.race(run, run, Schedulers.io());
487+
TestHelper.race(run, run);
489488
}
490489
}
491490

492491
@Test
493492
public void removeRace() {
494-
for (int i = 0; i < 500; i++) {
493+
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
495494
final CompositeDisposable cd = new CompositeDisposable();
496495

497496
final Disposable d1 = Disposables.empty();
@@ -505,13 +504,13 @@ public void run() {
505504
}
506505
};
507506

508-
TestHelper.race(run, run, Schedulers.io());
507+
TestHelper.race(run, run);
509508
}
510509
}
511510

512511
@Test
513512
public void deleteRace() {
514-
for (int i = 0; i < 500; i++) {
513+
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
515514
final CompositeDisposable cd = new CompositeDisposable();
516515

517516
final Disposable d1 = Disposables.empty();
@@ -525,13 +524,13 @@ public void run() {
525524
}
526525
};
527526

528-
TestHelper.race(run, run, Schedulers.io());
527+
TestHelper.race(run, run);
529528
}
530529
}
531530

532531
@Test
533532
public void clearRace() {
534-
for (int i = 0; i < 500; i++) {
533+
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
535534
final CompositeDisposable cd = new CompositeDisposable();
536535

537536
final Disposable d1 = Disposables.empty();
@@ -545,13 +544,13 @@ public void run() {
545544
}
546545
};
547546

548-
TestHelper.race(run, run, Schedulers.io());
547+
TestHelper.race(run, run);
549548
}
550549
}
551550

552551
@Test
553552
public void addDisposeRace() {
554-
for (int i = 0; i < 500; i++) {
553+
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
555554
final CompositeDisposable cd = new CompositeDisposable();
556555

557556
Runnable run = new Runnable() {
@@ -568,13 +567,13 @@ public void run() {
568567
}
569568
};
570569

571-
TestHelper.race(run, run2, Schedulers.io());
570+
TestHelper.race(run, run2);
572571
}
573572
}
574573

575574
@Test
576575
public void addAllDisposeRace() {
577-
for (int i = 0; i < 500; i++) {
576+
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
578577
final CompositeDisposable cd = new CompositeDisposable();
579578

580579
Runnable run = new Runnable() {
@@ -591,13 +590,13 @@ public void run() {
591590
}
592591
};
593592

594-
TestHelper.race(run, run2, Schedulers.io());
593+
TestHelper.race(run, run2);
595594
}
596595
}
597596

598597
@Test
599598
public void removeDisposeRace() {
600-
for (int i = 0; i < 500; i++) {
599+
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
601600
final CompositeDisposable cd = new CompositeDisposable();
602601

603602
final Disposable d1 = Disposables.empty();
@@ -618,13 +617,13 @@ public void run() {
618617
}
619618
};
620619

621-
TestHelper.race(run, run2, Schedulers.io());
620+
TestHelper.race(run, run2);
622621
}
623622
}
624623

625624
@Test
626625
public void deleteDisposeRace() {
627-
for (int i = 0; i < 500; i++) {
626+
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
628627
final CompositeDisposable cd = new CompositeDisposable();
629628

630629
final Disposable d1 = Disposables.empty();
@@ -645,13 +644,13 @@ public void run() {
645644
}
646645
};
647646

648-
TestHelper.race(run, run2, Schedulers.io());
647+
TestHelper.race(run, run2);
649648
}
650649
}
651650

652651
@Test
653652
public void clearDisposeRace() {
654-
for (int i = 0; i < 500; i++) {
653+
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
655654
final CompositeDisposable cd = new CompositeDisposable();
656655

657656
final Disposable d1 = Disposables.empty();
@@ -672,13 +671,13 @@ public void run() {
672671
}
673672
};
674673

675-
TestHelper.race(run, run2, Schedulers.io());
674+
TestHelper.race(run, run2);
676675
}
677676
}
678677

679678
@Test
680679
public void sizeDisposeRace() {
681-
for (int i = 0; i < 500; i++) {
680+
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
682681
final CompositeDisposable cd = new CompositeDisposable();
683682

684683
final Disposable d1 = Disposables.empty();
@@ -699,7 +698,7 @@ public void run() {
699698
}
700699
};
701700

702-
TestHelper.race(run, run2, Schedulers.io());
701+
TestHelper.race(run, run2);
703702
}
704703
}
705704

src/test/java/io/reactivex/disposables/DisposablesTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package io.reactivex.disposables;
1515

1616
import static org.junit.Assert.*;
17+
import static org.mockito.ArgumentMatchers.anyInt;
1718
import static org.mockito.Mockito.*;
1819

1920
import java.io.IOException;
@@ -27,7 +28,6 @@
2728
import io.reactivex.functions.Action;
2829
import io.reactivex.internal.disposables.DisposableHelper;
2930
import io.reactivex.plugins.RxJavaPlugins;
30-
import io.reactivex.schedulers.Schedulers;
3131

3232
public class DisposablesTest {
3333

@@ -123,7 +123,7 @@ public void run() throws Exception {
123123

124124
@Test
125125
public void disposeRace() {
126-
for (int i = 0; i < 100; i++) {
126+
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
127127
final Disposable d = Disposables.empty();
128128

129129
Runnable r = new Runnable() {
@@ -133,7 +133,7 @@ public void run() {
133133
}
134134
};
135135

136-
TestHelper.race(r, r, Schedulers.io());
136+
TestHelper.race(r, r);
137137
}
138138
}
139139

src/test/java/io/reactivex/internal/disposables/ArrayCompositeDisposableTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import io.reactivex.TestHelper;
2121
import io.reactivex.disposables.*;
22-
import io.reactivex.schedulers.Schedulers;
2322

2423
public class ArrayCompositeDisposableTest {
2524

@@ -70,7 +69,7 @@ public void normal() {
7069

7170
@Test
7271
public void disposeRace() {
73-
for (int i = 0; i < 100; i++) {
72+
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
7473
final ArrayCompositeDisposable acd = new ArrayCompositeDisposable(2);
7574

7675
Runnable r = new Runnable() {
@@ -80,13 +79,13 @@ public void run() {
8079
}
8180
};
8281

83-
TestHelper.race(r, r, Schedulers.io());
82+
TestHelper.race(r, r);
8483
}
8584
}
8685

8786
@Test
8887
public void replaceRace() {
89-
for (int i = 0; i < 100; i++) {
88+
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
9089
final ArrayCompositeDisposable acd = new ArrayCompositeDisposable(2);
9190

9291
Runnable r = new Runnable() {
@@ -96,13 +95,13 @@ public void run() {
9695
}
9796
};
9897

99-
TestHelper.race(r, r, Schedulers.io());
98+
TestHelper.race(r, r);
10099
}
101100
}
102101

103102
@Test
104103
public void setRace() {
105-
for (int i = 0; i < 100; i++) {
104+
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
106105
final ArrayCompositeDisposable acd = new ArrayCompositeDisposable(2);
107106

108107
Runnable r = new Runnable() {
@@ -112,7 +111,7 @@ public void run() {
112111
}
113112
};
114113

115-
TestHelper.race(r, r, Schedulers.io());
114+
TestHelper.race(r, r);
116115
}
117116
}
118117

src/test/java/io/reactivex/internal/disposables/CancellableDisposableTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import io.reactivex.exceptions.TestException;
2525
import io.reactivex.functions.Cancellable;
2626
import io.reactivex.plugins.RxJavaPlugins;
27-
import io.reactivex.schedulers.Schedulers;
2827

2928
public class CancellableDisposableTest {
3029

@@ -84,7 +83,7 @@ public void cancel() throws Exception {
8483
@Test
8584
public void disposeRace() {
8685

87-
for (int i = 0; i < 100; i++) {
86+
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
8887
final AtomicInteger count = new AtomicInteger();
8988

9089
Cancellable c = new Cancellable() {
@@ -103,7 +102,7 @@ public void run() {
103102
}
104103
};
105104

106-
TestHelper.race(r, r, Schedulers.io());
105+
TestHelper.race(r, r);
107106

108107
assertEquals(1, count.get());
109108
}

0 commit comments

Comments
 (0)