@Test
public void testDebounceWithCompleted() {

    Observable<String> source = Observable.create(new Observable.OnSubscribeFunc<String>() {
        @Override
        public Subscription onSubscribe(Observer<? super String> observer) {
            publishNext(observer, 100, "one");    // Should be skipped since "two" will arrive before the timeout expires.
            publishNext(observer, 400, "two");    // Should be published since "three" will arrive after the timeout expires.
            publishNext(observer, 900, "three");   // Should be skipped since onCompleted will arrive before the timeout expires.
            publishCompleted(observer, 1000);     // Should be published as soon as the timeout expires.
            return Subscriptions.empty();
        }
    });
    Observable<String> sampled = Observable.create(OperationDebounce.debounce(source, 400, TimeUnit.MILLISECONDS, scheduler));
    sampled.subscribe(observer);
    scheduler.advanceTimeTo(0, TimeUnit.MILLISECONDS);
    InOrder inOrder = inOrder(observer);
    // must go to 800 since it must be 400 after when two is sent, which is at 400
    scheduler.advanceTimeTo(800, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext("two");
    scheduler.advanceTimeTo(1000, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onCompleted();
    inOrder.verifyNoMoreInteractions();
}