def test_list_asynchronously(self): sink = TObserver(immediate_continue=0) observer = TakeWhileObserver( observer=sink, predicate=lambda v: v, ) self.source.observe(init_observer_info(observer)) ack = self.source.on_next_list([1, 1, 1]) self.assertFalse(ack.is_sync) self.assertEqual(1, sink.on_next_counter) self.assertListEqual(sink.received, [1, 1, 1]) ack.on_next(continue_ack) ack = self.source.on_next_list([1, 0, 1]) self.assertIsInstance(ack, StopAck) self.assertEqual(2, sink.on_next_counter) self.assertListEqual(sink.received, [1, 1, 1, 1])
def test_right_on_completed(self): """ right.on_completed left.on_next WaitOnLeft --------------------> WaitOnLeft ------------------> Stopped """ sink = TObserver(immediate_continue=0) self.obs.observe(init_observer_info(sink)) self.right.on_next_list([select_completed]) self.right.on_completed() self.assertIsInstance(self.measure_state(self.obs), ControlledZipStates.WaitOnLeft) self.left.on_next_list([select_next]) self.assertIsInstance(self.measure_state(self.obs), ControlledZipStates.Stopped) self.assertEqual([], sink.received) # the last SelectCompleted is optional
def test_complete_first_source(self): sink = TObserver() observer = FlatConcatNoBackpressureObserver( next_observer=sink, selector=lambda v: v, scheduler=self.scheduler, subscribe_scheduler=self.scheduler, composite_disposable=self.composite_disposable, ) self.source.observe(init_observer_info(observer=observer)) self.source.on_next_single(self.source1) self.scheduler.advance_by(1) self.source1.on_next_single(1) self.source.on_next_single(self.source2) self.scheduler.advance_by(1) self.source.on_completed() self.source2.on_next_single('a') self.source1.on_completed() self.assertEqual([1, 'a'], sink.received) self.assertFalse(sink.is_completed)
def test_wait_on_right_to_wait_on_left_right_with_synchronous_ack(self): """ s2.on_next WaitOnRight ------------> WaitOnLeftRight """ sink = TObserver() obs = ControlledZipObservable( left=self.left, right=self.right, scheduler=self.scheduler, request_left=lambda left, right: left <= right, request_right=lambda left, right: right <= left, match_func=lambda left, right: left == right, ) obs.observe(init_observer_info(sink)) ack1 = self.left.on_next_single(1) ack2 = self.right.on_next_single(1) self.assertIsInstance(self.measure_state(obs), ControlledZipStates.WaitOnLeftRight) self.assertIsInstance(ack1.value, ContinueAck) self.assertIsInstance(ack2, ContinueAck) self.assertListEqual(sink.received, [(1, 1)])
def test_on_next_assynchronously_reenter_fast_loop(self): """ ack.on_next queue = [OnNext(3)] -> queue = [] """ # preparations o1 = TObserver(immediate_continue=0) self.subject.observe(init_observer_info(o1)) self.source.on_next_single(1) self.source.on_next_single(2) self.source.on_next_single(3) o1.ack.on_next(continue_ack) self.scheduler.advance_by(1) # state change o1.ack.on_next(continue_ack) self.scheduler.advance_by(1) # validation self.assertEqual([1, 2, 3], o1.received) self.assertEqual(0, len(self.subject.shared_state.queue))
def test_wait_on_left_right_to_wait_on_right_with_synchronous_ack(self): """ s1.on_next WaitOnLeftRight ------------> WaitOnRight InitState InitState """ sink = TObserver() obs = ControlledZipObservable( left=self.left, right=self.right, scheduler=self.scheduler, request_left=lambda left, right: left <= right, request_right=lambda left, right: right <= left, match_func=lambda left, right: left == right, ) obs.observe(init_observer_info(sink)) ack1 = self.left.on_next_single(1) self.assertIsInstance(self.measure_state(obs), ControlledZipStates.WaitOnRight) self.assertIsInstance(self.measure_termination_state(obs), TerminationStates.InitState) self.assertFalse(ack1.has_value) self.assertListEqual(sink.received, [])
def test_acknowledge_both(self): """ ack.on_next WaitOnRightLeft ------------> WaitOnRightLeft """ sink = TObserver(immediate_continue=0) obs = ControlledZipObservable( left=self.left, right=self.right, scheduler=self.scheduler, request_left=lambda left, right: left <= right, request_right=lambda left, right: right <= left, match_func=lambda left, right: left == right, ) obs.observe(init_observer_info(sink)) ack1 = self.left.on_next_list([1]) ack2 = self.right.on_next_list([1]) sink.ack.on_next(continue_ack) self.assertIsInstance(self.measure_state(obs), ControlledZipStates.WaitOnLeftRight) self.assertIsInstance(ack1.value, ContinueAck) self.assertIsInstance(ack2.value, ContinueAck) self.assertListEqual(sink.received, [(1, 1)])
def test_left_complete_to_stopped(self): """ s2.on_next WaitOnRight ------------> Stopped LeftComplete BothCompletedState """ sink = TObserver(immediate_continue=0) obs = ControlledZipObservable( left=self.left, right=self.right, scheduler=self.scheduler, request_left=lambda left, right: left <= right, request_right=lambda left, right: right <= left, match_func=lambda left, right: left == right, ) obs.observe(init_observer_info(sink)) self.left.on_next_list([1]) self.left.on_completed() self.right.on_next_list([1]) self.assertIsInstance(self.measure_termination_state(obs), TerminationStates.LeftCompletedState) self.assertIsInstance(self.measure_state(obs), ControlledZipStates.Stopped) self.assertListEqual(sink.received, [(1, 1)])
def test_wait_on_left_right_to_wait_on_right(self): """ left.on_next WaitOnLeftRight -----------------> WaitOnRight InitState InitState """ sink = TObserver() obs = MergeSelectorObservable( left=self.left, right=self.right, # scheduler=self.scheduler, ) obs.observe(init_observer_info(sink)) ack = self.left.on_next_list([select_next, select_completed]) self.assertIsInstance(self.measure_termination_state(obs), TerminationStates.InitState) self.assertIsInstance(self.measure_state(obs), ControlledZipStates.WaitOnRight) self.assertEqual([], sink.received) self.assertFalse(ack.is_sync)
def test_on_error_should_terminate_current_and_future_subscribers(self): subject = PublishObservableSubject(self.scheduler) s1 = TObservable(observer=subject) def gen_observers(): for _ in range(10): observer = TObserver() subject.observe(init_observer_info(observer=observer, )) yield observer observer_list = list(gen_observers()) s1.on_next_iter([1]) s1.on_error(self.exception) o1 = TObserver() subject.observe(init_observer_info(o1)) for observer in observer_list: self.assertListEqual(observer.received, [1]) self.assertEqual(observer.exception, self.exception) self.assertEqual(o1.exception, self.exception)
def test_failure_after_non_matching_element_synchronously(self): sink = TObserver(immediate_continue=None) observer = TakeWhileObserver( observer=sink, predicate=lambda v: v, ) self.source.observe(init_observer_info(observer)) def gen_iterable(): for i in range(10): if i == 2: yield 0 elif i == 3: raise self.exception else: yield 1 ack = self.source.on_next_iter(gen_iterable()) self.assertIsInstance(ack, StopAck) self.assertEqual(1, sink.on_next_counter) self.assertListEqual([1, 1], sink.received) self.assertTrue(sink.is_completed) self.assertIsNone(sink.exception)
def setUp(self): self.scheduler = TScheduler() self.sink = TObserver()
def setUp(self) -> None: self.source = TObservable() self.sink = TObserver()
import time from threading import Thread import rxbp from rxbp.schedulers.timeoutscheduler import TimeoutScheduler from rxbp.testing.tobserver import TObserver def counter(sink): while True: time.sleep(1) print(f"immediate: {sink.immediate_continue}, received: ", sink.received) publisher = rxbp.interval(0.5).pipe(rxbp.op.map(lambda v: "data_" + str(v))) sink = TObserver(immediate_continue=12) publisher.subscribe(observer=sink, subscribe_scheduler=TimeoutScheduler()) t1 = Thread(target=counter, args=(sink, )) t1.start() t1.join()
def test_initialize(self): sink = TObserver() observer = ConnectableObserver( sink, )
def test_initialize(self): sink = TObserver() BufferedObserver(underlying=sink, scheduler=self.scheduler, subscribe_scheduler=self.scheduler, buffer_size=0)
def test_initialize(self): sink = TObserver() ToListObserver(observer=sink, )
def test_initialize(self): sink = TObserver() TakeWhileObserver( observer=sink, predicate=lambda _: True, )
def test_initialize(self): sink = TObserver() FilterObserver( observer=sink, predicate=lambda _: True, )
def test_initialize(self): sink = TObserver() DefaultIfEmptyObserver( next_observer=sink, lazy_val=lambda: 0, )
def gen_observers(): for i in range(10): o1 = TObserver() o1.immediate_continue = 5 subject.observe(init_observer_info(o1)) yield o1
def setUp(self): self.scheduler = TScheduler() self.sink = TObserver(immediate_continue=0)
def gen_observers(): for _ in range(10): observer = TObserver() subject.observe(init_observer_info(observer=observer, )) yield observer
def test_initialize(self): sink = TObserver() FirstObserver( observer=sink, stack=get_stack_lines(), )
def setUp(self) -> None: self.scheduler = TScheduler() self.composite_disposable = CompositeDisposable() self.sink = TObserver() self.exception = Exception()
def test_initialize(self): sink = TObserver() ZipCountObserver( observer=sink, selector=lambda v, idx: (v, idx), )