def test_retry_observable_retry_count_throws(self): scheduler1 = TestScheduler() xs = rx.return_value(1).pipe(ops.retry(3)) xs.subscribe(lambda x: _raise('ex'), scheduler=scheduler1) self.assertRaises(RxException, scheduler1.start) scheduler2 = TestScheduler() ys = rx.throw('ex').pipe(ops.retry(100)) d = ys.subscribe(on_error=lambda ex: _raise('ex'), scheduler=scheduler2) def dispose(_, __): d.dispose() scheduler2.schedule_absolute(0, dispose) scheduler2.start() scheduler3 = TestScheduler() zs = rx.return_value(1).pipe(ops.retry(100)) zs.subscribe(on_completed=lambda: _raise('ex'), scheduler=scheduler3) with pytest.raises(RxException): scheduler3.start() xss = rx.create(lambda o: _raise('ex')).pipe(ops.retry(100)) with pytest.raises(Exception): xss.subscribe()
def test_retry_observable_error(self): ex = 'ex' scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(100, 1), on_next(150, 2), on_next(200, 3), on_error(250, ex)) results = scheduler.start(lambda: xs.pipe(ops.retry()), disposed=1100) assert results.messages == [ on_next(300, 1), on_next(350, 2), on_next(400, 3), on_next(550, 1), on_next(600, 2), on_next(650, 3), on_next(800, 1), on_next(850, 2), on_next(900, 3), on_next(1050, 1) ] assert xs.subscriptions == [ subscribe(200, 450), subscribe(450, 700), subscribe(700, 950), subscribe(950, 1100) ]
def main(): xs = rx.from_marbles("1-2-3-4-5-6-7-9-|").pipe(ops.publish()) xs.pipe(ops.map(failing), ops.retry()).subscribe(print) xs.connect() # Must connect. Cannot use ref_count() with publish() time.sleep(5)
def test_retry_observable_infinite(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable( on_next(100, 1), on_next(150, 2), on_next(200, 3)) results = scheduler.start(lambda: xs.pipe(ops.retry())) assert results.messages == [ on_next(300, 1), on_next(350, 2), on_next(400, 3)] assert xs.subscriptions == [subscribe(200, 1000)]
def test_retry_observable_retry_count_dispose_iii(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable( on_next(100, 1), on_next(150, 2), on_next(200, 3), on_completed(250)) results = scheduler.start(lambda: xs.pipe(ops.retry(3))) assert results.messages == [ on_next(300, 1), on_next(350, 2), on_next(400, 3), on_completed(450)] assert xs.subscriptions == [subscribe(200, 450)]
def test_retry_observable_retry_count_dispose(self): scheduler = TestScheduler() ex = 'ex' xs = scheduler.create_cold_observable( on_next(5, 1), on_next(10, 2), on_next(15, 3), on_error(20, ex)) results = scheduler.start(lambda: xs.pipe(ops.retry(3)), disposed=231) assert results.messages == [ on_next(205, 1), on_next(210, 2), on_next(215, 3), on_next(225, 1), on_next(230, 2)] assert xs.subscriptions == [subscribe(200, 220), subscribe(220, 231)]
def test_retry_observable_infinite(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(100, 1), on_next(150, 2), on_next(200, 3)) results = scheduler.start(lambda: xs.pipe(ops.retry())) assert results.messages == [ on_next(300, 1), on_next(350, 2), on_next(400, 3) ] assert xs.subscriptions == [subscribe(200, 1000)]
def test_retry_observable_throws(self): scheduler1 = TestScheduler() xs = rx.return_value(1).pipe(ops.retry()) xs.subscribe(lambda x: _raise('ex'), scheduler=scheduler1) with pytest.raises(RxException): scheduler1.start() scheduler2 = TestScheduler() ys = rx.throw('ex').pipe(ops.retry()) d = ys.subscribe(on_error=lambda ex: _raise('ex'), scheduler=scheduler2) scheduler2.schedule_absolute(210, lambda sc, st: d.dispose()) scheduler2.start() scheduler3 = TestScheduler() zs = rx.return_value(1).pipe(ops.retry()) zs.subscribe(on_completed=lambda: _raise('ex'), scheduler=scheduler3) with pytest.raises(RxException): scheduler3.start()
def test_retry_observable_retry_count_dispose_iii(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable(on_next(100, 1), on_next(150, 2), on_next(200, 3), on_completed(250)) results = scheduler.start(lambda: xs.pipe(ops.retry(3))) assert results.messages == [ on_next(300, 1), on_next(350, 2), on_next(400, 3), on_completed(450) ] assert xs.subscriptions == [subscribe(200, 450)]
def test_retry_observable_retry_count_dispose(self): scheduler = TestScheduler() ex = 'ex' xs = scheduler.create_cold_observable(on_next(5, 1), on_next(10, 2), on_next(15, 3), on_error(20, ex)) results = scheduler.start(lambda: xs.pipe(ops.retry(3)), disposed=231) assert results.messages == [ on_next(205, 1), on_next(210, 2), on_next(215, 3), on_next(225, 1), on_next(230, 2) ] assert xs.subscriptions == [subscribe(200, 220), subscribe(220, 231)]
def test_retry_observable_error(self): ex = 'ex' scheduler = TestScheduler() xs = scheduler.create_cold_observable( on_next(100, 1), on_next(150, 2), on_next(200, 3), on_error(250, ex)) results = scheduler.start(lambda: xs.pipe(ops.retry()), disposed=1100) assert results.messages == [ on_next(300, 1), on_next(350, 2), on_next(400, 3), on_next(550, 1), on_next(600, 2), on_next(650, 3), on_next(800, 1), on_next(850, 2), on_next(900, 3), on_next(1050, 1)] assert xs.subscriptions == [ subscribe(200, 450), subscribe(450, 700), subscribe(700, 950), subscribe(950, 1100)]
def __init__(self, scheduler=None): self._observerable = rx.interval( ObserveConfig.interval, scheduler).pipe(ops.map(lambda dummy: get_merge_requests()), ops.retry(), ops.publish(), ops.ref_count()) self._ready_to_merge = self._observerable.pipe( ops.map(lambda requests: next((request for request in requests if is_ready_to_merge(request)), None)), ops.start_with(None), ops.distinct_until_changed()) self._ready_to_merge.subscribe(lambda ready_to_merge: logging.info( 'Ready to merge: ' + str(ready_to_merge))) voted_merge_requests = self._observerable.pipe( ops.map(_to_voted_merge_requests_set)) self._new_votes_merge_requests = voted_merge_requests.pipe( ops.skip(1), ops.zip(voted_merge_requests), ops.map(lambda zipped: zipped[0] - zipped[1]), ops.filter(len), ops.map(_to_merge_requests)) self._new_votes_merge_requests.pipe( ops.map(lambda diff_set: [merge_request.get_iid() for merge_request in diff_set]) ).subscribe( lambda ids: logging.info(f'New votes for merge requests: {ids}')) awards = self._new_votes_merge_requests.pipe(ops.map(_to_awards_set), ops.publish(), ops.ref_count(), ops.start_with(set())) self._new_awards = awards.pipe( ops.skip(1), ops.zip(awards), ops.map(lambda zipped: zipped[0] - zipped[1]), ops.filter(len), ops.flat_map(lambda diff_set: rx.from_iterable(diff_set)), ops.map(lambda award_key: award_key.award)) self._new_awards.subscribe( lambda new_award: logging.info('New award: ' + str(new_award)))
def formata(produto): if (produto['Preco'] <= 0): raise Exception('There is error cannot proceed!: {}'.format( produto['Preco'])) else: produto = produto["Tipo"] + " " + produto["Marca"] + " - " + str( produto["Preco"]) return produto print("Sujestões de pedidos para a pessoa 1") # Task 1 => não quer presunto source = rx.from_(produtos) case1 = source.pipe( ops.filter(lambda a: a['Tipo'] != 'presunto' and a['Tipo'] != 'pepino'), ops.map(lambda a: formata(a)), ops.retry(1), ops.map(lambda a: adding_delay(a)), ops.subscribe_on(thread_pool_scheduler)) case1.subscribe( on_next=lambda i: print("Para Pessoa1 - {0}".format(i)), on_error=lambda e: print("Error : {0}".format(e)), on_completed=lambda: print("Task 1 complete"), ) # Se uma parar a outra funciona # Task 2 => é vegetariana então não quer nem presunto nem carne. print("Sujestões de pedidos para a pessoa 2") case2 = source.pipe( ops.skip_while(lambda a: a['Tipo'] == 'carne' or a['Tipo'] == 'presunto'), ops.map(lambda a: formata(a)), ops.retry(1), ops.map(lambda a: adding_delay(a)), ops.observe_on(thread_pool_scheduler))
def unlock_request(img_faces_pair): return rx_request('post', ARGS.door_api_url, timeout=0.3).pipe( ops.do_action(on_error=lambda e: logger.exception(e)), ops.retry(3), ops.catch(rx.empty()), ops.do_action(on_next=lambda _: logger.info('Door unlocked\n')), ops.do_action(on_next=lambda _: log_unlock(img_faces_pair)))
op.debounce() op.distinct() op.filter() op.element_at() op.first() op.ignore_elements() op.last() op.skip() op.skip_last() op.take() op.take_last() # ... """Error Handling""" op.catch() op.retry() """Utility""" op.delay() op.materialize() op.time_interval() op.timeout() op.timestamp() """Conditional and Boolean""" op.all() op.contains() op.default_if_empty() op.sequence_equal() op.skip_until() op.skip_while()
import rx import rx.operators as ops subscribe_count = 0 def on_subscribe(observer, scheduler): global subscribe_count subscribe_count += 1 if subscribe_count == 1: observer.on_next(1) observer.on_error("error!") else: observer.on_next(1) observer.on_next(2) observer.on_next(3) observer.on_completed() err = rx.create(on_subscribe) err.pipe(ops.retry(2)).subscribe( on_next=lambda i: print("item: {}".format(i)), on_error=lambda e: print("error: {}".format(e)), on_completed=lambda: print("completed"))