Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

additional buffer tests #761

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions rxjava-core/src/test/java/rx/operators/OperationBufferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -516,4 +517,146 @@ public void bufferWithBOBoundaryThrows() {
verify(o, never()).onCompleted();
verify(o, never()).onNext(any());
}

@Test
public void bufferTimeSpanCountEmitAllItems() throws InterruptedException {

final int maxRounds = 100;
final int maxCount = 1000;

final int bufferCount = 42;
final int bufferTimeSpan = 42; // µs

int round = 0;
final AtomicInteger count = new AtomicInteger(0);
do {
++round;
count.set(0);

Observable<List<Integer>> observable = Observable //
.range(1, maxCount) //
.buffer(bufferTimeSpan, TimeUnit.MICROSECONDS, bufferCount);

final CountDownLatch lock = new CountDownLatch(1);

observable.subscribe( // onNext
new Action1<List<Integer>>() {
@Override
public void call(List<Integer> val) {
count.addAndGet(val.size());
}
}, // onError
new Action1<Throwable>() {
@Override
public void call(Throwable err) {
lock.countDown();
}
}, // onComplete
new Action0() {
@Override
public void call() {
lock.countDown();
}
}
);
lock.await();
} while ((maxCount == count.get()) && (round < maxRounds));

assertEquals(maxCount, count.get());
assertEquals(maxRounds, round);
}

@Test
public void bufferTimeSpanEmitAllItems() throws InterruptedException {

final int maxRounds = 100;
final int maxCount = 1000;

final int bufferTimeSpan = 42; // µs

int round = 0;
final AtomicInteger count = new AtomicInteger(0);
do {
++round;
count.set(0);

Observable<List<Integer>> observable = Observable //
.range(1, maxCount) //
.buffer(bufferTimeSpan, TimeUnit.MICROSECONDS);

final CountDownLatch lock = new CountDownLatch(1);

observable.subscribe( // onNext
new Action1<List<Integer>>() {
@Override
public void call(List<Integer> val) {
count.addAndGet(val.size());
}
}, // onError
new Action1<Throwable>() {
@Override
public void call(Throwable err) {
lock.countDown();
}
}, // onComplete
new Action0() {
@Override
public void call() {
lock.countDown();
}
}
);
lock.await();
} while ((maxCount == count.get()) && (round < maxRounds));

assertEquals(maxCount, count.get());
assertEquals(maxRounds, round);
}

@Test
public void bufferCountEmitAllItems() throws InterruptedException {

final int maxRounds = 100;
final int maxCount = 1000;

final int bufferCount = 42;

int round = 0;
final AtomicInteger count = new AtomicInteger(0);
do {
++round;
count.set(0);

Observable<List<Integer>> observable = Observable //
.range(1, maxCount) //
.buffer(bufferCount);

final CountDownLatch lock = new CountDownLatch(1);

observable.subscribe( // onNext
new Action1<List<Integer>>() {
@Override
public void call(List<Integer> val) {
count.addAndGet(val.size());
}
}, // onError
new Action1<Throwable>() {
@Override
public void call(Throwable err) {
lock.countDown();
}
}, // onComplete
new Action0() {
@Override
public void call() {
lock.countDown();
}
}
);
lock.await();
} while ((maxCount == count.get()) && (round < maxRounds));

assertEquals(maxCount, count.get());
assertEquals(maxRounds, round);
}
}