def test_select_throws(self): try: Observable.return_value(1) \ .select(lambda x, y: x) \ .subscribe(lambda x: _raise("ex")) except RxException: pass try: Observable.throw_exception('ex') \ .select(lambda x, y: x) \ .subscribe(on_error=lambda ex: _raise(ex)) except RxException: pass try: Observable.empty() \ .select(lambda x, y: x) \ .subscribe(lambda x: x, lambda ex: ex, lambda: _raise('ex')) except RxException: pass def subscribe(observer): _raise('ex') try: Observable.create(subscribe) \ .select(lambda x: x) \ .subscribe() except RxException: pass
def test_select_throws(): try: Observable.return_value(1) \ .select(lambda x, y: x) \ .subscribe(lambda x: _raise("ex")) except RxException: pass try: Observable.throw_exception('ex') \ .select(lambda x, y: x) \ .subscribe(on_error=lambda ex: _raise(ex)) except RxException: pass try: Observable.empty() \ .select(lambda x, y: x) \ .subscribe(lambda x: x, lambda ex: ex, lambda: _raise('ex')) except RxException: pass def subscribe(observer): _raise('ex') try: Observable.create(subscribe) \ .select(lambda x: x).dump() \ .subscribe() except RxException: pass
def test_select_with_index_throws(): try: return Observable.return_value(1) \ .select(lambda x, index: x) \ .subscribe(lambda x: _raise('ex')) except RxException: pass try: return Observable.throw_exception('ex') \ .select(lambda x, index: x) \ .subscribe(lambda x: x, lambda ex: _raise(ex)) except RxException: pass try: return Observable.empty() \ .select(lambda x, index: x) \ .subscribe(lambda x: x, lambda ex: _, lambda : _raise('ex')) except RxException: pass try: return Observable.create(lambda o: _raise('ex')) \ .select(lambda x, index: x) \ .subscribe() except RxException: pass
def test_select_with_index_throws(self): try: return Observable.return_value(1) \ .select(lambda x, index: x) \ .subscribe(lambda x: _raise('ex')) except RxException: pass try: return Observable.throw_exception('ex') \ .select(lambda x, index: x) \ .subscribe(lambda x: x, lambda ex: _raise(ex)) except RxException: pass try: return Observable.empty() \ .select(lambda x, index: x) \ .subscribe(lambda x: x, lambda ex: _, lambda : _raise('ex')) except RxException: pass try: return Observable.create(lambda o: _raise('ex')) \ .select(lambda x, index: x) \ .subscribe() except RxException: pass
def test_repeat_observable_repeat_count_throws(self): scheduler1 = TestScheduler() xs = Observable.return_value(1, scheduler1).repeat(3) xs.subscribe(lambda x: _raise('ex')) with self.assertRaises(RxException): scheduler1.start() scheduler2 = TestScheduler() ys = Observable.throw_exception('ex1', scheduler2).repeat(3) ys.subscribe(on_error=lambda ex: _raise('ex2')) with self.assertRaises(RxException): scheduler2.start() scheduler3 = TestScheduler() zs = Observable.return_value(1, scheduler3).repeat(100) d = zs.subscribe(on_completed=lambda: _raise('ex3')) scheduler3.schedule_absolute(10, lambda sc, st: d.dispose()) scheduler3.start() xss = Observable.create(lambda o: _raise('ex4')).repeat(3) with self.assertRaises(RxException): xss.subscribe()
def test_retry_observable_throws(self): scheduler1 = TestScheduler() xs = Observable.return_value(1, scheduler1).retry() xs.subscribe(lambda x: _raise('ex')) try: return scheduler1.start() except RxException: pass scheduler2 = TestScheduler() ys = Observable.throw_exception('ex', scheduler2).retry() d = ys.subscribe(on_error=lambda ex: _raise('ex')) scheduler2.schedule_absolute(210, lambda: d.dispose()) scheduler2.start() scheduler3 = TestScheduler() zs = Observable.return_value(1, scheduler3).retry() zs.subscribe(on_completed=lambda: _raise('ex')) try: return scheduler3.start() except RxException: pass xss = Observable.create(lambda o: _raise('ex')).retry() try: return xss.subscribe() except RxException: pass
def subscribe(observer): result = None try: result = observable_factory() except Exception as ex: return Observable.throw_exception(ex).subscribe(observer) return result.subscribe(observer)
def subscribe(observer): try: result = observable_factory() except Exception as ex: return Observable.throw_exception(ex).subscribe(observer) result = Observable.from_future(result) return result.subscribe(observer)
def subscribe(observer): disposable = Disposable.empty() try: resource = resource_factory() if resource: disposable = resource source = observable_factory(resource) except Exception as exception: d = Observable.throw_exception(exception).subscribe(observer) return CompositeDisposable(d, disposable) return CompositeDisposable(source.subscribe(observer), disposable)
def test_select_throws(self): with self.assertRaises(RxException): Observable.return_value(1) \ .map(lambda x, y: x) \ .subscribe(lambda x: _raise("ex")) with self.assertRaises(RxException): Observable.throw_exception('ex') \ .map(lambda x, y: x) \ .subscribe(on_error=lambda ex: _raise(ex)) with self.assertRaises(RxException): Observable.empty() \ .map(lambda x, y: x) \ .subscribe(lambda x: x, lambda ex: ex, lambda: _raise('ex')) def subscribe(observer): _raise('ex') with self.assertRaises(RxException): Observable.create(subscribe) \ .map(lambda x: x) \ .subscribe()
def test_select_throws(self): with self.assertRaises(RxException): Observable.return_value(1) \ .select(lambda x, y: x) \ .subscribe(lambda x: _raise("ex")) with self.assertRaises(RxException): Observable.throw_exception('ex') \ .select(lambda x, y: x) \ .subscribe(on_error=lambda ex: _raise(ex)) with self.assertRaises(RxException): Observable.empty() \ .select(lambda x, y: x) \ .subscribe(lambda x: x, lambda ex: ex, lambda: _raise('ex')) def subscribe(observer): _raise('ex') with self.assertRaises(RxException): Observable.create(subscribe) \ .select(lambda x: x) \ .subscribe()
def go(): error = Exception('woops') source = Observable.throw_exception(error) future = source.to_future(asyncio.Future) def done(future): try: value = future.result() except Exception as ex: success[1] = str(ex) == str(error) else: success[0] = False future.add_done_callback(done)
def test_select_with_index_throws(self): with self.assertRaises(RxException): return Observable.return_value(1) \ .select(lambda x, index: x) \ .subscribe(lambda x: _raise('ex')) with self.assertRaises(RxException): return Observable.throw_exception('ex') \ .select(lambda x, index: x) \ .subscribe(lambda x: x, lambda ex: _raise(ex)) with self.assertRaises(RxException): return Observable.empty() \ .select(lambda x, index: x) \ .subscribe(lambda x: x, lambda ex: _, lambda : _raise('ex')) with self.assertRaises(RxException): return Observable.create(lambda o: _raise('ex')) \ .select(lambda x, index: x) \ .subscribe()
def test_select_with_index_throws(self): with self.assertRaises(RxException): return Observable.return_value(1) \ .map(lambda x, index: x) \ .subscribe(lambda x: _raise('ex')) with self.assertRaises(RxException): return Observable.throw_exception('ex') \ .map(lambda x, index: x) \ .subscribe(lambda x: x, lambda ex: _raise(ex)) with self.assertRaises(RxException): return Observable.empty() \ .map(lambda x, index: x) \ .subscribe(lambda x: x, lambda ex: _, lambda : _raise('ex')) with self.assertRaises(RxException): return Observable.create(lambda o: _raise('ex')) \ .map(lambda x, index: x) \ .subscribe()
def test_retry_observable_throws(self): scheduler1 = TestScheduler() xs = Observable.return_value(1, scheduler1).retry() xs.subscribe(lambda x: _raise('ex')) self.assertRaises(RxException, scheduler1.start) scheduler2 = TestScheduler() ys = Observable.throw_exception('ex', scheduler2).retry() d = ys.subscribe(on_error=lambda ex: _raise('ex')) scheduler2.schedule_absolute(210, lambda sc, st: d.dispose()) scheduler2.start() scheduler3 = TestScheduler() zs = Observable.return_value(1, scheduler3).retry() zs.subscribe(on_completed=lambda: _raise('ex')) self.assertRaises(RxException, scheduler3.start) xss = Observable.create(lambda o: _raise('ex')).retry() self.assertRaises(Exception, xss.subscribe)
def timeout(self, duetime, other=None, scheduler=None): """Returns the source observable sequence or the other observable sequence if duetime elapses. 1 - res = source.timeout(new Date()); # As a date 2 - res = source.timeout(5000); # 5 seconds # As a date and timeout observable 3 - res = source.timeout(datetime(), rx.Observable.return_value(42)) # 5 seconds and timeout observable 4 - res = source.timeout(5000, rx.Observable.return_value(42)) # As a date and timeout observable 5 - res = source.timeout(datetime(), rx.Observable.return_value(42), rx.Scheduler.timeout) # 5 seconds and timeout observable 6 - res = source.timeout(5000, rx.Observable.return_value(42), rx.Scheduler.timeout) Keyword arguments: :param datetime|int duetime: Absolute (specified as a datetime object) or relative time (specified as an integer denoting milliseconds) when a timeout occurs. :param Observable other: Sequence to return in case of a timeout. If not specified, a timeout error throwing sequence will be used. :param Scheduler scheduler: Scheduler to run the timeout timers on. If not specified, the timeout scheduler is used. :returns: The source sequence switching to the other sequence in case of a timeout. :rtype: Observable """ scheduler_method = None source = self other = other or Observable.throw_exception(Exception("Timeout")) other = Observable.from_future(other) scheduler = scheduler or timeout_scheduler if isinstance(duetime, datetime): scheduler_method = scheduler.schedule_absolute else: scheduler_method = scheduler.schedule_relative def subscribe(observer): switched = [False] _id = [0] original = SingleAssignmentDisposable() subscription = SerialDisposable() timer = SerialDisposable() subscription.disposable = original def create_timer(): my_id = _id[0] def action(scheduler, state=None): switched[0] = (_id[0] == my_id) timer_wins = switched[0] if timer_wins: subscription.disposable = other.subscribe(observer) timer.disposable = scheduler_method(duetime, action) create_timer() def on_next(x): on_next_wins = not switched[0] if on_next_wins: _id[0] += 1 observer.on_next(x) create_timer() def on_error(e): on_error_wins = not switched[0] if on_error_wins: _id[0] += 1 observer.on_error(e) def on_completed(): on_completed_wins = not switched[0] if on_completed_wins: _id[0] += 1 observer.on_completed() original.disposable = source.subscribe(on_next, on_error, on_completed) return CompositeDisposable(subscription, timer) return AnonymousObservable(subscribe)
def timeout_with_selector(self, first_timeout=None, timeout_duration_selector=None, other=None): """Returns the source observable sequence, switching to the other observable sequence if a timeout is signaled. 1 - res = source.timeout_with_selector(rx.Observable.timer(500)) 2 - res = source.timeout_with_selector(rx.Observable.timer(500), lambda x: rx.Observable.timer(200)) 3 - res = source.timeout_with_selector(rx.Observable.timer(500), lambda x: rx.Observable.timer(200)), rx.Observable.return_value(42)) first_timeout -- [Optional] Observable sequence that represents the timeout for the first element. If not provided, this defaults to Observable.never(). timeout_Duration_selector -- [Optional] Selector to retrieve an observable sequence that represents the timeout between the current element and the next element. other -- [Optional] Sequence to return in case of a timeout. If not provided, this is set to Observable.throw_exception(). Returns the source sequence switching to the other sequence in case of a timeout. """ first_timeout = first_timeout or Observable.never() other = other or Observable.throw_exception(Exception('Timeout')) source = self def subscribe(observer): subscription = SerialDisposable() timer = SerialDisposable() original = SingleAssignmentDisposable() subscription.disposable = original switched = False _id = [0] def set_timer(timeout): my_id = _id[0] def timer_wins(): return _id[0] == my_id d = SingleAssignmentDisposable() timer.disposable = d def on_next(x): if timer_wins(): subscription.disposable = other.subscribe(observer) d.dispose() def on_error(e): if timer_wins(): observer.on_error(e) def on_completed(): if timer_wins(): subscription.disposable = other.subscribe(observer) d.disposable = timeout.subscribe(on_next, on_error, on_completed) set_timer(first_timeout) def observer_wins(): res = not switched if res: _id[0] += 1 return res def on_next(x): if observer_wins(): observer.on_next(x) timeout = None try: timeout = timeout_duration_selector(x) except Exception as e: observer.on_error(e) return set_timer(timeout) def on_error(e): if observer_wins(): observer.on_error(e) def on_completed(): if observer_wins(): observer.on_completed() original.disposable = source.subscribe(on_next, on_error, on_completed) return CompositeDisposable(subscription, timer) return AnonymousObservable(subscribe)
def timeout(self, duetime, other=None, scheduler=None): """ Returns the source observable sequence or the other observable sequence if duetime elapses. 1 - res = source.timeout(new Date()); // As a date 2 - res = source.timeout(5000); // 5 seconds 3 - res = source.timeout(new Date(), Rx.Observable.returnValue(42)); // As a date and timeout observable 4 - res = source.timeout(5000, Rx.Observable.returnValue(42)); // 5 seconds and timeout observable 5 - res = source.timeout(new Date(), Rx.Observable.returnValue(42), Rx.Scheduler.timeout); // As a date and timeout observable 6 - res = source.timeout(5000, Rx.Observable.returnValue(42), Rx.Scheduler.timeout); // 5 seconds and timeout observable duetime -- Absolute (specified as a datetime object) or relative time (specified as an integer denoting milliseconds) when a timeout occurs. other -- [Optional] Sequence to return in case of a timeout. If not specified, a timeout error throwing sequence will be used. scheduler -- [Optional] Scheduler to run the timeout timers on. If not specified, the timeout scheduler is used. Returns the source sequence switching to the other sequence in case of a timeout. """ scheduler_method = None source = self other = other or Observable.throw_exception(Exception("Timeout")) scheduler = scheduler or timeout_scheduler if isinstance(duetime, datetime): scheduler_method = scheduler.schedule_absolute else: duetime = duetime if isinstance(duetime, timedelta) else timedelta(milliseconds=duetime) scheduler_method = scheduler.schedule_relative def subscribe(observer): switched = False _id = 0 original = SingleAssignmentDisposable() subscription = SerialDisposable() timer = SerialDisposable() subscription.disposable = original def create_timer(): my_id = _id def action(scheduler, state=None): nonlocal switched switched = (_id == my_id) timer_wins = switched if timer_wins: subscription.disposable = other.subscribe(observer) timer.disposable = scheduler_method(duetime, action) create_timer() def on_next(x): nonlocal _id on_next_wins = not switched if on_next_wins: _id += 1 observer.on_next(x) create_timer() def on_error(e): nonlocal _id on_error_wins = not switched if on_error_wins: _id += 1 observer.on_error(e) def on_completed(): nonlocal _id on_completed_wins = not switched if on_completed_wins: _id += 1 observer.on_completed() original.disposable = source.subscribe(on_next, on_error, on_completed) return CompositeDisposable(subscription, timer) return AnonymousObservable(subscribe)
def timeout(self, duetime, other=None, scheduler=None): """ Returns the source observable sequence or the other observable sequence if duetime elapses. 1 - res = source.timeout(new Date()); // As a date 2 - res = source.timeout(5000); // 5 seconds 3 - res = source.timeout(new Date(), Rx.Observable.returnValue(42)); // As a date and timeout observable 4 - res = source.timeout(5000, Rx.Observable.returnValue(42)); // 5 seconds and timeout observable 5 - res = source.timeout(new Date(), Rx.Observable.returnValue(42), Rx.Scheduler.timeout); // As a date and timeout observable 6 - res = source.timeout(5000, Rx.Observable.returnValue(42), Rx.Scheduler.timeout); // 5 seconds and timeout observable duetime -- Absolute (specified as a datetime object) or relative time (specified as an integer denoting milliseconds) when a timeout occurs. other -- [Optional] Sequence to return in case of a timeout. If not specified, a timeout error throwing sequence will be used. scheduler -- [Optional] Scheduler to run the timeout timers on. If not specified, the timeout scheduler is used. Returns the source sequence switching to the other sequence in case of a timeout. """ scheduler_method = None source = self other = other or Observable.throw_exception(Exception("Timeout")) scheduler = scheduler or timeout_scheduler if isinstance(duetime, datetime): scheduler_method = scheduler.schedule_absolute else: duetime = duetime if isinstance(duetime, timedelta) else timedelta( milliseconds=duetime) scheduler_method = scheduler.schedule_relative def subscribe(observer): switched = False _id = 0 original = SingleAssignmentDisposable() subscription = SerialDisposable() timer = SerialDisposable() subscription.disposable = original def create_timer(): my_id = _id def action(scheduler, state=None): nonlocal switched switched = (_id == my_id) timer_wins = switched if timer_wins: subscription.disposable = other.subscribe(observer) timer.disposable = scheduler_method(duetime, action) create_timer() def on_next(x): nonlocal _id on_next_wins = not switched if on_next_wins: _id += 1 observer.on_next(x) create_timer() def on_error(e): nonlocal _id on_error_wins = not switched if on_error_wins: _id += 1 observer.on_error(e) def on_completed(): nonlocal _id on_completed_wins = not switched if on_completed_wins: _id += 1 observer.on_completed() original.disposable = source.subscribe(on_next, on_error, on_completed) return CompositeDisposable(subscription, timer) return AnonymousObservable(subscribe)
def on_error(self, e): self.on_next(Observable.throw_exception(e))