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()
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_for_each_index_return(self): lstX = [] lstI = [] def action(x, i): lstX.append(x) lstI.append(i) Observable.return_value(42).to_blocking().for_each(action) assert (lstX == [42]) assert (lstI == [0])
def test_for_each_index_return(self): lstX = [] lstI = [] def action(x, i): lstX.append(x) lstI.append(i) Observable.return_value(42).to_blocking().for_each(action) assert(lstX == [42]) assert(lstI == [0])
def test_return_observer_throws(self): scheduler1 = TestScheduler() xs = Observable.return_value(1, scheduler1) xs.subscribe(lambda x: _raise('ex')) self.assertRaises(RxException, scheduler1.start) scheduler2 = TestScheduler() ys = Observable.return_value(1, scheduler2) ys.subscribe(lambda x: x, lambda ex: ex, lambda: _raise('ex')) self.assertRaises(RxException, scheduler2.start)
def repeat(cls, value=None, repeat_count=None, scheduler=None): """Generates an observable sequence that repeats the given element the specified number of times, using the specified scheduler to send out observer messages. 1 - res = rx.Observable.repeat(42) 2 - res = rx.Observable.repeat(42, 4) 3 - res = rx.Observable.repeat(42, 4, Rx.Scheduler.timeout) 4 - res = rx.Observable.repeat(42, None, Rx.Scheduler.timeout) Keyword arguments: value -- Element to repeat. repeat_count -- [Optional] Number of times to repeat the element. If not specified, repeats indefinitely. scheduler -- Scheduler to run the producer loop on. If not specified, defaults to ImmediateScheduler. Returns an observable sequence that repeats the given element the specified number of times.""" scheduler = scheduler or current_thread_scheduler if repeat_count == -1: repeat_count = None xs = Observable.return_value(value, scheduler) return xs.repeat(repeat_count)
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 = Observable.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_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 = Observable.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_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)
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()
def go(): source = Observable.return_value(42) future = source.to_future(asyncio.Future) def done(future): try: value = future.result() except Exception: success[1] = False else: success[0] = value == 42 future.add_done_callback(done)
def test_connectable_observable_creation(self): y = [0] s2 = Subject() co2 = ConnectableObservable(Observable.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 = 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()
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()
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 repeat_value(value: Any = None, repeat_count: int = None) -> ObservableBase: """Generates an observable sequence that repeats the given element the specified number of times. 1 - res = repeat_value(42) 2 - res = repeat_value(42, 4) Keyword arguments: 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 = Observable.return_value(value) return xs.repeat(repeat_count)
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_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()
def test_return_disposed_after_next(self): scheduler = TestScheduler() d = SerialDisposable() xs = Observable.return_value(42, scheduler) 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) return d.disposable scheduler.schedule_absolute(100, action) scheduler.start() results.messages.assert_equal(on_next(101, 42))
def test_for_each_return(self): lst = [] Observable.return_value(42).to_blocking().for_each( lambda x: lst.append(x)) assert (lst == [42])
async def go(): nonlocal result source = Observable.return_value(42) result = await source
def factory(): return Observable.return_value(42, scheduler)
def prep(p, *args): return Observable.return_value(p).delay(delays.get(p))
def test_for_each_return(self): lst = [] Observable.return_value(42).to_blocking().for_each(lambda x: lst.append(x)) assert(lst == [42])
def factory(): return Observable.return_value(42)