Java Code Examples for rx.subjects.BehaviorSubject#onNext()
The following examples show how to use
rx.subjects.BehaviorSubject#onNext() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChoiceOfTwoLoadBalancerTest.java From ocelli with Apache License 2.0 | 6 votes |
@Test public void testMany() { BehaviorSubject<List<Integer>> source = BehaviorSubject.create(); LoadBalancer<Integer> lb = LoadBalancer.fromSnapshotSource(source).build(ChoiceOfTwoLoadBalancer.create(COMPARATOR)); source.onNext(Lists.newArrayList(0,1,2,3,4,5,6,7,8,9)); AtomicIntegerArray counts = new AtomicIntegerArray(10); for (int i = 0; i < 100000; i++) { counts.incrementAndGet(lb.next()); } Double[] pct = new Double[counts.length()]; for (int i = 0; i < counts.length(); i++) { pct[i] = counts.get(i)/100000.0; } for (int i = 1; i < counts.length(); i++) { Assert.assertTrue(counts.get(i) > counts.get(i-1)); } }
Example 2
Source File: LocalPlaylistStore.java From Jockey with Apache License 2.0 | 5 votes |
@Override public void addToPlaylist(Playlist playlist, Song song) { MediaStoreUtil.appendToPlaylist(mContext, playlist, song); if (mPlaylistContents.containsKey(playlist)) { BehaviorSubject<List<Song>> observableContents = mPlaylistContents.get(playlist); List<Song> updatedContents = new ArrayList<>(observableContents.getValue()); updatedContents.add(song); observableContents.onNext(updatedContents); } }
Example 3
Source File: LocalPlaylistStore.java From Jockey with Apache License 2.0 | 5 votes |
@Override public void addToPlaylist(Playlist playlist, List<Song> songs) { MediaStoreUtil.appendToPlaylist(mContext, playlist, songs); if (mPlaylistContents.containsKey(playlist)) { BehaviorSubject<List<Song>> observableContents = mPlaylistContents.get(playlist); List<Song> updatedContents = new ArrayList<>(observableContents.getValue()); updatedContents.addAll(songs); observableContents.onNext(updatedContents); } }
Example 4
Source File: ChoiceOfTwoLoadBalancerTest.java From ocelli with Apache License 2.0 | 5 votes |
@Test public void testOne() { BehaviorSubject<List<Integer>> source = BehaviorSubject.create(); LoadBalancer<Integer> lb = LoadBalancer.fromSnapshotSource(source).build(ChoiceOfTwoLoadBalancer.create(COMPARATOR)); source.onNext(Lists.newArrayList(0)); for (int i = 0; i < 100; i++) { Assert.assertEquals(0, (int)lb.next()); } }
Example 5
Source File: ChoiceOfTwoLoadBalancerTest.java From ocelli with Apache License 2.0 | 5 votes |
@Test public void testTwo() { BehaviorSubject<List<Integer>> source = BehaviorSubject.create(); LoadBalancer<Integer> lb = LoadBalancer.fromSnapshotSource(source).build(ChoiceOfTwoLoadBalancer.create(COMPARATOR)); source.onNext(Lists.newArrayList(0,1)); AtomicIntegerArray counts = new AtomicIntegerArray(2); for (int i = 0; i < 100; i++) { counts.incrementAndGet(lb.next()); } Assert.assertEquals(counts.get(0), 0); Assert.assertEquals(counts.get(1), 100); }
Example 6
Source File: DeliverReplayTest.java From FlowGeek with GNU General Public License v2.0 | 4 votes |
@Test public void testPagingCapabilities() { PublishSubject<Object> view = PublishSubject.create(); BehaviorSubject<Integer> nextPageRequests = BehaviorSubject.create(); final TestObserver<Delivery<Object, String>> testObserver = new TestObserver<>(); nextPageRequests .concatMap(new Func1<Integer, Observable<Integer>>() { @Override public Observable<Integer> call(Integer targetPage) { return targetPage <= requestedPageCount ? Observable.<Integer>never() : Observable.range(requestedPageCount, targetPage - requestedPageCount); } }) .doOnNext(new Action1<Integer>() { @Override public void call(Integer it) { requestedPageCount = it + 1; } }) .startWith(Observable.range(0, requestedPageCount)) .concatMap(new Func1<Integer, Observable<String>>() { @Override public Observable<String> call(final Integer page) { return requestPage(page, PAGE_SIZE); } }) .compose(new DeliverReplay<Object, String>(view)) .subscribe(testObserver); ArrayList<Delivery<Object, String>> onNext = new ArrayList<>(); testObserver.assertReceivedOnNext(onNext); view.onNext(999); addOnNext(onNext, 999, 0, 1, 2); testObserver.assertReceivedOnNext(onNext); nextPageRequests.onNext(2); addOnNext(onNext, 999, 3, 4, 5); testObserver.assertReceivedOnNext(onNext); view.onNext(null); assertEquals(0, testObserver.getOnCompletedEvents().size()); testObserver.assertReceivedOnNext(onNext); nextPageRequests.onNext(3); assertEquals(0, testObserver.getOnCompletedEvents().size()); testObserver.assertReceivedOnNext(onNext); view.onNext(9999); addOnNext(onNext, 9999, 0, 1, 2, 3, 4, 5, 6, 7, 8); assertEquals(0, testObserver.getOnCompletedEvents().size()); testObserver.assertReceivedOnNext(onNext); }
Example 7
Source File: MesosServerSimulationScenariosTest.java From mesos-rxjava with Apache License 2.0 | 4 votes |
@Test public void behaviorSubjectSubscribeHeartbeat() throws Exception { final List<String> events = newArrayList( SUBSCRIBED, HEARTBEAT ); final List<byte[]> expected = events.stream() .map(StringMessageCodec.UTF8_STRING::encode) .map(RecordIOUtils::createChunk) .collect(Collectors.toList()); /* * Use of BehaviorSubject here is important. The reason is that BehaviorSubject will buffer any items passed * to it's onNext method even if the Subject has not yet been subscribed to. A PublishSubject will only forward * events to it's subscriber without any buffering. */ final BehaviorSubject<String> subject = BehaviorSubject.create(); final MesosServerSimulation<String, String> sim = new MesosServerSimulation<>( Observable.from(events), StringMessageCodec.UTF8_STRING, StringMessageCodec.UTF8_STRING, SUBSCRIBE::equals ); final int serverPort = sim.start(); final URI uri = URI.create(String.format("http://localhost:%d/api/v1/scheduler", serverPort)); final HttpClient<ByteBuf, ByteBuf> httpClient = createClient(uri); final HttpClientRequest<ByteBuf> request = createRequest(uri, SUBSCRIBE); final Observable<byte[]> observable = openStream(httpClient, request); final TestSubscriber<byte[]> testSubscriber = new TestSubscriber<>(); final AwaitableEventSubscriberDecorator<byte[]> sub = new AwaitableEventSubscriberDecorator<>(testSubscriber); assertThat(sim.getCallsReceived()).hasSize(0); assertThat(testSubscriber.getOnNextEvents()).hasSize(0); final Subscription subscription = observable.subscribe(sub); // SUBSCRIBED subject.onNext(events.get(0)); // HEARTBEAT subject.onNext(events.get(1)); sub.awaitEvent(2); final List<byte[]> onNextEvents = testSubscriber.getOnNextEvents(); assertThat(onNextEvents).hasSize(2); assertThat(onNextEvents.get(0)).isEqualTo(expected.get(0)); assertThat(onNextEvents.get(1)).isEqualTo(expected.get(1)); assertThat(sim.getCallsReceived()).hasSize(1); assertThat(sim.getCallsReceived()).contains(SUBSCRIBE); assertThat(deepEquals(expected, testSubscriber.getOnNextEvents())).isTrue(); subscription.unsubscribe(); sim.shutdown(); }
Example 8
Source File: DeliverReplayTest.java From nucleus with MIT License | 4 votes |
@Test public void testPagingCapabilities() { PublishSubject<Object> view = PublishSubject.create(); BehaviorSubject<Integer> nextPageRequests = BehaviorSubject.create(); final TestObserver<Delivery<Object, String>> testObserver = new TestObserver<>(); nextPageRequests .concatMap(new Func1<Integer, Observable<Integer>>() { @Override public Observable<Integer> call(Integer targetPage) { return targetPage <= requestedPageCount ? Observable.<Integer>never() : Observable.range(requestedPageCount, targetPage - requestedPageCount); } }) .doOnNext(new Action1<Integer>() { @Override public void call(Integer it) { requestedPageCount = it + 1; } }) .startWith(Observable.range(0, requestedPageCount)) .concatMap(new Func1<Integer, Observable<String>>() { @Override public Observable<String> call(final Integer page) { return requestPage(page, PAGE_SIZE); } }) .compose(new DeliverReplay<Object, String>(view)) .subscribe(testObserver); ArrayList<Delivery<Object, String>> onNext = new ArrayList<>(); testObserver.assertReceivedOnNext(onNext); view.onNext(999); addOnNext(onNext, 999, 0, 1, 2); testObserver.assertReceivedOnNext(onNext); nextPageRequests.onNext(2); addOnNext(onNext, 999, 3, 4, 5); testObserver.assertReceivedOnNext(onNext); view.onNext(null); assertEquals(0, testObserver.getOnCompletedEvents().size()); testObserver.assertReceivedOnNext(onNext); nextPageRequests.onNext(3); assertEquals(0, testObserver.getOnCompletedEvents().size()); testObserver.assertReceivedOnNext(onNext); view.onNext(9999); addOnNext(onNext, 9999, 0, 1, 2, 3, 4, 5, 6, 7, 8); assertEquals(0, testObserver.getOnCompletedEvents().size()); testObserver.assertReceivedOnNext(onNext); }
Example 9
Source File: ChoiceOfTwoLoadBalancerTest.java From ocelli with Apache License 2.0 | 3 votes |
@Test(expected=NoSuchElementException.class) public void testEmpty() { BehaviorSubject<List<Integer>> source = BehaviorSubject.create(); LoadBalancer<Integer> lb = LoadBalancer.fromSnapshotSource(source).build(ChoiceOfTwoLoadBalancer.create(COMPARATOR)); source.onNext(Lists.<Integer>newArrayList()); lb.next(); }