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_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()
Exemple #3
0
    def test_starmap_subscription_error(self):
        mapper = ops.starmap(lambda x, y: (x, y))

        with self.assertRaises(RxException):
            return_value((1, 10)).pipe(
                mapper
            ).subscribe(lambda x: _raise("ex"))

        with self.assertRaises(RxException):
            throw('ex').pipe(
                mapper
            ).subscribe(on_error=lambda ex: _raise(ex))

        with self.assertRaises(RxException):
            empty().pipe(
                mapper
            ).subscribe(lambda x: x, lambda ex: ex, lambda: _raise('ex'))

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

        with self.assertRaises(RxException):
            create(subscribe).pipe(
                mapper
            ).subscribe()
Exemple #4
0
def select_flow(context: Context) -> Observable:
    response_type = next(iter(context.oauth2_request.response_type))
    if response_type == 'code':
        return return_value(context).pipe(
            op.flat_map(call_async(authorization_code_grant)))
    if response_type == 'token':
        return return_value(context).pipe(
            op.flat_map(call_async(implicit_grant)))
    raise InvalidRequest('Invalid response_type parameter')
    def test_return_observer_throws(self):
        scheduler1 = TestScheduler()
        xs = rx.return_value(1)
        xs.subscribe(lambda x: _raise('ex'), scheduler=scheduler1)

        self.assertRaises(RxException, scheduler1.start)

        scheduler2 = TestScheduler()
        ys = rx.return_value(1)
        ys.subscribe(lambda x: x,
                     lambda ex: ex,
                     lambda: _raise('ex'),
                     scheduler=scheduler2)

        self.assertRaises(RxException, scheduler2.start)
    def test_return_disposed_after_next(self):
        scheduler = TestScheduler()
        d = SerialDisposable()
        xs = rx.return_value(42)
        results = scheduler.create_observer()

        def action(scheduler, state):
            def on_next(x):
                d.dispose()
                results.on_next(x)

            def on_error(e):
                results.on_error(e)

            def on_completed():
                results.on_completed()

            d.disposable = xs.subscribe(on_next,
                                        on_error,
                                        on_completed,
                                        scheduler=scheduler)
            return d.disposable

        scheduler.schedule_absolute(100, action)
        scheduler.start()
        assert results.messages == [on_next(100, 42)]
Exemple #7
0
 async def go():
     source = rx.return_value(42)
     fut = next(source.__await__())
     # This used to raise an InvalidStateError before we got
     # support for cancellation.
     fut.cancel()
     await fut
Exemple #8
0
def test_subject_create():
    _x = [None]
    _ex = [None]
    done = False

    def on_next(x):
        _x[0] = x

    def on_error(ex):
        _ex[0] = ex

    def on_completed():
        done = True

    v = AnonymousObserver(on_next, on_error, on_completed)

    o = rx.return_value(42)

    s = Subject.create(v, o)

    def on_next2(x):
        _x[0] = x

    s.subscribe(on_next2)

    assert (42 == _x[0])
    s.on_next(21)

    e = 'ex'
    s.on_error(e)

    assert (e == _ex[0])

    s.on_completed()
    assert (not done)
Exemple #9
0
def get_authorization_stream(context: Context) -> Observable:
    return return_value(context).pipe(
        op.flat_map(call_async(validate_redirect_uri)),
        op.flat_map(call_async(validate_response_type)),
        op.flat_map(call_async(validate_scope)),
        op.flat_map(select_flow),
    )
def test_subject_create():
    _x = [None]
    _ex = [None]
    done = False

    def on_next(x):
        _x[0] = x

    def on_error(ex):
        _ex[0] = ex

    def on_completed():
        done = True

    v = AnonymousObserver(on_next, on_error, on_completed)

    o = rx.return_value(42)

    s = Subject.create(v, o)

    def on_next2(x):
        _x[0] = x
    s.subscribe(on_next2)

    assert(42 == _x[0])
    s.on_next(21)

    e = 'ex'
    s.on_error(e)

    assert(e == _ex[0])

    s.on_completed()
    assert(not done)
    def test_map_throws(self):
        mapper = map(lambda x: x)
        with self.assertRaises(RxException):
            return_value(1).pipe(mapper).subscribe(lambda x: _raise("ex"))

        with self.assertRaises(RxException):
            throw('ex').pipe(mapper).subscribe(on_error=lambda ex: _raise(ex))

        with self.assertRaises(RxException):
            empty().pipe(mapper).subscribe(lambda x: x, lambda ex: ex,
                                           lambda: _raise('ex'))

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

        with self.assertRaises(RxException):
            create(subscribe).pipe(map(lambda x: x)).subscribe()
 def _wait_for_order_fill(self,
                          market: Market,
                          client_id: int,
                          max_wait_in_seconds: int = 60):
     self.logger.info(
         f"Waiting up to {max_wait_in_seconds} seconds for {client_id}.")
     return rx.interval(1.0).pipe(
         ops.flat_map(lambda _: market.load_event_queue()),
         ops.skip_while(lambda item: item.client_order_id != client_id),
         ops.skip_while(lambda item: not item.event_flags.fill),
         ops.first(), ops.map(lambda _: True),
         ops.timeout(max_wait_in_seconds, rx.return_value(False))).run()
    def test_repeat_observable_throws(self):
        scheduler1 = TestScheduler()
        xs = rx.return_value(11).pipe(ops.repeat())
        xs.subscribe(lambda x: _raise('ex'), scheduler=scheduler1)

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

        scheduler2 = TestScheduler()
        ys = rx.throw('ex').pipe(ops.repeat())
        ys.subscribe(lambda ex: _raise('ex'), scheduler=scheduler2)

        with self.assertRaises(Exception):
            scheduler2.start()

        scheduler3 = TestScheduler()
        zs = rx.return_value(1).pipe(ops.repeat())
        d = zs.subscribe(on_completed=lambda: _raise('ex'), scheduler=scheduler3)

        scheduler3.schedule_absolute(210, lambda sc, st: d.dispose())
        scheduler3.start()
    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_connectable_observable_creation(self):
        y = [0]

        s2 = Subject()
        co2 = ConnectableObservable(rx.return_value(1), s2)

        def on_next(x):
            y[0] = x
        co2.subscribe(on_next=on_next)
        self.assertNotEqual(1, y[0])

        co2.connect()
        self.assertEqual(1, y[0])
    def test_repeat_observable_repeat_count_throws(self):
        scheduler1 = TestScheduler()
        xs = rx.return_value(1).pipe(ops.repeat(3))
        xs.subscribe(lambda x: _raise('ex'), scheduler=scheduler1)

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

        scheduler2 = TestScheduler()
        ys = rx.throw('ex1').pipe(ops.repeat(3))
        ys.subscribe(on_error=lambda ex: _raise('ex2'), scheduler=scheduler2)

        with self.assertRaises(RxException):
            scheduler2.start()
    def test_repeat_observable_repeat_count_throws(self):
        scheduler1 = TestScheduler()
        xs = rx.return_value(1).pipe(ops.repeat(3))
        xs.subscribe(lambda x: _raise('ex'), scheduler=scheduler1)

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

        scheduler2 = TestScheduler()
        ys = rx.throw('ex1').pipe(ops.repeat(3))
        ys.subscribe(on_error=lambda ex: _raise('ex2'), scheduler=scheduler2)

        with self.assertRaises(RxException):
            scheduler2.start()
    def test_map_with_index_throws(self):
        with self.assertRaises(RxException):
            mapper = map_indexed(lambda x, index: x)

            return return_value(1).pipe(mapper).subscribe(
                lambda x: _raise('ex'))

        with self.assertRaises(RxException):
            return throw('ex').pipe(mapper).subscribe(lambda x: x,
                                                      lambda ex: _raise(ex))

        with self.assertRaises(RxException):
            return empty().pipe(mapper).subscribe(lambda x: x, lambda ex: None,
                                                  lambda: _raise('ex'))

        with self.assertRaises(RxException):
            return create(lambda o, s: _raise('ex')).pipe(mapper).subscribe()
Exemple #19
0
    def test_map_with_index_throws(self):
        with self.assertRaises(RxException):
            mapper = map_indexed(lambda x, index: x)

            return return_value(1).pipe(
                mapper
            ).subscribe(lambda x: _raise('ex'))

        with self.assertRaises(RxException):
            return throw('ex').pipe(
                mapper
            ).subscribe(lambda x: x, lambda ex: _raise(ex))

        with self.assertRaises(RxException):
            return empty().pipe(
                mapper
            ).subscribe(lambda x: x, lambda ex: None, lambda: _raise('ex'))

        with self.assertRaises(RxException):
            return create(lambda o, s: _raise('ex')).pipe(
                mapper
            ).subscribe()
Exemple #20
0
def _repeat_value(value: Any = None, repeat_count: Optional[int] = None) -> Observable:
    """Generates an observable sequence that repeats the given element
    the specified number of times.

    Examples:
        1 - res = repeat_value(42)
        2 - res = repeat_value(42, 4)

    Args:
        value: Element to repeat.
        repeat_count: [Optional] Number of times to repeat the element.
            If not specified, repeats indefinitely.

    Returns:
        An observable sequence that repeats the given element the
        specified number of times.
    """

    if repeat_count == -1:
        repeat_count = None

    xs = rx.return_value(value)
    return xs.pipe(ops.repeat(repeat_count))
Exemple #21
0
def _repeat_value(value: Any = None, repeat_count: int = None) -> Observable:
    """Generates an observable sequence that repeats the given element
    the specified number of times.

    Examples:
        1 - res = repeat_value(42)
        2 - res = repeat_value(42, 4)

    Args:
        value: Element to repeat.
        repeat_count: [Optional] Number of times to repeat the element.
            If not specified, repeats indefinitely.

    Returns:
        An observable sequence that repeats the given element the
        specified number of times.
    """

    if repeat_count == -1:
        repeat_count = None

    xs = rx.return_value(value)
    return xs.pipe(ops.repeat(repeat_count))
Exemple #22
0
            def mapper(ys, i):
                def proj(y):
                    return "%s %s" % (i, y)

                return ys.pipe(ops.map(proj), ops.concat(rx.return_value('%s end' % i)))
Exemple #23
0
def get_token_stream(context: Context) -> Observable:
    return return_value(context).pipe(
        op.map(validate_grant_type),
        op.flat_map(call_async(validate_code)),
        op.flat_map(call_async(get_token_by_code)),
    )
Exemple #24
0
 def create_stub_stream(context):
     return return_value(context).pipe(
         op.map(lambda x: StubResponse(test='passed')))
 async def go():
     nonlocal result
     source = rx.return_value(42).pipe(ops.delay(0.1))
     result = await source
 async def go():
     nonlocal result
     source = rx.return_value(42)
     result = await source
 def factory():
     return rx.return_value(42)
 async def go():
     nonlocal result
     source = rx.return_value(42)
     result = await source