Ejemplo n.º 1
0
 async def go():
     nonlocal result
     source = Observable.throw(error)
     try:
         result = await source
     except Exception as ex:
         result = ex
Ejemplo n.º 2
0
 async def go():
     nonlocal result
     source = Observable.throw(error)
     try:
         result = await source
     except Exception as ex:
         result = ex
Ejemplo n.º 3
0
    def test_retry_observable_retry_count_throws(self):
        scheduler1 = TestScheduler()
        xs = Observable.return_value(1).retry(3)
        xs.subscribe_(lambda x: _raise('ex'), scheduler=scheduler1)

        self.assertRaises(RxException, scheduler1.start)

        scheduler2 = TestScheduler()
        ys = Observable.throw('ex').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 = Observable.return_value(1).retry(100)
        zs.subscribe_(on_completed=lambda: _raise('ex'), scheduler=scheduler3)

        with pytest.raises(RxException):
            scheduler3.start()

        xss = Observable.create(lambda o: _raise('ex')).retry(100)
        with pytest.raises(Exception):
            xss.subscribe()
Ejemplo n.º 4
0
    def subscribe(observer, scheduler=None):
        try:
            result = observable_factory(scheduler)
        except Exception as ex:
            return Observable.throw(ex).subscribe(observer)

        result = Observable.from_future(result) if is_future(result) else result
        return result.subscribe(observer, scheduler)
Ejemplo n.º 5
0
    def test_map_throws(self):
        with self.assertRaises(RxException):
            Observable.return_value(1) \
                .map(mapper_indexed=lambda x, y: x) \
                .subscribe_(lambda x: _raise("ex"))

        with self.assertRaises(RxException):
            Observable.throw('ex') \
                .map(mapper_indexed=lambda x, y: x) \
                .subscribe_(on_error=lambda ex: _raise(ex))

        with self.assertRaises(RxException):
            Observable.empty() \
                .map(mapper_indexed=lambda x, y: x) \
                .subscribe_(lambda x: x, lambda ex: ex, lambda: _raise('ex'))

        def subscribe(observer, scheduler=None):
            _raise('ex')

        with self.assertRaises(RxException):
            Observable.create(subscribe) \
                .map(lambda x: x) \
                .subscribe()
Ejemplo n.º 6
0
    def test_repeat_observable_repeat_count_throws(self):
        scheduler1 = TestScheduler()
        xs = Observable.return_value(1).repeat(3)
        xs.subscribe_(lambda x: _raise('ex'), scheduler=scheduler1)

        with self.assertRaises(RxException):
            scheduler1.start()

        scheduler2 = TestScheduler()
        ys = Observable.throw('ex1').repeat(3)
        ys.subscribe_(on_error=lambda ex: _raise('ex2'), scheduler=scheduler2)

        with self.assertRaises(RxException):
            scheduler2.start()
Ejemplo n.º 7
0
    def subscribe(observer, scheduler=None):
        disposable = Disposable.empty()
        try:
            resource = resource_factory()
            if resource:
                disposable = resource

            source = observable_factory(resource)
        except Exception as exception:
            d = Observable.throw(exception).subscribe(observer, scheduler)
            return CompositeDisposable(d, disposable)

        return CompositeDisposable(source.subscribe(observer, scheduler),
                                   disposable)
Ejemplo n.º 8
0
        def go():
            error = Exception('woops')

            source = Observable.throw(error)
            future = source.to_future(asyncio.Future)

            def done(future):
                try:
                    future.result()
                except Exception as ex:
                    success[1] = str(ex) == str(error)
                else:
                    success[0] = False

            future.add_done_callback(done)
Ejemplo n.º 9
0
def start_async(function_async) -> ObservableBase:
    """Invokes the asynchronous function, surfacing the result through
    an observable sequence.

    Keyword arguments:
    function_async -- Asynchronous function which returns a Future to
        run.

    Returns an observable sequence exposing the function's result value,
    or an exception.
    """

    try:
        future = function_async()
    except Exception as ex:
        return Observable.throw(ex)

    return Observable.from_future(future)
Ejemplo n.º 10
0
def start_async(cls, function_async):
    """Invokes the asynchronous function, surfacing the result through an
    observable sequence.

    Keyword arguments:
    :param types.FunctionType function_async: Asynchronous function which
        returns a Future to run.

    :returns: An observable sequence exposing the function's result value, or an
        exception.
    :rtype: Observable
    """

    try:
        future = function_async()
    except Exception as ex:
        return Observable.throw(ex)

    return Observable.from_future(future)
Ejemplo n.º 11
0
    def test_map_with_index_throws(self):
        with self.assertRaises(RxException):
            return Observable.return_value(1) \
                .map(mapper_indexed=lambda x, index: x) \
                .subscribe_(lambda x: _raise('ex'))

        with self.assertRaises(RxException):
            return Observable.throw('ex') \
                .map(mapper_indexed=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(mapper_indexed=lambda x, index: x) \
                .subscribe()
Ejemplo n.º 12
0
    def test_retry_observable_throws(self):
        scheduler1 = TestScheduler()
        xs = Observable.return_value(1).retry()
        xs.subscribe_(lambda x: _raise('ex'), scheduler=scheduler1)

        with pytest.raises(RxException):
            scheduler1.start()

        scheduler2 = TestScheduler()
        ys = Observable.throw('ex').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 = Observable.return_value(1).retry()
        zs.subscribe_(on_completed=lambda: _raise('ex'), scheduler=scheduler3)

        with pytest.raises(RxException):
            scheduler3.start()
Ejemplo n.º 13
0
 def create():
     return Observable.throw(ex).time_interval()
Ejemplo n.º 14
0
 def test_for_each_index_throws(self):
     ex = Exception()
     xs = Observable.throw(ex)
     self.assertRaises(Exception, lambda:xs.to_blocking().for_each(lambda x, i: _raise(ex)))
Ejemplo n.º 15
0
def timeout(source: ObservableBase,
            duetime: Union[int, datetime],
            other: ObservableBase = None) -> ObservableBase:
    """Returns the source observable sequence or the other observable
    sequence if duetime elapses.

    1 - res = source.timeout(5000); # 5 seconds
    # As a date and timeout observable
    2 - res = source.timeout(datetime(), Observable.return_value(42))
    # 5 seconds and timeout observable
    3 - res = source.timeout(5000, Observable.return_value(42))
    # As a date and timeout observable

    Keyword arguments:
    duetime -- Absolute (specified as a datetime object) or relative
        time (specified as an integer denoting milliseconds) when a
        timeout occurs.
    other -- Sequence to return in case of a timeout. If not
        specified, a timeout error throwing sequence will be used.

    Returns the source sequence switching to the other sequence in case
        of a timeout.
    """

    other = other or Observable.throw(Exception("Timeout"))
    other = Observable.from_future(other) if is_future(other) else other

    def subscribe(observer, scheduler=None):
        scheduler = scheduler or timeout_scheduler

        if isinstance(duetime, datetime):
            scheduler_method = scheduler.schedule_absolute
        else:
            scheduler_method = scheduler.schedule_relative

        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, scheduler)

            timer.disposable = scheduler_method(duetime, action)

        create_timer()

        def on_next(value):
            send_wins = not switched[0]
            if send_wins:
                _id[0] += 1
                observer.on_next(value)
                create_timer()

        def on_error(error):
            on_error_wins = not switched[0]
            if on_error_wins:
                _id[0] += 1
                observer.on_error(error)

        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, scheduler)
        return CompositeDisposable(subscription, timer)

    return AnonymousObservable(subscribe)
Ejemplo n.º 16
0
 def test_for_each_index_throws(self):
     ex = Exception()
     xs = Observable.throw(ex)
     self.assertRaises(
         Exception, lambda: xs.to_blocking().for_each(lambda x: _raise(ex)))
Ejemplo n.º 17
0
 def on_error(self, error):
     self.on_next(Observable.throw(error))
Ejemplo n.º 18
0
def timeout_with_selector(source,
                          first_timeout=None,
                          timeout_duration_mapper=None,
                          other=None) -> ObservableBase:
    """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_mapper -- [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().

    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('Timeout'))

    def subscribe(observer, scheduler=None):
        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, scheduler)

                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,
                                              scheduler)

        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_mapper(x)
                except Exception as e:
                    observer.on_error(e)
                    return

                set_timer(timeout)

        def on_error(error):
            if observer_wins():
                observer.on_error(error)

        def on_completed():
            if observer_wins():
                observer.on_completed()

        original.disposable = source.subscribe_(on_next, on_error,
                                                on_completed, scheduler)
        return CompositeDisposable(subscription, timer)

    return AnonymousObservable(subscribe)
Ejemplo n.º 19
0
 def closing():
     return Observable.throw(ex)