Skip to content

Instantly share code, notes, and snippets.

@drhr
Last active October 6, 2016 22:23
Show Gist options
  • Save drhr/e6826772512836eb51a45c2682550d3e to your computer and use it in GitHub Desktop.
Save drhr/e6826772512836eb51a45c2682550d3e to your computer and use it in GitHub Desktop.
A test for "BufferDebounce" to confirm how bursts are captured given particular timings
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.util.List;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.observers.TestSubscriber;
import rx.schedulers.TestScheduler;
import rx.subjects.PublishSubject;
import rx.subjects.Subject;
@RunWith(JUnit4.class)
public class BufferDebounceTest {
Subject<Integer, Integer> bursty;
@Before
public void setup() {
bursty = PublishSubject.create();
}
@Test
public void test() {
// given
TestScheduler testScheduler = new TestScheduler();
Observable<Integer> burstyMulticast = bursty.publish().refCount();
Observable<Integer> burstyDebounced = burstyMulticast.debounce(100, TimeUnit.MILLISECONDS, testScheduler);
Observable<List<Integer>> burstyBuffered = burstyMulticast.buffer(burstyDebounced).filter(next -> next.size() >= 3);
// when
TestSubscriber<List<Integer>> testSubscriber = new TestSubscriber<>();
burstyBuffered.subscribe(testSubscriber);
// then
bursty.onNext(1);
bursty.onNext(1);
bursty.onNext(1);
bursty.onNext(1);
testScheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS);
testSubscriber.assertValueCount(1);
// then
bursty.onNext(1);
bursty.onNext(1);
bursty.onNext(1);
testScheduler.advanceTimeBy(99, TimeUnit.MILLISECONDS);
bursty.onNext(1);
testScheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS);
testSubscriber.assertValueCount(2);
// then
bursty.onNext(1);
bursty.onNext(1);
testScheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS);
testSubscriber.assertValueCount(2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment