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

Bug with "Sample" operator in 1.2.4 #4952

Closed
kidinov opened this issue Jan 3, 2017 · 4 comments
Closed

Bug with "Sample" operator in 1.2.4 #4952

kidinov opened this issue Jan 3, 2017 · 4 comments

Comments

@kidinov
Copy link

kidinov commented Jan 3, 2017

Library version 1.2.4.

Bug reporting inspired by that SO question.

Code sample to reproduce:

Observable<String> names = Observable
        .just("Mary", "Patricia", "Linda", "Barbara",
                "Elizabeth", "Jennifer", "Maria", "Susan",
                "Margaret", "Dorothy");
Observable<Long> absoluteDelayMillis = Observable
        .just(0.1, 0.6, 0.9, 1.1,
                3.3, 3.4, 3.5, 3.6,
                4.4, 4.8)
        .map(d -> (long) (d * 1_000));
Observable<String> delayedNames = names
        .zipWith(absoluteDelayMillis,
                (n, d) -> Observable
                        .just(n)
                        .delay(d, TimeUnit.MILLISECONDS))
        .flatMap(o -> o);
delayedNames
        .sample(1, SECONDS)
        .subscribe(System.out::println);

Output:

Linda
Barbara
Susan
Dorothy

But, as far I understand that operator, it should be:

Linda
Barbara
Susan

Because "tick" on 5000 should not happen after onComplete called.

@kidinov kidinov changed the title Bug with "Sample" in 1.2.4 Bug with "Sample" operator in 1.2.4 Jan 3, 2017
@akarnokd
Copy link
Member

akarnokd commented Jan 3, 2017

This was a requested feature that made it into 1.1.3: Discussion, PR.

The common documentation of sample also indicates emission of the very last event in case of the termination. The RxJava diagram hasn't been updated it seems.

In 2.x, the last element in the internal buffer is not emitted in any of the sample variants.

@akarnokd
Copy link
Member

akarnokd commented Jan 3, 2017

I can add an overload scan(long, TimeUnit, Scheduler, boolean) to emit the very last unsampled value before completing, but it won't get into 2.0.4 as I need to update 2 x 2 operators, plus there is a peculiar race between onComplete() and the sampling run() that needs to be addressed.

@akarnokd
Copy link
Member

See #4955 for 2.x and #5007 for 1.x.

@akarnokd
Copy link
Member

Closing via #5007.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants