Skip to content

Instantly share code, notes, and snippets.

@SeongUgJung
Last active August 29, 2015 14:26
Show Gist options
  • Save SeongUgJung/c653d482f1d36c373cc8 to your computer and use it in GitHub Desktop.
Save SeongUgJung/c653d482f1d36c373cc8 to your computer and use it in GitHub Desktop.
publishsubject + flatmap problem
List<ChatChooseItem> chatChooseItems = getMockItems();
List<ChatChooseItem> testList = new ArrayList<>();
PublishSubject<String> publishSubject = PublishSubject.create();
publishSubject
.throttleWithTimeout(300, TimeUnit.MILLISECONDS)
.flatMap(s -> Observable.from(chatChooseItems)
.filter(chatChooseItem -> chatChooseItem.getName().toLowerCase().contains(s.toLowerCase()))
.toSortedList((lhs, rhs) -> lhs.getName().toLowerCase().compareTo(rhs.getName().toLowerCase()))
)
.subscribe(testList::addAll);
publishSubject.onNext("he");
Awaitility.await().until(() -> testList.size() > 0);
MatcherAssert.assertThat(testList.size(), CoreMatchers.is(IsEqual.equalTo(2)));
List<ChatChooseItem> chatChooseItems = getMockItems();
List<ChatChooseItem> testList = new ArrayList<>();
PublishSubject<String> publishSubject = PublishSubject.create();
publishSubject
.throttleWithTimeout(300, TimeUnit.MILLISECONDS)
.flatMap(s -> Observable.from(chatChooseItems)
.filter(chatChooseItem -> chatChooseItem.getName().toLowerCase()
.contains(s.toLowerCase())))
.toSortedList((lhs, rhs) -> lhs.getName().toLowerCase().compareTo(rhs.getName().toLowerCase()))
.subscribe(testList::addAll);
publishSubject.onNext("he");
Awaitility.await().until(() -> testList.size() > 0);
MatcherAssert.assertThat(testList.size(), CoreMatchers.is(IsNull.notNullValue()));
@SeongUgJung
Copy link
Author

it would be assertion fail.

@SeongUgJung
Copy link
Author

I think toSortedList of PublishSubject need to onComplete.

so If you combine with flatMap & sort, then toSortedList is in flatMap

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