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

2.x: Fix takeLast(time) last events time window calculation. #6653

Merged
merged 1 commit into from
Sep 13, 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
11 changes: 8 additions & 3 deletions src/test/java/io/reactivex/TimesteppingScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

@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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,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 @@ -275,4 +275,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);
}
}