Ejemplo n.º 1
0
 def test_select_disposeinsideselector(self):
     scheduler = TestScheduler()
     xs = scheduler.create_hot_observable(on_next(100, 1), on_next(200, 2), on_next(500, 3), on_next(600, 4))
     results = scheduler.create_observer()
     d = SerialDisposable()
     invoked = [0]
     
     def projection(x, *args, **kw):
         invoked[0] += 1
         
         if scheduler.clock > 400:
             #print("*** Dispose ****")
             d.dispose()
         return x
 
     d.disposable = xs.select(projection).subscribe(results)
 
     def action(scheduler, state):
         return d.dispose()
 
     scheduler.schedule_absolute(ReactiveTest.disposed, action)
     scheduler.start()
     
     results.messages.assert_equal(on_next(100, 1), on_next(200, 2))
     xs.subscriptions.assert_equal(ReactiveTest.subscribe(0, 500))
     
     assert invoked[0] == 3
Ejemplo n.º 2
0
    def test_interval_timespan_observer_throws(self):
        scheduler = TestScheduler()
        xs = Observable.interval(1, scheduler=scheduler)
        xs.subscribe(lambda x: _raise("ex"))

        with self.assertRaises(RxException):
            scheduler.start()
Ejemplo n.º 3
0
    def test_multicast_hot_21(self):
        c = [None]
        d1 = [None]
        d2 = [None]
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(on_next(40, 0), on_next(90, 1), on_next(150, 2), on_next(210, 3), on_next(240, 4), on_next(270, 5), on_next(330, 6), on_next(340, 7), on_completed(390))
        s = Subject()
        o = scheduler.create_observer()

        def action0(scheduler, state):
            c[0] = xs.multicast(s)
        scheduler.schedule_absolute(50, action0)

        def action1(scheduler, state):
            d2[0] = c[0].connect()
        scheduler.schedule_absolute(100, action1)

        def action2(scheduler, state):
            d1[0] = c[0].subscribe(o)
        scheduler.schedule_absolute(200, action2)

        def action3(scheduler, state):
            return d1[0].dispose()
        scheduler.schedule_absolute(300, action3)

        scheduler.start()
        o.messages.assert_equal(on_next(210, 3), on_next(240, 4), on_next(270, 5))
        xs.subscriptions.assert_equal(subscribe(100, 390))
Ejemplo n.º 4
0
    def test_multicast_hot_6(self):
        c = [None]
        d1 = [None]
        d2 = [None]
        ex = 'ex'
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(on_next(40, 0), on_next(90, 1), on_next(150, 2), on_next(210, 3), on_next(240, 4), on_next(270, 5), on_next(330, 6), on_next(340, 7), on_completed(390))
        s = Subject()
        o = scheduler.create_observer()

        def action0(scheduler, state):
            c[0] = xs.pipe(ops.multicast(s))
        scheduler.schedule_absolute(50, action0)

        def action1(scheduler, state):
            d2[0] = c[0].connect(scheduler)
        scheduler.schedule_absolute(100, action1)

        def action2(scheduler, state):
            d1[0] = c[0].subscribe(o, scheduler)
        scheduler.schedule_absolute(400, action2)

        scheduler.start()
        assert o.messages == [on_completed(400)]
        assert xs.subscriptions == [subscribe(100, 390)]
Ejemplo n.º 5
0
def test_select_with_index_dispose_inside_selector():
    scheduler = TestScheduler()
    xs = scheduler.create_hot_observable(on_next(100, 4), on_next(200, 3), on_next(500, 2), on_next(600, 1))
    invoked = 0
    results = scheduler.create_observer()
    d = SerialDisposable()
    
    def projection(x, index):
        nonlocal invoked
        invoked += 1
        if scheduler.clock > 400:
            d.dispose()
        
        return x + index * 10

    d.disposable = xs.select(projection).subscribe(results)

    def action(scheduler, state):
        return d.dispose()

    scheduler.schedule_absolute(disposed, action)
    scheduler.start()
    results.messages.assert_equal(on_next(100, 4), on_next(200, 13))
    xs.subscriptions.assert_equal(subscribe(0, 500))
    assert invoked == 3
Ejemplo n.º 6
0
    def test_where_index_dispose_in_predicate(self):
        scheduler = TestScheduler()
        ys = [None]
        invoked = [0]
        xs = scheduler.create_hot_observable(on_next(110, 1), on_next(180, 2), on_next(230, 3), on_next(270, 4), on_next(340, 5), on_next(380, 6), on_next(390, 7), on_next(450, 8), on_next(470, 9), on_next(560, 10), on_next(580, 11), on_completed(600), on_next(610, 12), on_error(620, 'ex'), on_completed(630))
        results = scheduler.create_observer()
        d = SerialDisposable()
    
        def action1(scheduler, state):
            def predicate(x, index):
                invoked[0] += 1
                if x == 8:
                    d.dispose()
            
                return is_prime(x + index * 10)
            ys[0] = xs.filter(predicate)

        scheduler.schedule_absolute(created, action1)
    
        def action2(scheduler, state):
             d.disposable = ys[0].subscribe(results)

        scheduler.schedule_absolute(subscribed, action2)
    
        def action3(scheduler, state):
            d.dispose()

        scheduler.schedule_absolute(disposed, action3)
    
        scheduler.start()
        results.messages.assert_equal(on_next(230, 3), on_next(390, 7))
        xs.subscriptions.assert_equal(subscribe(200, 450))
        assert(invoked[0] == 6)
Ejemplo n.º 7
0
    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()
Ejemplo n.º 8
0
    def test_map_disposeinsidemapper(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(on_next(100, 1), on_next(200, 2), on_next(500, 3), on_next(600, 4))
        results = scheduler.create_observer()
        d = SerialDisposable()
        invoked = [0]

        def projection(x, *args, **kw):
            invoked[0] += 1

            if scheduler.clock > 400:
                d.dispose()
            return x

        d.disposable = xs.pipe(
            map(projection)
        ).subscribe(results, scheduler)

        def action(scheduler, state):
            return d.dispose()

        scheduler.schedule_absolute(ReactiveTest.disposed, action)
        scheduler.start()

        assert results.messages == [on_next(100, 1), on_next(200, 2)]
        assert xs.subscriptions == [ReactiveTest.subscribe(0, 500)]

        assert invoked[0] == 3
Ejemplo n.º 9
0
    def test_multicast_hot_5(self):
        c = [None]
        d1 = [None]
        d2 = [None]
        ex = 'ex'
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(on_next(40, 0), on_next(90, 1), on_next(150, 2), on_next(210, 3), on_next(240, 4), on_next(270, 5), on_next(330, 6), on_next(340, 7), on_error(390, ex))
        s = Subject()
        o = scheduler.create_observer()

        def action0(scheduler, state):
            c[0] = xs.multicast(s)
        scheduler.schedule_absolute(50, action0)

        def action1(scheduler, state):
            d2[0] = c[0].connect()
        scheduler.schedule_absolute(100, action1)

        def action2(scheduler, state):
            d1[0] = c[0].subscribe(o)
        scheduler.schedule_absolute(400, action2)

        scheduler.start()
        o.messages.assert_equal(on_error(400, ex))
        xs.subscriptions.assert_equal(subscribe(100, 390))
Ejemplo n.º 10
0
    def test_paused_with_immediate_unpause(self):
        subscription = [None]

        scheduler = TestScheduler()

        results = scheduler.create_observer()

        xs = scheduler.create_hot_observable(
            on_next(150, 1),
            on_next(210, 2),
            on_completed(500)
        )

        controller = Observable.just(True)

        pausable_buffered = xs.pausable_buffered(controller)

        def action1(scheduler, state):
            subscription[0] = pausable_buffered.subscribe(results)
        scheduler.schedule_absolute(200, action1)

        scheduler.start()

        results.messages.assert_equal(
            on_next(210, 2),
            on_completed(500)
        )
Ejemplo n.º 11
0
    def test_paused_skip_initial_elements(self):
        subscription = [None]
        scheduler = TestScheduler()

        controller = Subject()
        results = scheduler.create_observer()

        xs = scheduler.create_hot_observable(on_next(150, 1), on_next(230, 2), on_next(270, 3), on_completed(400))

        def action1(scheduler, state):
            subscription[0] = xs.pausable_buffered(controller).subscribe(results)
            controller.on_next(False)

        scheduler.schedule_absolute(200, action1)

        def action2(scheduler, state):
            controller.on_next(True)

        scheduler.schedule_absolute(280, action2)

        def action3(scheduler, state):
            subscription[0].dispose()

        scheduler.schedule_absolute(1000, action3)

        scheduler.start()
        results.messages.assert_equal(on_next(280, 2), on_next(280, 3), on_completed(400))
Ejemplo n.º 12
0
 def test_never_basic(self):
     scheduler = TestScheduler()
     xs = Observable.never()
     results = scheduler.create_observer()
     xs.subscribe(results)
     scheduler.start()
     results.messages.assert_equal()
Ejemplo n.º 13
0
    def test_paused_with_state_change_in_subscriber(self):
        scheduler = TestScheduler()

        results = scheduler.create_observer()

        xs = scheduler.create_hot_observable(
            on_next(150, 1), on_next(210, 2), on_next(250, 3), on_next(270, 4), on_next(330, 5), on_completed(500)
        )

        controller = Subject()

        pausable_buffered = xs.pausable_buffered(controller)

        def action1(scheduler, state):
            def on_next(value):
                results.on_next(value)
                controller.on_next(False)

                def action2(scheduler, state):
                    controller.on_next(True)

                scheduler.schedule_relative(100, action2)

            subscription = pausable_buffered.subscribe(on_next, results.on_error, results.on_completed)
            controller.on_next(True)

        scheduler.schedule_absolute(200, action1)

        scheduler.start()

        results.messages.assert_equal(
            on_next(210, 2), on_next(310, 3), on_next(310, 4), on_next(410, 5), on_completed(500)
        )
Ejemplo n.º 14
0
    def test_group_by_with_merge(self):
        scheduler = TestScheduler()

        xs = [None]
        results = [None]

        def action1(scheduler, state):
            xs[0] = rx.from_iterable(["alpha", "apple", "beta", "bat", "gamma"]) \
                .pipe(ops.group_by(lambda s: s[0]),
                      ops.map(lambda xs: xs.pipe(ops.to_iterable(), ops.map(list))),
                      ops.merge_all(),
                      )

        scheduler.schedule_absolute(created, action1)

        def action2(scheduler, state):
            results[0] = scheduler.create_observer()
            xs[0].subscribe(results[0], scheduler)
        scheduler.schedule_absolute(subscribed, action2)

        scheduler.start()

        assert results[0].messages == [
            on_next(200, ["alpha", "apple"]),
            on_next(200, ["beta", "bat"]),
            on_next(200, ["gamma"]),
            on_completed(200)]
def test_repeat_observable_repeat_count_throws():
    scheduler1 = TestScheduler()
    xs = Observable.return_value(1, scheduler1).repeat(3)
    xs.subscribe(lambda x: _raise('ex'))
    
    try:
        return scheduler1.start()
    except RxException:
        pass

    scheduler2 = TestScheduler()
    ys = Observable.throwException('ex1', scheduler2).repeat(3)
    ys.subscribe(lambda ex: _raise('ex2'))
    
    try:
        return scheduler2.start()
    except RxException:
        pass

    scheduler3 = TestScheduler()
    zs = Observable.return_value(1, scheduler3).repeat(100)
    d = zs.subscribe(on_complete=lambda: _raise('ex3'))
    
    scheduler3.schedule_absolute(10, lambda: d.dispose())
    
    scheduler3.start()
    xss = Observable.create(lambda o: _raise('ex4')).repeat(3)
    try:
        return xss.subscribe()
    except RxException:
        pass
Ejemplo n.º 16
0
    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
Ejemplo n.º 17
0
    def test_empty_observer_throw_exception(self):
        scheduler = TestScheduler()
        xs = empty()
        xs.subscribe(lambda x: None, lambda ex: None, lambda: _raise('ex'), scheduler=scheduler)

        with self.assertRaises(RxException):
            scheduler.start()
Ejemplo n.º 18
0
    def test_group_by_with_merge(self):
        scheduler = TestScheduler()

        xs = [None]
        results = [None]

        def action1(scheduler, state):
            xs[0] = Observable.from_(["alpha", "apple", "beta", "bat", "gamma"]) \
                              .group_by(lambda s: s[0]) \
                              .map(lambda group: group.to_list()) \
                              .merge_all()
        scheduler.schedule_absolute(created, action1)

        def action2(scheduler, state):
            results[0] = scheduler.create_observer()
            xs[0].subscribe(results[0])
        scheduler.schedule_absolute(subscribed, action2)

        scheduler.start()

        results[0].messages.assert_equal(
            on_next(200, ["alpha", "apple"]),
            on_next(200, ["beta", "bat"]),
            on_next(200, ["gamma"]),
            on_completed(200))
Ejemplo n.º 19
0
    def test_range_double_subscribe(self):
        scheduler = TestScheduler()
        obs = Observable.range(1, 3)

        results = scheduler.start(lambda: obs)
        results.messages.assert_equal(on_next(200, 1), on_next(200, 2), on_next(200, 3), on_completed(200))

        results = scheduler.start(lambda: obs)
        results.messages.assert_equal(on_next(1001, 1), on_next(1001, 2), on_next(1001, 3), on_completed(1001))
Ejemplo n.º 20
0
    def test_double_subscribe_to_iterable(self):
        iterable_finite = [1, 2, 3]
        scheduler = TestScheduler()
        obs = Observable.from_(iterable_finite)

        results = scheduler.start(lambda: obs)
        results.messages.assert_equal(on_next(200, 1), on_next(200, 2), on_next(200, 3), on_completed(200))

        results = scheduler.start(lambda: obs)
        results.messages.assert_equal(on_next(1001, 1), on_next(1001, 2), on_next(1001, 3), on_completed(1001))
Ejemplo n.º 21
0
    def test_paused_with_observable_controller_and_pause_and_unpause(self):
        subscription = [None]

        scheduler = TestScheduler()

        results = scheduler.create_observer()

        xs = scheduler.create_hot_observable(
            on_next(150, 1),
            on_next(210, 2),
            on_next(230, 3),
            on_next(301, 4),
            on_next(350, 5),
            on_next(399, 6),
            on_next(450, 7),
            on_next(470, 8),
            on_completed(500),
        )

        controller = scheduler.create_hot_observable(on_next(201, True), on_next(300, False), on_next(400, True))

        pausable_buffered = xs.pausable_buffered(controller)

        def action1(scheduler, state):
            subscription[0] = pausable_buffered.subscribe(results)

        scheduler.schedule_absolute(200, action1)

        def action2(scheduler, state):
            pausable_buffered.pause()

        scheduler.schedule_absolute(460, action2)

        def action3(scheduler, state):
            pausable_buffered.resume()

        scheduler.schedule_absolute(480, action3)

        def action4(scheduler, state):
            subscription[0].dispose()

        scheduler.schedule_absolute(1000, action4)

        scheduler.start()

        results.messages.assert_equal(
            on_next(210, 2),
            on_next(230, 3),
            on_next(400, 4),
            on_next(400, 5),
            on_next(400, 6),
            on_next(450, 7),
            on_next(480, 8),
            on_completed(500),
        )
Ejemplo n.º 22
0
    def test_do_action_without_next(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(on_next(150, 1), on_next(210, 2),  on_completed(250))
        completed = [False]
        def create():
            def on_completed():
                completed[0] = True
            return xs.do_action(on_completed=on_completed)

        scheduler.start(create)

        assert(completed[0])
Ejemplo n.º 23
0
def test_group_by_inner_error():
    ex = 'ex1'
    scheduler = TestScheduler()
    xs = scheduler.create_hot_observable(on_next(90, "error"), on_next(110, "error"), on_next(130, "error"), on_next(220, "  foo"), on_next(240, " FoO "), on_next(270, "baR  "), on_next(310, "foO "), on_next(350, " Baz   "), on_next(360, "  qux "), on_next(390, "   bar"), on_next(420, " BAR  "), on_next(470, "FOO "), on_next(480, "baz  "), on_next(510, " bAZ "), on_next(530, "    fOo    "), on_error(570, ex), on_next(580, "error"), on_completed(600), on_error(650, 'ex'))
    outer_subscription = None
    inner_subscriptions = {}
    inners = {}
    outer = None
    results = {}
    result = None

    def action1(scheduler, state):
        nonlocal outer
        outer = xs.group_by(
            lambda x: x.lower().strip(), 
            lambda x: x[::-1]
        )
        return outer
    scheduler.schedule_absolute(created, action1)

    def action2(scheduler, state):
        nonlocal outer_subscription

        def on_next(group):
            nonlocal result, inners, results

            result = scheduler.create_observer()
            inners[group.key] = group
            results[group.key] = result

            def action3(scheduler, state):
                nonlocal inner_subscriptions
                inner_subscriptions[group.key] = group.subscribe(result)
            
            scheduler.schedule_relative(100, action3)
        outer_subscription = outer.subscribe(on_next, lambda e: None)
        return outer_subscription
    scheduler.schedule_absolute(subscribed, action2)

    def action4(scheduler, state):
        outer_subscription.dispose();
        for sub in inner_subscriptions.values():
            sub.dispose()
    scheduler.schedule_absolute(disposed, action4)

    scheduler.start()
    assert(len(inners) == 4)
    #results['foo'].messages.assert_equal(on_next(470, " OOF"), on_next(530, "    oOf    "), on_error(570, ex))
    #results['bar'].messages.assert_equal(on_next(390, "rab   "), on_next(420, "  RAB "), on_error(570, ex))
    #results['baz'].messages.assert_equal(on_next(480, "  zab"), on_next(510, " ZAb "), on_error(570, ex))
    #results['qux'].messages.assert_equal(on_error(570, ex))
    xs.subscriptions.assert_equal(subscribe(200, 570))
Ejemplo n.º 24
0
    def test_do_plain_action(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(on_next(150, 1), on_next(210, 2), on_next(220, 3), on_next(230, 4), on_next(240, 5), on_completed(250))
        i = [0]

        def create():
            def action(x):
                i[0] += 1
                return i[0]
            return xs.do_action(action)
        scheduler.start(create)

        self.assertEqual(4, i[0])
Ejemplo n.º 25
0
    def test_partition_completed(self):
        scheduler = TestScheduler()

        xs = scheduler.create_hot_observable(
            on_next(180, 5),
            on_next(210, 4),
            on_next(240, 3),
            on_next(290, 2),
            on_next(350, 1),
            on_completed(360)
        )

        observables = []
        subscription1 = [None]
        subscription2 = [None]
        results1 = scheduler.create_observer()
        results2 = scheduler.create_observer()

        def action0(scheduler, state):
            observables.extend(xs.partition(is_even))

        scheduler.schedule_absolute(ReactiveTest.created, action0)

        def action1(scheduler, state):
            subscription1[0] = observables[0].subscribe(results1)
            subscription2[0] = observables[1].subscribe(results2)
        scheduler.schedule_absolute(ReactiveTest.subscribed, action1)

        def action2(scheduler, state):
            subscription1[0].dispose()
            subscription2[0].dispose()
        scheduler.schedule_absolute(ReactiveTest.disposed, action2)

        scheduler.start()

        results1.messages.assert_equal(
            on_next(210, 4),
            on_next(290, 2),
            on_completed(360)
        )

        results2.messages.assert_equal(
            on_next(240, 3),
            on_next(350, 1),
            on_completed(360)
        )

        xs.subscriptions.assert_equal(
            subscribe(200, 360)
        )
Ejemplo n.º 26
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()
Ejemplo n.º 27
0
    def test_replay_time_basic(self):
        subscription = [None]
        connection = [None]
        ys = [None]
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(on_next(110, 7), on_next(220, 3), on_next(280, 4), on_next(290, 1), on_next(340, 8), on_next(360, 5), on_next(
            370, 6), on_next(390, 7), on_next(410, 13), on_next(430, 2), on_next(450, 9), on_next(520, 11), on_next(560, 20), on_completed(600))
        results = scheduler.create_observer()

        def action0(scheduler, state):
            ys[0] = xs.pipe(ops.replay(window=150, scheduler=scheduler))
        scheduler.schedule_absolute(created, action0)

        def action1(scheduler, state):
            subscription[0] = ys[0].subscribe(results)
        scheduler.schedule_absolute(450, action1)

        def action2(scheduler, state):
            subscription[0].dispose()
        scheduler.schedule_absolute(disposed, action2)

        def action3(scheduler, state):
            connection[0] = ys[0].connect(scheduler)
        scheduler.schedule_absolute(300, action3)

        def action4(scheduler, state):
            connection[0].dispose()
        scheduler.schedule_absolute(400, action4)

        def action5(scheduler, state):
            connection[0] = ys[0].connect(scheduler)
        scheduler.schedule_absolute(500, action5)

        def action6(scheduler, state):
            connection[0].dispose()
        scheduler.schedule_absolute(550, action6)

        def action7(scheduler, state):
            connection[0] = ys[0].connect(scheduler)
        scheduler.schedule_absolute(650, action7)

        def action8(scheduler, state):
            connection[0].dispose()
        scheduler.schedule_absolute(800, action8)

        scheduler.start()
        assert results.messages == [on_next(450, 8), on_next(
            450, 5), on_next(450, 6), on_next(450, 7), on_next(520, 11)]
        assert xs.subscriptions == [subscribe(300, 400), subscribe(500, 550), subscribe(650, 800)]
Ejemplo n.º 28
0
    def test_timeout_duration_simple_never(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(310, 1),
            on_next(350, 2),
            on_next(420, 3),
            on_completed(450)
        )
        ys = scheduler.create_cold_observable()

        def create():
            return xs.timeout_with_selector(ys, lambda _: ys)

        results = scheduler.start(create)

        results.messages.assert_equal(
            on_next(310, 1),
            on_next(350, 2),
            on_next(420, 3),
            on_completed(450)
        )
        xs.subscriptions.assert_equal(
            subscribe(200, 450)
        )
        ys.subscriptions.assert_equal(
            subscribe(200, 310),
            subscribe(310, 350),
            subscribe(350, 420),
            subscribe(420, 450)
        )
Ejemplo n.º 29
0
    def test_timeout_duration_simple_timeoutbycompletion(self):
        ex = 'ex'
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(on_next(310, 1), on_next(350, 2), on_next(420, 3), on_completed(450))
        ys = scheduler.create_cold_observable()
        zs = scheduler.create_cold_observable()

        def create():
            def selector(x):
                if x < 3:
                    return zs
                else:
                    raise Exception(ex)

            return xs.timeout_with_selector(ys, selector)

        results = scheduler.start(create)

        results.messages.assert_equal(
            on_next(310, 1),
            on_next(350, 2),
            on_next(420, 3),
            on_error(420, ex)
        )
        xs.subscriptions.assert_equal(subscribe(200, 420))
        ys.subscriptions.assert_equal(subscribe(200, 310))
        zs.subscriptions.assert_equal(subscribe(310, 350), subscribe(350, 420))
Ejemplo n.º 30
0
    def test_interval_timespan_negative(self):
        scheduler = TestScheduler()
        def create():
            return Observable.interval(-1, scheduler=scheduler)

        results = scheduler.start(create, disposed=210)
        results.messages.assert_equal(on_next(201, 0), on_next(202, 1), on_next(203, 2), on_next(204, 3), on_next(205, 4), on_next(206, 5), on_next(207, 6), on_next(208, 7), on_next(209, 8))
Ejemplo n.º 31
0
    def test_group_join_op_normal_ii(self):
        scheduler = TestScheduler()

        xs = scheduler.create_hot_observable(
            on_next(210, TimeInterval(0, 10)), on_next(219, TimeInterval(1,
                                                                         5)),
            on_next(240, TimeInterval(2, 10)),
            on_next(300, TimeInterval(3, 100)),
            on_next(310, TimeInterval(4, 80)), on_next(500,
                                                       TimeInterval(5, 90)),
            on_next(700, TimeInterval(6, 25)),
            on_next(710, TimeInterval(7, 200)),
            on_next(720, TimeInterval(8, 100)), on_completed(721))

        ys = scheduler.create_hot_observable(
            on_next(215, TimeInterval("hat", (20))),
            on_next(217, TimeInterval("bat", (1))),
            on_next(290, TimeInterval("wag", (200))),
            on_next(300, TimeInterval("pig", (10))),
            on_next(305, TimeInterval("cup", (50))),
            on_next(600, TimeInterval("yak", (90))),
            on_next(702, TimeInterval("tin", (20))),
            on_next(712, TimeInterval("man", (10))),
            on_next(722, TimeInterval("rat", (200))),
            on_next(732, TimeInterval("wig", (5))), on_completed(990))

        xsd = []
        ysd = []

        def create():
            def mapper(x_yy):
                x, yy = x_yy
                return yy.pipe(
                    ops.map(lambda y: '{}{}'.format(x.value, y.value)))

            return xs.pipe(
                ops.group_join(
                    ys,
                    lambda x: new_timer(xsd, x.interval, scheduler),
                    lambda y: new_timer(ysd, y.interval, scheduler),
                ),
                ops.flat_map(mapper),
            )

        res = scheduler.start(create=create)

        assert res.messages == [
            on_next(215, "0hat"),
            on_next(217, "0bat"),
            on_next(219, "1hat"),
            on_next(300, "3wag"),
            on_next(300, "3pig"),
            on_next(305, "3cup"),
            on_next(310, "4wag"),
            on_next(310, "4pig"),
            on_next(310, "4cup"),
            on_next(702, "6tin"),
            on_next(710, "7tin"),
            on_next(712, "6man"),
            on_next(712, "7man"),
            on_next(720, "8tin"),
            on_next(720, "8man"),
            on_next(722, "6rat"),
            on_next(722, "7rat"),
            on_next(722, "8rat"),
            on_next(732, "7wig"),
            on_next(732, "8wig"),
            on_completed(910)
        ]

        assert xs.subscriptions == [subscribe(200, 721)]

        assert ys.subscriptions == [subscribe(200, 910)]
def test_infinite():
    scheduler = TestScheduler()

    xs = scheduler.create_hot_observable(on_next(70, 1), on_next(110, 2),
                                         on_next(220, 3), on_next(270, 4),
                                         on_next(340, 5), on_next(410, 6),
                                         on_next(520, 7), on_next(630, 8),
                                         on_next(710, 9), on_next(870, 10),
                                         on_next(940, 11), on_next(1020, 12))

    subject = None
    subscription = None
    subscription1 = None
    subscription2 = None
    subscription3 = None

    results1 = scheduler.create_observer()
    results2 = scheduler.create_observer()
    results3 = scheduler.create_observer()

    def action1(scheduler, state=None):
        nonlocal subject
        subject = BehaviorSubject(100)

    scheduler.schedule_absolute(100, action1)

    def action2(scheduler, state=None):
        nonlocal subscription
        subscription = xs.subscribe(subject)

    scheduler.schedule_absolute(200, action2)

    def action3(scheduler, state=None):
        subscription.dispose()

    scheduler.schedule_absolute(1000, action3)

    def action4(scheduler, state=None):
        nonlocal subscription1
        subscription1 = subject.subscribe(results1)

    scheduler.schedule_absolute(300, action4)

    def action5(scheduler, state=None):
        nonlocal subscription2
        subscription2 = subject.subscribe(results2)

    scheduler.schedule_absolute(400, action5)

    def action6(scheduler, state=None):
        nonlocal subscription3
        subscription3 = subject.subscribe(results3)

    scheduler.schedule_absolute(900, action6)

    def action7(scheduler, state=None):
        subscription1.dispose()

    scheduler.schedule_absolute(600, action7)

    def action8(scheduler, state=None):
        subscription2.dispose()

    scheduler.schedule_absolute(700, action8)

    def action9(scheduler, state=None):
        subscription1.dispose()

    scheduler.schedule_absolute(800, action9)

    def action10(scheduler, state=None):
        subscription3.dispose()

    scheduler.schedule_absolute(950, action10)

    scheduler.start()

    results1.messages.assert_equal(on_next(300, 4), on_next(340, 5),
                                   on_next(410, 6), on_next(520, 7))

    results2.messages.assert_equal(on_next(400, 5), on_next(410, 6),
                                   on_next(520, 7), on_next(630, 8))

    results3.messages.assert_equal(on_next(900, 10), on_next(940, 11))
Ejemplo n.º 33
0
    def test_connectable_observable_multiple_non_overlapped_connections(self):
        scheduler = TestScheduler()

        xs = scheduler.create_hot_observable(on_next(210, 1), on_next(220, 2),
                                             on_next(230, 3), on_next(240, 4),
                                             on_next(250, 5), on_next(260, 6),
                                             on_next(270, 7), on_next(280, 8),
                                             on_next(290, 9),
                                             on_completed(300))

        subject = Subject()

        conn = xs.multicast(subject)

        c1 = [None]

        def action10(scheduler, state):
            c1[0] = conn.connect(scheduler)

        scheduler.schedule_absolute(225, action10)

        def action11(scheduler, state):
            c1[0].dispose()

        scheduler.schedule_absolute(241, action11)

        def action12(scheduler, state):
            c1[0].dispose()  # idempotency test

        scheduler.schedule_absolute(245, action12)

        def action13(scheduler, state):
            c1[0].dispose()  # idempotency test

        scheduler.schedule_absolute(251, action13)

        def action14(scheduler, state):
            c1[0].dispose()  # idempotency test

        scheduler.schedule_absolute(260, action14)

        c2 = [None]

        def action20(scheduler, state):
            c2[0] = conn.connect(scheduler)

        scheduler.schedule_absolute(249, action20)

        def action21(scheduler, state):
            c2[0].dispose()

        scheduler.schedule_absolute(255, action21)

        def action22(scheduler, state):
            c2[0].dispose()  # idempotency test

        scheduler.schedule_absolute(265, action22)

        def action23(scheduler, state):
            c2[0].dispose()  # idempotency test

        scheduler.schedule_absolute(280, action23)

        c3 = [None]

        def action30(scheduler, state):
            c3[0] = conn.connect(scheduler)

        scheduler.schedule_absolute(275, action30)

        def action31(scheduler, state):
            c3[0].dispose()

        scheduler.schedule_absolute(295, action31)

        res = scheduler.start(lambda: conn)

        assert res.messages == [
            on_next(230, 3),
            on_next(240, 4),
            on_next(250, 5),
            on_next(280, 8),
            on_next(290, 9)
        ]

        assert xs.subscriptions == [
            subscribe(225, 241),
            subscribe(249, 255),
            subscribe(275, 295)
        ]
def test_subject_disposed():
    scheduler = TestScheduler()

    subject = None

    results1 = scheduler.create_observer()
    subscription1 = None

    results2 = scheduler.create_observer()
    subscription2 = None

    results3 = scheduler.create_observer()
    subscription3 = None

    def action1(scheduler, state=None):
        nonlocal subject
        subject = BehaviorSubject(0)

    scheduler.schedule_absolute(100, action1)

    def action2(scheduler, state=None):
        nonlocal subscription1
        subscription1 = subject.subscribe(results1)

    scheduler.schedule_absolute(200, action2)

    def action3(scheduler, state=None):
        nonlocal subscription2
        subscription2 = subject.subscribe(results2)

    scheduler.schedule_absolute(300, action3)

    def action4(scheduler, state=None):
        nonlocal subscription3
        subscription3 = subject.subscribe(results3)

    scheduler.schedule_absolute(400, action4)

    def action5(scheduler, state=None):
        subscription1.dispose()

    scheduler.schedule_absolute(500, action5)

    def action6(scheduler, state=None):
        subject.dispose()

    scheduler.schedule_absolute(600, action6)

    def action7(scheduler, state=None):
        subscription2.dispose()

    scheduler.schedule_absolute(700, action7)

    def action8(scheduler, state=None):
        subscription3.dispose()

    scheduler.schedule_absolute(800, action8)

    def action9(scheduler, state=None):
        subject.on_next(1)

    scheduler.schedule_absolute(150, action9)

    def action10(scheduler, state=None):
        subject.on_next(2)

    scheduler.schedule_absolute(250, action10)

    def action11(scheduler, state=None):
        subject.on_next(3)

    scheduler.schedule_absolute(350, action11)

    def action12(scheduler, state=None):
        subject.on_next(4)

    scheduler.schedule_absolute(450, action12)

    def action13(scheduler, state=None):
        subject.on_next(5)

    scheduler.schedule_absolute(550, action13)

    @raises(DisposedException)
    def action14(scheduler, state=None):
        subject.on_next(6)

    scheduler.schedule_absolute(650, action14)

    @raises(DisposedException)
    def action15(scheduler, state=None):
        subject.on_completed()

    scheduler.schedule_absolute(750, action15)

    @raises(DisposedException)
    def action16(scheduler, state=None):
        subject.on_error(RxException())

    scheduler.schedule_absolute(850, action16)

    @raises(DisposedException)
    def action17(scheduler, state=None):
        subject.subscribe(None)

    scheduler.schedule_absolute(950, action17)

    scheduler.start()

    results1.messages.assert_equal(on_next(200, 1), on_next(250, 2),
                                   on_next(350, 3), on_next(450, 4))

    results2.messages.assert_equal(on_next(300, 2), on_next(350, 3),
                                   on_next(450, 4), on_next(550, 5))

    results3.messages.assert_equal(on_next(400, 3), on_next(450, 4),
                                   on_next(550, 5))
def test_canceled():
    scheduler = TestScheduler()

    xs = scheduler.create_hot_observable(on_completed(630), on_next(640, 9),
                                         on_completed(650),
                                         on_error(660, RxException()))

    subject = [None]
    subscription = [None]
    subscription1 = [None]
    subscription2 = [None]
    subscription3 = [None]

    results1 = scheduler.create_observer()
    results2 = scheduler.create_observer()
    results3 = scheduler.create_observer()

    def action1(scheduler, state=None):
        subject[0] = ReplaySubject(3, 100, scheduler)

    scheduler.schedule_absolute(100, action1)

    def action2(scheduler, state=None):
        subscription[0] = xs.subscribe(subject[0])

    scheduler.schedule_absolute(200, action2)

    def action3(scheduler, state=None):
        subscription[0].dispose()

    scheduler.schedule_absolute(1000, action3)

    def action4(scheduler, state=None):
        subscription1[0] = subject[0].subscribe(results1)

    scheduler.schedule_absolute(300, action4)

    def action5(scheduler, state=None):
        subscription2[0] = subject[0].subscribe(results2)

    scheduler.schedule_absolute(400, action5)

    def action6(scheduler, state=None):
        subscription3[0] = subject[0].subscribe(results3)

    scheduler.schedule_absolute(900, action6)

    def action7(scheduler, state=None):
        subscription1[0].dispose()

    scheduler.schedule_absolute(600, action7)

    def action8(scheduler, state=None):
        subscription2[0].dispose()

    scheduler.schedule_absolute(700, action8)

    def action9(scheduler, state=None):
        subscription1[0].dispose()

    scheduler.schedule_absolute(800, action9)

    def action10(scheduler, state=None):
        subscription3[0].dispose()

    scheduler.schedule_absolute(950, action10)

    scheduler.start()

    assert results1.messages == []

    assert results2.messages == [on_completed(630)]

    assert results3.messages == [on_completed(900)]
Ejemplo n.º 36
0
    def test_flat_map_dispose(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(5, scheduler.create_cold_observable(on_error(1, 'ex1'))),
            on_next(105, scheduler.create_cold_observable(on_error(1, 'ex2'))),
            on_next(
                300,
                scheduler.create_cold_observable(on_next(10, 102),
                                                 on_next(90, 103),
                                                 on_next(110, 104),
                                                 on_next(190, 105),
                                                 on_next(440, 106),
                                                 on_completed(460))),
            on_next(
                400,
                scheduler.create_cold_observable(on_next(180, 202),
                                                 on_next(190, 203),
                                                 on_completed(205))),
            on_next(
                550,
                scheduler.create_cold_observable(on_next(10, 301),
                                                 on_next(50, 302),
                                                 on_next(70, 303),
                                                 on_next(260, 304),
                                                 on_next(310, 305),
                                                 on_completed(410))),
            on_next(750, scheduler.create_cold_observable(on_completed(40))),
            on_next(
                850,
                scheduler.create_cold_observable(on_next(80, 401),
                                                 on_next(90, 402),
                                                 on_completed(100))),
            on_completed(900))

        def create():
            return xs.pipe(ops.flat_map(lambda x: x))

        results = scheduler.start(create, disposed=700)

        assert results.messages == [
            on_next(310, 102),
            on_next(390, 103),
            on_next(410, 104),
            on_next(490, 105),
            on_next(560, 301),
            on_next(580, 202),
            on_next(590, 203),
            on_next(600, 302),
            on_next(620, 303)
        ]
        assert xs.subscriptions == [subscribe(200, 700)]
        assert xs.messages[2].value.value.subscriptions == [
            subscribe(300, 700)
        ]
        assert xs.messages[3].value.value.subscriptions == [
            subscribe(400, 605)
        ]
        assert xs.messages[4].value.value.subscriptions == [
            subscribe(550, 700)
        ]
        assert xs.messages[5].value.value.subscriptions == []
        assert xs.messages[6].value.value.subscriptions == []
def test_replay_subject_dies_out():
    scheduler = TestScheduler()

    xs = scheduler.create_hot_observable(on_next(70, 1), on_next(110, 2),
                                         on_next(220, 3), on_next(270, 4),
                                         on_next(340, 5), on_next(410, 6),
                                         on_next(520, 7), on_completed(580))

    subject = [None]

    results1 = scheduler.create_observer()
    results2 = scheduler.create_observer()
    results3 = scheduler.create_observer()
    results4 = scheduler.create_observer()

    def action1(scheduler, state=None):
        subject[0] = ReplaySubject(sys.maxsize, 100, scheduler)

    scheduler.schedule_absolute(100, action1)

    def action2(scheduler, state=None):
        xs.subscribe(subject[0])

    scheduler.schedule_absolute(200, action2)

    def action3(scheduler, state=None):
        subject[0].subscribe(results1)

    scheduler.schedule_absolute(300, action3)

    def action4(scheduler, state=None):
        subject[0].subscribe(results2)

    scheduler.schedule_absolute(400, action4)

    def action5(scheduler, state=None):
        subject[0].subscribe(results3)

    scheduler.schedule_absolute(600, action5)

    def action6(scheduler, state=None):
        subject[0].subscribe(results4)

    scheduler.schedule_absolute(900, action6)

    scheduler.start()

    assert results1.messages == [
        on_next(300, 3),
        on_next(300, 4),
        on_next(340, 5),
        on_next(410, 6),
        on_next(520, 7),
        on_completed(580)
    ]

    assert results2.messages == [
        on_next(400, 5),
        on_next(410, 6),
        on_next(520, 7),
        on_completed(580)
    ]

    assert results3.messages == [on_next(600, 7), on_completed(600)]

    assert results4.messages == [on_completed(900)]
def test_subject_disposed():
    scheduler = TestScheduler()

    subject = [None]
    subscription1 = [None]
    subscription2 = [None]
    subscription3 = [None]

    results1 = scheduler.create_observer()
    results2 = scheduler.create_observer()
    results3 = scheduler.create_observer()

    def action1(scheduler, state=None):
        subject[0] = ReplaySubject(scheduler=scheduler)

    scheduler.schedule_absolute(100, action1)

    def action2(scheduler, state=None):
        subscription1[0] = subject[0].subscribe(results1)

    scheduler.schedule_absolute(200, action2)

    def action3(scheduler, state=None):
        subscription2[0] = subject[0].subscribe(results2)

    scheduler.schedule_absolute(300, action3)

    def action4(scheduler, state=None):
        subscription3[0] = subject[0].subscribe(results3)

    scheduler.schedule_absolute(400, action4)

    def action5(scheduler, state=None):
        subscription1[0].dispose()

    scheduler.schedule_absolute(500, action5)

    def action6(scheduler, state=None):
        subject[0].dispose()

    scheduler.schedule_absolute(600, action6)

    def action7(scheduler, state=None):
        subscription2[0].dispose()

    scheduler.schedule_absolute(700, action7)

    def action8(scheduler, state=None):
        subscription3[0].dispose()

    scheduler.schedule_absolute(800, action8)

    def action9(scheduler, state=None):
        subject[0].on_next(1)

    scheduler.schedule_absolute(150, action9)

    def action10(scheduler, state=None):
        subject[0].on_next(2)

    scheduler.schedule_absolute(250, action10)

    def action11(scheduler, state=None):
        subject[0].on_next(3)

    scheduler.schedule_absolute(350, action11)

    def action12(scheduler, state=None):
        subject[0].on_next(4)

    scheduler.schedule_absolute(450, action12)

    def action13(scheduler, state=None):
        subject[0].on_next(5)

    scheduler.schedule_absolute(550, action13)

    def action14(scheduler, state=None):
        with pytest.raises(DisposedException):
            subject[0].on_next(6)

    scheduler.schedule_absolute(650, action14)

    def action15(scheduler, state=None):
        with pytest.raises(DisposedException):
            subject[0].on_completed()

    scheduler.schedule_absolute(750, action15)

    def action16(scheduler, state=None):
        with pytest.raises(DisposedException):
            subject[0].on_error(Exception())

    scheduler.schedule_absolute(850, action16)

    def action17(scheduler, state=None):
        with pytest.raises(DisposedException):
            subject[0].subscribe(None)

    scheduler.schedule_absolute(950, action17)

    scheduler.start()

    assert results1.messages == [
        on_next(200, 1),
        on_next(250, 2),
        on_next(350, 3),
        on_next(450, 4)
    ]

    assert results2.messages == [
        on_next(300, 1),
        on_next(300, 2),
        on_next(350, 3),
        on_next(450, 4),
        on_next(550, 5)
    ]

    assert results3.messages == [
        on_next(400, 1),
        on_next(400, 2),
        on_next(400, 3),
        on_next(450, 4),
        on_next(550, 5)
    ]
Ejemplo n.º 39
0
 def test_count_empty(self):
     scheduler = TestScheduler()
     xs = scheduler.create_hot_observable(on_next(150, 1),
                                          on_completed(250))
     res = scheduler.start(create=lambda: xs.count()).messages
     res.assert_equal(on_next(250, 0), on_completed(250))
Ejemplo n.º 40
0
 def test_min_int32_return(self):
     scheduler = TestScheduler()
     xs = scheduler.create_hot_observable(on_next(150, 1), on_next(210, 2),
                                          on_completed(250))
     res = scheduler.start(create=lambda: xs.min()).messages
     res.assert_equal(on_next(250, 2), on_completed(250))
Ejemplo n.º 41
0
 def test_min_int32_never(self):
     scheduler = TestScheduler()
     xs = scheduler.create_hot_observable(on_next(150, 1))
     res = scheduler.start(create=lambda: xs.min()).messages
     res.assert_equal()
Ejemplo n.º 42
0
    def test_publish_with_initial_value_dispose(self):
        connection = [None]
        subscription = [None]
        ys = [None]

        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(on_next(110, 7), on_next(220, 3),
                                             on_next(280, 4), on_next(290, 1),
                                             on_next(340, 8), on_next(360, 5),
                                             on_next(370, 6), on_next(390, 7),
                                             on_next(410, 13), on_next(430, 2),
                                             on_next(450, 9), on_next(520, 11),
                                             on_next(560, 20),
                                             on_completed(600))

        results = scheduler.create_observer()

        def action0(scheduler, state):
            ys[0] = xs.publish_value(1979)

        scheduler.schedule_absolute(created, action0)

        def action1(scheduler, state):
            subscription[0] = ys[0].subscribe(results)

        scheduler.schedule_absolute(subscribed, action1)

        def action2(scheduler, state):
            subscription[0].dispose()

        scheduler.schedule_absolute(350, action2)

        def action3(scheduler, state):
            connection[0] = ys[0].connect()

        scheduler.schedule_absolute(300, action3)

        def action4(scheduler, state):
            connection[0].dispose()

        scheduler.schedule_absolute(400, action4)

        def action5(scheduler, state):
            connection[0] = ys[0].connect()

        scheduler.schedule_absolute(500, action5)

        def action6(scheduler, state):
            connection[0].dispose()

        scheduler.schedule_absolute(550, action6)

        def action7(scheduler, state):
            connection[0] = ys[0].connect()

        scheduler.schedule_absolute(650, action7)

        def action8(scheduler, state):
            connection[0].dispose()

        scheduler.schedule_absolute(800, action8)

        scheduler.start()
        results.messages.assert_equal(on_next(200, 1979), on_next(340, 8))
        xs.subscriptions.assert_equal(subscribe(300, 400), subscribe(500, 550),
                                      subscribe(650, 800))
def test_infinite():
    scheduler = TestScheduler()

    xs = scheduler.create_hot_observable(on_next(70, 1), on_next(110, 2),
                                         on_next(220, 3), on_next(270, 4),
                                         on_next(340, 5), on_next(410, 6),
                                         on_next(520, 7), on_next(630, 8),
                                         on_next(710, 9), on_next(870, 10),
                                         on_next(940, 11), on_next(1020, 12))

    subject = [None]
    subscription = [None]
    subscription1 = [None]
    subscription2 = [None]
    subscription3 = [None]

    results1 = scheduler.create_observer()
    results2 = scheduler.create_observer()
    results3 = scheduler.create_observer()

    def action1(scheduler, state=None):
        subject[0] = ReplaySubject(3, 100, scheduler)

    scheduler.schedule_absolute(100, action1)

    def action2(scheduler, state=None):
        subscription[0] = xs.subscribe(subject[0])

    scheduler.schedule_absolute(200, action2)

    def action3(scheduler, state=None):
        subscription[0].dispose()

    scheduler.schedule_absolute(1000, action3)

    def action4(scheduler, state=None):
        subscription1[0] = subject[0].subscribe(results1)

    scheduler.schedule_absolute(300, action4)

    def action5(scheduler, state=None):
        subscription2[0] = subject[0].subscribe(results2)

    scheduler.schedule_absolute(400, action5)

    def action6(scheduler, state=None):
        subscription3[0] = subject[0].subscribe(results3)

    scheduler.schedule_absolute(900, action6)

    def action7(scheduler, state=None):
        subscription1[0].dispose()

    scheduler.schedule_absolute(600, action7)

    def action8(scheduler, state=None):
        subscription2[0].dispose()

    scheduler.schedule_absolute(700, action8)

    def action9(scheduler, state=None):
        subscription1[0].dispose()

    scheduler.schedule_absolute(800, action9)

    def action10(scheduler, state=None):
        subscription3[0].dispose()

    scheduler.schedule_absolute(950, action10)

    scheduler.start()

    assert results1.messages == [
        on_next(300, 3),
        on_next(300, 4),
        on_next(340, 5),
        on_next(410, 6),
        on_next(520, 7)
    ]

    assert results2.messages == [
        on_next(400, 5),
        on_next(410, 6),
        on_next(520, 7),
        on_next(630, 8)
    ]

    assert results3.messages == [on_next(900, 10), on_next(940, 11)]
Ejemplo n.º 44
0
    def test_flat_map_on_error(self):
        invoked = [0]
        ex = 'ex'
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(5, scheduler.create_cold_observable(on_error(1, 'ex1'))),
            on_next(105, scheduler.create_cold_observable(on_error(1, 'ex2'))),
            on_next(
                300,
                scheduler.create_cold_observable(on_next(10, 102),
                                                 on_next(90, 103),
                                                 on_next(110, 104),
                                                 on_next(190, 105),
                                                 on_next(440, 106),
                                                 on_completed(460))),
            on_next(
                400,
                scheduler.create_cold_observable(on_next(180, 202),
                                                 on_next(190, 203),
                                                 on_completed(205))),
            on_next(
                550,
                scheduler.create_cold_observable(on_next(10, 301),
                                                 on_next(50, 302),
                                                 on_next(70, 303),
                                                 on_next(260, 304),
                                                 on_next(310, 305),
                                                 on_completed(410))),
            on_next(750, scheduler.create_cold_observable(on_completed(40))),
            on_next(
                850,
                scheduler.create_cold_observable(on_next(80, 401),
                                                 on_next(90, 402),
                                                 on_completed(100))),
            on_completed(900))

        def factory():
            def projection(x):
                invoked[0] += 1
                if invoked[0] == 3:
                    raise Exception(ex)
                return x

            return xs.pipe(ops.flat_map(projection))

        results = scheduler.start(factory)

        assert results.messages == [
            on_next(310, 102),
            on_next(390, 103),
            on_next(410, 104),
            on_next(490, 105),
            on_error(550, ex)
        ]
        assert xs.subscriptions == [subscribe(200, 550)]
        assert xs.messages[2].value.value.subscriptions == [
            subscribe(300, 550)
        ]
        assert xs.messages[3].value.value.subscriptions == [
            subscribe(400, 550)
        ]
        assert xs.messages[4].value.value.subscriptions == []
        assert xs.messages[5].value.value.subscriptions == []
        assert xs.messages[6].value.value.subscriptions == []
Ejemplo n.º 45
0
wwt_test_scheduler = TestScheduler()

Observable.interval(50, wwt_test_scheduler).take_until(
    Observable.timer(100)).window_with_time(10).subscribe(
        lambda observable: observable.count().subscribe(print_value))

# test_scheduler.start()

print('-- Combine Latest')
cl_test_scheduler = TestScheduler()

Observable.combine_latest(
    Observable.interval(1, cl_test_scheduler).map(lambda x: 'a: {}'.format(x)),
    Observable.interval(2, cl_test_scheduler).map(lambda x: 'b: {}'.format(x)),
    lambda a, b: '{}; {}'.format(a, b)).take_until(
        Observable.timer(1)).subscribe(print_value)
# cl_test_scheduler.start()

print('-- Zip')

zip_test_scheduler = TestScheduler()

Observable.zip(
    Observable.interval(1,
                        zip_test_scheduler).map(lambda x: 'a: {}'.format(x)),
    Observable.interval(2,
                        zip_test_scheduler).map(lambda x: 'b: {}'.format(x)),
    lambda a, b: '{}; {}'.format(a, b)).take_until(
        Observable.timer(1)).subscribe(print_value)
zip_test_scheduler.start()
Ejemplo n.º 46
0
    def test_group_join_op_normal_i(self):
        scheduler = TestScheduler()

        xs = scheduler.create_hot_observable(
            on_next(210, TimeInterval(0, 10)), on_next(219, TimeInterval(1,
                                                                         5)),
            on_next(240, TimeInterval(2, 10)),
            on_next(300, TimeInterval(3, 100)),
            on_next(310, TimeInterval(4, 80)), on_next(500,
                                                       TimeInterval(5, 90)),
            on_next(700, TimeInterval(6, 25)),
            on_next(710, TimeInterval(7, 280)),
            on_next(720, TimeInterval(8, 100)),
            on_next(830, TimeInterval(9, 10)), on_completed(900))

        ys = scheduler.create_hot_observable(
            on_next(215, TimeInterval("hat", 20)),
            on_next(217, TimeInterval("bat", 1)),
            on_next(290, TimeInterval("wag", 200)),
            on_next(300, TimeInterval("pig", 10)),
            on_next(305, TimeInterval("cup", 50)),
            on_next(600, TimeInterval("yak", 90)),
            on_next(702, TimeInterval("tin", 20)),
            on_next(712, TimeInterval("man", 10)),
            on_next(722, TimeInterval("rat", 200)),
            on_next(732, TimeInterval("wig", 5)), on_completed(800))

        xsd = []
        ysd = []

        def create():
            return xs.group_join(
                ys, lambda x: new_timer(xsd, x.interval, scheduler),
                lambda y: new_timer(ysd, y.interval, scheduler), lambda x, yy:
                yy.map(lambda y: str(x.value) + y.value)).merge_all()

        res = scheduler.start(create=create)

        assert res.messages == [
            on_next(215, "0hat"),
            on_next(217, "0bat"),
            on_next(219, "1hat"),
            on_next(300, "3wag"),
            on_next(300, "3pig"),
            on_next(305, "3cup"),
            on_next(310, "4wag"),
            on_next(310, "4pig"),
            on_next(310, "4cup"),
            on_next(702, "6tin"),
            on_next(710, "7tin"),
            on_next(712, "6man"),
            on_next(712, "7man"),
            on_next(720, "8tin"),
            on_next(720, "8man"),
            on_next(722, "6rat"),
            on_next(722, "7rat"),
            on_next(722, "8rat"),
            on_next(732, "7wig"),
            on_next(732, "8wig"),
            on_next(830, "9rat"),
            on_completed(990)
        ]

        assert xs.subscriptions == [subscribe(200, 990)]

        assert ys.subscriptions == [subscribe(200, 990)]
Ejemplo n.º 47
0
def test_error():
    scheduler = TestScheduler()

    ex = RxException()

    xs = scheduler.create_hot_observable(on_next(70, 1), on_next(110, 2),
                                         on_next(220, 3), on_next(270, 4),
                                         on_next(340, 5), on_next(410, 6),
                                         on_next(520, 7), on_error(630, ex),
                                         on_next(640, 9), on_completed(650),
                                         on_error(660, RxException()))

    subject = [None]
    subscription = [None]
    subscription1 = [None]
    subscription2 = [None]
    subscription3 = [None]

    results1 = scheduler.create_observer()
    results2 = scheduler.create_observer()
    results3 = scheduler.create_observer()

    def action1(scheduler, state=None):
        subject[0] = BehaviorSubject(100)

    scheduler.schedule_absolute(100, action1)

    def action2(scheduler, state=None):
        subscription[0] = xs.subscribe(subject[0])

    scheduler.schedule_absolute(200, action2)

    def action3(scheduler, state=None):
        subscription[0].dispose()

    scheduler.schedule_absolute(1000, action3)

    def action4(scheduler, state=None):
        subscription1[0] = subject[0].subscribe(results1)

    scheduler.schedule_absolute(300, action4)

    def action5(scheduler, state=None):
        subscription2[0] = subject[0].subscribe(results2)

    scheduler.schedule_absolute(400, action5)

    def action6(scheduler, state=None):
        subscription3[0] = subject[0].subscribe(results3)

    scheduler.schedule_absolute(900, action6)

    def action7(scheduler, state=None):
        subscription1[0].dispose()

    scheduler.schedule_absolute(600, action7)

    def action8(scheduler, state=None):
        subscription2[0].dispose()

    scheduler.schedule_absolute(700, action8)

    def action9(scheduler, state=None):
        subscription1[0].dispose()

    scheduler.schedule_absolute(800, action9)

    def action10(scheduler, state=None):
        subscription3[0].dispose()

    scheduler.schedule_absolute(950, action10)

    scheduler.start()

    assert results1.messages == [
        on_next(300, 4),
        on_next(340, 5),
        on_next(410, 6),
        on_next(520, 7)
    ]

    assert results2.messages == [
        on_next(400, 5),
        on_next(410, 6),
        on_next(520, 7),
        on_error(630, ex)
    ]

    assert results3.messages == [on_error(900, ex)]
Ejemplo n.º 48
0
def test_group_by_inner_error():
    ex = 'ex1'
    scheduler = TestScheduler()
    xs = scheduler.create_hot_observable(on_next(90, "error"),
                                         on_next(110, "error"),
                                         on_next(130, "error"),
                                         on_next(220, "  foo"),
                                         on_next(240, " FoO "),
                                         on_next(270, "baR  "),
                                         on_next(310, "foO "),
                                         on_next(350, " Baz   "),
                                         on_next(360, "  qux "),
                                         on_next(390, "   bar"),
                                         on_next(420, " BAR  "),
                                         on_next(470, "FOO "),
                                         on_next(480, "baz  "),
                                         on_next(510, " bAZ "),
                                         on_next(530, "    fOo    "),
                                         on_error(570, ex),
                                         on_next(580, "error"),
                                         on_completed(600),
                                         on_error(650, 'ex'))
    outer_subscription = None
    inner_subscriptions = {}
    inners = {}
    outer = None
    results = {}
    result = None

    def action1(scheduler, state):
        nonlocal outer
        outer = xs.group_by(lambda x: x.lower().strip(), lambda x: x[::-1])
        return outer

    scheduler.schedule_absolute(created, action1)

    def action2(scheduler, state):
        nonlocal outer_subscription

        def on_next(group):
            nonlocal result, inners, results

            result = scheduler.create_observer()
            inners[group.key] = group
            results[group.key] = result

            def action3(scheduler, state):
                nonlocal inner_subscriptions
                inner_subscriptions[group.key] = group.subscribe(result)

            scheduler.schedule_relative(100, action3)

        outer_subscription = outer.subscribe(on_next, lambda e: None)
        return outer_subscription

    scheduler.schedule_absolute(subscribed, action2)

    def action4(scheduler, state):
        outer_subscription.dispose()
        for sub in inner_subscriptions.values():
            sub.dispose()

    scheduler.schedule_absolute(disposed, action4)

    scheduler.start()
    assert (len(inners) == 4)
    #results['foo'].messages.assert_equal(on_next(470, " OOF"), on_next(530, "    oOf    "), on_error(570, ex))
    #results['bar'].messages.assert_equal(on_next(390, "rab   "), on_next(420, "  RAB "), on_error(570, ex))
    #results['baz'].messages.assert_equal(on_next(480, "  zab"), on_next(510, " ZAb "), on_error(570, ex))
    #results['qux'].messages.assert_equal(on_error(570, ex))
    xs.subscriptions.assert_equal(subscribe(200, 570))
Ejemplo n.º 49
0
 def test_count_never(self):
     scheduler = TestScheduler()
     xs = scheduler.create_hot_observable(on_next(150, 1))
     res = scheduler.start(create=lambda: xs.pipe(_.count())).messages
     assert res == []
def test_canceled():
    scheduler = TestScheduler()

    xs = scheduler.create_hot_observable(on_completed(630), on_next(640, 9),
                                         on_completed(650),
                                         on_error(660, RxException()))

    subject = None
    subscription = None
    subscription1 = None
    subscription2 = None
    subscription3 = None

    results1 = scheduler.create_observer()
    results2 = scheduler.create_observer()
    results3 = scheduler.create_observer()

    def action1(scheduler, state=None):
        nonlocal subject
        subject = BehaviorSubject(100)

    scheduler.schedule_absolute(100, action1)

    def action2(scheduler, state=None):
        nonlocal subscription
        subscription = xs.subscribe(subject)

    scheduler.schedule_absolute(200, action2)

    def action3(scheduler, state=None):
        subscription.dispose()

    scheduler.schedule_absolute(1000, action3)

    def action4(scheduler, state=None):
        nonlocal subscription1
        subscription1 = subject.subscribe(results1)

    scheduler.schedule_absolute(300, action4)

    def action5(scheduler, state=None):
        nonlocal subscription2
        subscription2 = subject.subscribe(results2)

    scheduler.schedule_absolute(400, action5)

    def action6(scheduler, state=None):
        nonlocal subscription3
        subscription3 = subject.subscribe(results3)

    scheduler.schedule_absolute(900, action6)

    def action7(scheduler, state=None):
        subscription1.dispose()

    scheduler.schedule_absolute(600, action7)

    def action8(scheduler, state=None):
        subscription2.dispose()

    scheduler.schedule_absolute(700, action8)

    def action9(scheduler, state=None):
        subscription1.dispose()

    scheduler.schedule_absolute(800, action9)

    def action10(scheduler, state=None):
        subscription3.dispose()

    scheduler.schedule_absolute(950, action10)

    scheduler.start()

    results1.messages.assert_equal(on_next(300, 100))

    results2.messages.assert_equal(on_next(400, 100), on_completed(630))

    results3.messages.assert_equal(on_completed(900))
Ejemplo n.º 51
0
 def test_count_on_error(self):
     ex = 'ex'
     scheduler = TestScheduler()
     xs = scheduler.create_hot_observable(on_next(150, 1), on_error(210, ex))
     res = scheduler.start(create=lambda: xs.pipe(_.count())).messages
     assert res == [on_error(210, ex)]
Ejemplo n.º 52
0
def test_finite():
    scheduler = TestScheduler()

    xs = scheduler.create_hot_observable(
        on_next(70, 1),
        on_next(110, 2),
        on_next(220, 3),
        on_next(270, 4),
        on_next(340, 5),
        on_next(410, 6),
        on_next(520, 7),
        on_completed(630),
        on_next(640, 9),
        on_completed(650),
        on_error(660, 'ex')
    )

    subject = [None]
    subscription = [None]
    subscription1 = [None]
    subscription2 = [None]
    subscription3 = [None]

    results1 = scheduler.create_observer()
    results2 = scheduler.create_observer()
    results3 = scheduler.create_observer()

    def action1(scheduler, state=None):
        subject[0] = ReplaySubject(3, 100, scheduler)
    scheduler.schedule_absolute(100, action1)

    def action3(scheduler, state=None):
        subscription[0] = xs.subscribe(subject[0])
    scheduler.schedule_absolute(200, action3)

    def action4(scheduler, state=None):
        subscription[0].dispose()
    scheduler.schedule_absolute(1000, action4)

    def action5(scheduler, state=None):
        subscription1[0] = subject[0].subscribe(results1)
    scheduler.schedule_absolute(300, action5)

    def action6(scheduler, state=None):
        subscription2[0] = subject[0].subscribe(results2)
    scheduler.schedule_absolute(400, action6)

    def action7(scheduler, state=None):
        subscription3[0] = subject[0].subscribe(results3)
    scheduler.schedule_absolute(900, action7)

    def action8(scheduler, state=None):
        subscription1[0].dispose()
    scheduler.schedule_absolute(600, action8)

    def action9(scheduler, state=None):
        subscription2[0].dispose()
    scheduler.schedule_absolute(700, action9)

    def action10(scheduler, state=None):
        subscription1[0].dispose()
    scheduler.schedule_absolute(800, action10)

    def action11(scheduler, state=None):
        subscription3[0].dispose()
    scheduler.schedule_absolute(950, action11)

    scheduler.start()

    results1.messages.assert_equal(
        on_next(301, 3),
        on_next(302, 4),
        on_next(341, 5),
        on_next(411, 6),
        on_next(521, 7)
    )

    results2.messages.assert_equal(
        on_next(401, 5),
        on_next(411, 6),
        on_next(521, 7),
        on_completed(631)
    )

    results3.messages.assert_equal(
        on_completed(901)
    )
Ejemplo n.º 53
0
    def test_group_by_inner_error(self):
        ex = 'ex1'
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(on_next(90, "error"),
                                             on_next(110, "error"),
                                             on_next(130, "error"),
                                             on_next(220, "  foo"),
                                             on_next(240, " FoO "),
                                             on_next(270, "baR  "),
                                             on_next(310, "foO "),
                                             on_next(350, " Baz   "),
                                             on_next(360, "  qux "),
                                             on_next(390, "   bar"),
                                             on_next(420, " BAR  "),
                                             on_next(470, "FOO "),
                                             on_next(480, "baz  "),
                                             on_next(510, " bAZ "),
                                             on_next(530, "    fOo    "),
                                             on_error(570, ex),
                                             on_next(580, "error"),
                                             on_completed(600),
                                             on_error(650, 'ex'))
        inner_subscriptions = {}
        inners = {}
        results = {}
        c = {"outer_subscription": None, "outer": None}

        def action1(scheduler, state):
            c["outer"] = xs.group_by(lambda x: x.lower().strip(),
                                     lambda x: x[::-1])
            return c["outer"]

        scheduler.schedule_absolute(created, action1)

        def action2(scheduler, state):
            def on_next(group):
                result = scheduler.create_observer()
                inners[group.key] = group
                results[group.key] = result

                def action3(scheduler, state):
                    inner_subscriptions[group.key] = group.subscribe(
                        result, scheduler)

                scheduler.schedule_relative(100, action3)

            c["outer_subscription"] = c["outer"].subscribe_(
                on_next, lambda e: None, scheduler=scheduler)
            return c["outer_subscription"]

        scheduler.schedule_absolute(subscribed, action2)

        def action4(scheduler, state):
            c["outer_subscription"].dispose()
            for sub in inner_subscriptions.values():
                sub.dispose()

        scheduler.schedule_absolute(disposed, action4)

        scheduler.start()
        assert len(inners) == 4
        assert results['foo'].messages == [
            on_next(470, " OOF"),
            on_next(530, "    oOf    "),
            on_error(570, ex)
        ]
        assert results['bar'].messages == [
            on_next(390, "rab   "),
            on_next(420, "  RAB "),
            on_error(570, ex)
        ]
        assert results['baz'].messages == [
            on_next(480, "  zab"),
            on_next(510, " ZAb "),
            on_error(570, ex)
        ]
        assert results['qux'].messages == [on_error(570, ex)]
        assert xs.subscriptions == [subscribe(200, 570)]
Ejemplo n.º 54
0
    def test_group_by_inner_complete(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(90, "error"),
            on_next(110, "error"),
            on_next(130, "error"),
            on_next(220, "  foo"),
            on_next(240, " FoO "),
            on_next(270, "baR  "),
            on_next(310, "foO "),
            on_next(350, " Baz   "),
            on_next(360, "  qux "),
            on_next(390, "   bar"),
            on_next(420, " BAR  "),
            on_next(470, "FOO "),
            on_next(480, "baz  "),
            on_next(510, " bAZ "),
            on_next(530, "    fOo    "),
            on_completed(570),
            on_next(580, "error"),
            on_completed(600),
            on_error(650, 'ex'))
        c = {
            "outer_subscription": None,
            "inner_subscriptions": {},
            "inners": {},
            "results": {},
            "outer": None
        }

        def action1(scheduler, state):
            c["outer"] = xs.pipe(
                    ops.group_by(lambda x: x.lower().strip(), lambda x: x[::-1]),
                    )

        scheduler.schedule_absolute(created, action1)

        def action2(scheduler, state):

            def next(group):

                result = scheduler.create_observer()
                c["inners"][group.key] = group
                c["results"][group.key] = result

                def action21(scheduler, state):
                    c["inner_subscriptions"][group.key] = group.subscribe(result, scheduler)

                scheduler.schedule_relative(100, action21)
            c["outer_subscription"] = c["outer"].subscribe(next, scheduler=scheduler)
        scheduler.schedule_absolute(subscribed, action2)

        def action3(scheduler, state):
            c["outer_subscription"].dispose()
            for sub in c["inner_subscriptions"].values():
                sub.dispose()

        scheduler.schedule_absolute(disposed, action3)
        scheduler.start()
        assert(len(c["inners"]) == 4)
        assert c["results"]['foo'].messages == [
            on_next(470, " OOF"),
            on_next(530, "    oOf    "),
            on_completed(570)]
        assert c["results"]['bar'].messages == [
            on_next(390, "rab   "),
            on_next(420, "  RAB "),
            on_completed(570)]
        assert c["results"]['baz'].messages == [
            on_next(480, "  zab"),
            on_next(510, " ZAb "),
            on_completed(570)]
        assert c["results"]['qux'].messages == [
            on_completed(570)]
        assert xs.subscriptions == [
            subscribe(200, 570)]
Ejemplo n.º 55
0
    def test_publish_with_initial_value_error(self):
        connection = [None]
        subscription = [None]
        ys = [None]

        ex = 'ex'
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(on_next(110, 7), on_next(220, 3),
                                             on_next(280, 4), on_next(290, 1),
                                             on_next(340, 8), on_next(360, 5),
                                             on_next(370, 6), on_next(390, 7),
                                             on_next(410, 13), on_next(430, 2),
                                             on_next(450, 9), on_next(520, 11),
                                             on_next(560, 20),
                                             on_error(600, ex))
        results = scheduler.create_observer()

        def action0(scheduler, state):
            ys[0] = xs.pipe(ops.publish_value(1979))

        scheduler.schedule_absolute(created, action0)

        def action1(scheduler, state):
            subscription[0] = ys[0].subscribe(results)

        scheduler.schedule_absolute(subscribed, action1)

        def action2(scheduler, state):
            subscription[0].dispose()

        scheduler.schedule_absolute(disposed, action2)

        def action3(scheduler, state):
            connection[0] = ys[0].connect()

        scheduler.schedule_absolute(300, action3)

        def action4(scheduler, state):
            connection[0].dispose()

        scheduler.schedule_absolute(400, action4)

        def action5(scheduler, state):
            connection[0] = ys[0].connect()

        scheduler.schedule_absolute(500, action5)

        def action6(scheduler, state):
            connection[0].dispose()

        scheduler.schedule_absolute(800, action6)

        scheduler.start()
        assert results.messages == [
            on_next(200, 1979),
            on_next(340, 8),
            on_next(360, 5),
            on_next(370, 6),
            on_next(390, 7),
            on_next(520, 11),
            on_next(560, 20),
            on_error(600, ex)
        ]
        assert xs.subscriptions == [subscribe(300, 400), subscribe(500, 600)]
Ejemplo n.º 56
0
def test_subject_disposed():
    subject = [None]
    subscription1 = [None]
    subscription2 = [None]
    subscription3 = [None]
    scheduler = TestScheduler()

    results1 = scheduler.create_observer()
    results2 = scheduler.create_observer()
    results3 = scheduler.create_observer()

    def action1(scheduler, state=None):
        subject[0] = AsyncSubject()

    scheduler.schedule_absolute(100, action1)

    def action2(scheduler, state=None):
        subscription1[0] = subject[0].subscribe(results1)

    scheduler.schedule_absolute(200, action2)

    def action3(scheduler, state=None):
        subscription2[0] = subject[0].subscribe(results2)

    scheduler.schedule_absolute(300, action3)

    def action4(scheduler, state=None):
        subscription3[0] = subject[0].subscribe(results3)

    scheduler.schedule_absolute(400, action4)

    def action5(scheduler, state=None):
        subscription1[0].dispose()

    scheduler.schedule_absolute(500, action5)

    def action6(scheduler, state=None):
        subject[0].dispose()

    scheduler.schedule_absolute(600, action6)

    def action7(scheduler, state=None):
        subscription2[0].dispose()

    scheduler.schedule_absolute(700, action7)

    def action8(scheduler, state=None):
        subscription3[0].dispose()

    scheduler.schedule_absolute(800, action8)

    def action9(scheduler, state=None):
        subject[0].on_next(1)

    scheduler.schedule_absolute(150, action9)

    def action10(scheduler, state=None):
        subject[0].on_next(2)

    scheduler.schedule_absolute(250, action10)

    def action11(scheduler, state=None):
        subject[0].on_next(3)

    scheduler.schedule_absolute(350, action11)

    def action12(scheduler, state=None):
        subject[0].on_next(4)

    scheduler.schedule_absolute(450, action12)

    def action13(scheduler, state=None):
        subject[0].on_next(5)

    scheduler.schedule_absolute(550, action13)

    @raises(DisposedException)
    def action14(scheduler, state=None):
        subject[0].on_next(6)

    scheduler.schedule_absolute(650, action14)

    @raises(DisposedException)
    def action15(scheduler, state=None):
        subject[0].on_completed()

    scheduler.schedule_absolute(750, action15)

    @raises(DisposedException)
    def action16(scheduler, state=None):
        subject[0].on_error('ex')

    scheduler.schedule_absolute(850, action16)

    @raises(DisposedException)
    def action17(scheduler, state=None):
        subject[0].subscribe(None)

    scheduler.schedule_absolute(950, action17)

    scheduler.start()
    results1.messages.assert_equal()
    results2.messages.assert_equal()
    results3.messages.assert_equal()
Ejemplo n.º 57
0
print('-- window_with_count | FlatMap')
rx.from_(range(3)).pipe(ops.window_with_count(2),
                        ops.flat_map(lambda x: x)).subscribe(print_value)
# flatmap 은
# flat 하면 리스트를 푼다.
# [[1,2], [3]] -> [1,2,3]
# 그리고 나서 map 한다. x -> x
# [1, 2, 3] -> [1, 2, 3]

print('-- window_with_time')

test_scheduler = TestScheduler()
rx.interval(0.05, test_scheduler).pipe(ops.take_until(
    rx.timer(0.1)), ops.window_with_time(0.01)).subscribe(
        lambda observable: observable.pipe(ops.count()).subscribe(print_value))
test_scheduler.start()
time.sleep(0.5)
# 결과가 이게 맞나?? 단위가 뭐지?

print('-- combine_latest')
rx.combine_latest(
    rx.interval(0.1).pipe(ops.map(lambda x: 'a- {}'.format(x))),
    rx.interval(0.2).pipe(ops.map(lambda x: 'b- {}'.format(x)))).pipe(
        ops.take_until(rx.timer(0.5))).subscribe(print_value)
time.sleep(1)
# 누가 먼저 결과를 배출할지 예상 불가

print('-- zip')
rx.zip(
    rx.interval(0.1).pipe(ops.map(lambda x: 'a- {}'.format(x))),
    rx.interval(0.2).pipe(ops.map(lambda x: 'b- {}'.format(x)))).pipe(