Skip to content

Commit a5d885d

Browse files
Fix non-deterministic test
1 parent 089794c commit a5d885d

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

rxjava-core/src/test/java/rx/internal/util/RxRingBufferWithoutUnsafeTest.java

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ protected RxRingBuffer createRingBuffer() {
3939
/**
4040
* Single producer, 2 consumers. The request() ensures it gets scheduled back on the same Producer thread.
4141
*/
42-
@Test(timeout = 2000)
42+
@Test
4343
public void testConcurrency() throws InterruptedException {
4444
final RxRingBuffer b = createRingBuffer();
45-
final CountDownLatch latch = new CountDownLatch(255);
45+
final CountDownLatch emitLatch = new CountDownLatch(255);
46+
final CountDownLatch drainLatch = new CountDownLatch(2);
4647

4748
final Scheduler.Worker w1 = Schedulers.newThread().createWorker();
4849
Scheduler.Worker w2 = Schedulers.newThread().createWorker();
@@ -58,33 +59,32 @@ public void testConcurrency() throws InterruptedException {
5859

5960
@Override
6061
public void request(final long n) {
61-
System.out.println("request[" + c.incrementAndGet() + "]: " + n + " Thread: " + Thread.currentThread());
62+
// System.out.println("request[" + c.incrementAndGet() + "]: " + n + " Thread: " + Thread.currentThread());
6263
w1.schedule(new Action0() {
6364

6465
@Override
6566
public void call() {
66-
if (latch.getCount() == 0) {
67+
if (emitLatch.getCount() == 0) {
6768
return;
6869
}
6970
for (int i = 0; i < n; i++) {
7071
try {
71-
emit.incrementAndGet();
7272
b.onNext("one");
73+
emit.incrementAndGet();
7374
} catch (MissingBackpressureException e) {
7475
System.out.println("BackpressureException => item: " + i + " requested: " + n + " emit: " + emit.get() + " poll: " + poll.get());
7576
backpressureExceptions.incrementAndGet();
7677
}
7778
}
7879
// we'll release after n batches
79-
latch.countDown();
80+
emitLatch.countDown();
8081
}
8182

8283
});
8384
}
8485

8586
};
8687
final TestSubscriber<String> ts = new TestSubscriber<String>();
87-
8888
w1.schedule(new Action0() {
8989

9090
@Override
@@ -95,7 +95,7 @@ public void call() {
9595

9696
});
9797

98-
w2.schedule(new Action0() {
98+
Action0 drainer = new Action0() {
9999

100100
@Override
101101
public void call() {
@@ -109,39 +109,30 @@ public void call() {
109109
if (emitted > 0) {
110110
ts.requestMore(emitted);
111111
emitted = 0;
112+
} else {
113+
if (emitLatch.getCount() == 0) {
114+
// this works with SynchronizedQueue, if changing to a non-blocking Queue
115+
// then this will likely need to change like the SpmcTest version
116+
drainLatch.countDown();
117+
return;
118+
}
112119
}
113120
}
114121
}
115122

116123
}
117124

118-
});
119-
120-
w3.schedule(new Action0() {
125+
};
121126

122-
@Override
123-
public void call() {
124-
int emitted = 0;
125-
while (true) {
126-
Object o = b.poll();
127-
if (o != null) {
128-
emitted++;
129-
poll.incrementAndGet();
130-
} else {
131-
if (emitted > 0) {
132-
ts.requestMore(emitted);
133-
emitted = 0;
134-
}
135-
}
136-
}
137-
}
127+
w2.schedule(drainer);
128+
w3.schedule(drainer);
138129

139-
});
130+
emitLatch.await();
131+
drainLatch.await();
140132

141-
latch.await();
142-
w1.unsubscribe();
143133
w2.unsubscribe();
144134
w3.unsubscribe();
135+
w1.unsubscribe(); // put this one last as unsubscribing from it can cause Exceptions to be throw in w2/w3
145136

146137
System.out.println("emit: " + emit.get() + " poll: " + poll.get());
147138
assertEquals(0, backpressureExceptions.get());

0 commit comments

Comments
 (0)