Skip to content

3.x: Fix takeLast(time) last events time window calculation. #6648

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

Merged
merged 1 commit into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ void drain() {
final Observer<? super T> a = downstream;
final SpscLinkedArrayQueue<Object> q = queue;
final boolean delayError = this.delayError;
final long timestampLimit = scheduler.now(unit) - time;

for (;;) {
if (cancelled) {
Expand Down Expand Up @@ -171,7 +172,7 @@ void drain() {
@SuppressWarnings("unchecked")
T o = (T)q.poll();

if ((Long)ts < scheduler.now(unit) - time) {
if ((Long)ts < timestampLimit) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import io.reactivex.rxjava3.processors.PublishProcessor;
import io.reactivex.rxjava3.schedulers.*;
import io.reactivex.rxjava3.subscribers.TestSubscriber;
import io.reactivex.rxjava3.testsupport.TestHelper;
import io.reactivex.rxjava3.testsupport.*;

public class FlowableTakeLastTimedTest extends RxJavaTest {

Expand Down Expand Up @@ -338,4 +338,27 @@ public Publisher<Object> apply(Flowable<Object> f) throws Exception {
public void badRequest() {
TestHelper.assertBadRequestReported(PublishProcessor.create().takeLast(1, TimeUnit.SECONDS));
}

@Test
public void lastWindowIsFixedInTime() {
TimesteppingScheduler scheduler = new TimesteppingScheduler();
scheduler.stepEnabled = false;

PublishProcessor<Integer> pp = PublishProcessor.create();

TestSubscriber<Integer> ts = pp
.takeLast(2, TimeUnit.SECONDS, scheduler)
.test();

pp.onNext(1);
pp.onNext(2);
pp.onNext(3);
pp.onNext(4);

scheduler.stepEnabled = true;

pp.onComplete();

ts.assertResult(1, 2, 3, 4);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import io.reactivex.rxjava3.observers.TestObserver;
import io.reactivex.rxjava3.schedulers.*;
import io.reactivex.rxjava3.subjects.PublishSubject;
import io.reactivex.rxjava3.testsupport.TestHelper;
import io.reactivex.rxjava3.testsupport.*;

public class ObservableTakeLastTimedTest extends RxJavaTest {

Expand Down Expand Up @@ -277,4 +277,27 @@ public void run() {
TestHelper.race(r1, r2);
}
}

@Test
public void lastWindowIsFixedInTime() {
TimesteppingScheduler scheduler = new TimesteppingScheduler();
scheduler.stepEnabled = false;

PublishSubject<Integer> ps = PublishSubject.create();

TestObserver<Integer> to = ps
.takeLast(2, TimeUnit.SECONDS, scheduler)
.test();

ps.onNext(1);
ps.onNext(2);
ps.onNext(3);
ps.onNext(4);

scheduler.stepEnabled = true;

ps.onComplete();

to.assertResult(1, 2, 3, 4);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* Basic scheduler that produces an ever increasing {@link #now(TimeUnit)} value.
* Use this scheduler only as a time source!
*/
public class TimesteppingScheduler extends Scheduler {
public final class TimesteppingScheduler extends Scheduler {

final class TimesteppingWorker extends Worker {
@Override
Expand All @@ -42,11 +42,13 @@ public Disposable schedule(Runnable run, long delay, TimeUnit unit) {

@Override
public long now(TimeUnit unit) {
return time++;
return TimesteppingScheduler.this.now(unit);
}
}

long time;
public long time;

public boolean stepEnabled = true;

@Override
public Worker createWorker() {
Expand All @@ -55,6 +57,9 @@ public Worker createWorker() {

@Override
public long now(TimeUnit unit) {
return time++;
if (stepEnabled) {
return time++;
}
return time;
}
}