Exemple #1
0
def test_to_observable_return():
    scheduler = TestScheduler()
    
    def create():
        return OnNext(42).to_observable(scheduler)
    
    res = scheduler.start(create)
    res.messages.assert_equal(ReactiveTest.on_next(201, 42), ReactiveTest.on_completed(201))
def test_to_observable_return():
    scheduler = TestScheduler()

    def create():
        return OnNext(42).to_observable(scheduler)

    res = scheduler.start(create)
    assert res.messages == [ReactiveTest.on_next(200, 42), ReactiveTest.on_completed(200)]
Exemple #3
0
    def test_interval(self):
        scheduler = TestScheduler()
        interval_time = 300

        def create():
            return Observable.interval(interval_time, scheduler)
        subscribed = 300
        disposed = 1400
        results = scheduler.start(create, created=1, subscribed=subscribed, disposed=disposed)
        print(results.messages)
        assert results.messages == [
            ReactiveTest.on_next(600, 0),
            ReactiveTest.on_next(900, 1),
            ReactiveTest.on_next(1200, 2),
        ]
Exemple #4
0
    def test_starmap_no_mapper(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            # 100 create
            on_next(180, (1, 10)),
            # 200 subscribe
            on_next(210, (2, 20)),
            on_next(240, (3, 30)),
            on_next(290, (4, 40)),
            on_next(350, (5, 50)),
            on_completed(400),
            on_next(410, (-1, -10)),
            on_completed(420),
            on_error(430, 'ex'))

        def factory():
            return xs.pipe(ops.starmap())

        results = scheduler.start(factory)
        assert results.messages == [
            on_next(210, (2, 20)),
            on_next(240, (3, 30)),
            on_next(290, (4, 40)),
            on_next(350, (5, 50)),
            on_completed(400)]

        assert xs.subscriptions == [ReactiveTest.subscribe(200, 400)]
Exemple #5
0
    def test_map_completed(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(180, 1),
            on_next(210, 2),
            on_next(240, 3),
            on_next(290, 4),
            on_next(350, 5),
            on_completed(400),
            on_next(410, -1),
            on_completed(420),
            on_error(430, 'ex'))
        invoked = [0]

        def factory():
            def projection(x):
                invoked[0] += 1
                return x + 1

            return xs.pipe(map(projection))

        results = scheduler.start(factory)
        assert results.messages == [on_next(210, 3), on_next(
            240, 4), on_next(290, 5), on_next(350, 6), on_completed(400)]
        assert xs.subscriptions == [ReactiveTest.subscribe(200, 400)]
        assert invoked[0] == 4
def test_select_disposeinsideselector():
    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):
        nonlocal invoked
        invoked += 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 == 3
Exemple #7
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:
                #print("*** Dispose ****")
                d.dispose()
            return x

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

        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
Exemple #8
0
    def test_starmap_dispose_inside_mapper(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            # 100 create
            on_next(110, (1, 10)),
            # 200 subscribe
            on_next(210, (2, 20)),
            on_next(310, (3, 30)),
            on_next(410, (4, 40)))

        results = scheduler.create_observer()
        d = SerialDisposable()
        invoked = [0]

        def mapper(x, y):
            invoked[0] += 1
            if scheduler.clock > 250:
                d.dispose()
            return x + y

        d.disposable = xs.pipe(ops.starmap(mapper)).subscribe(
            results, scheduler)

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

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

        assert results.messages == [on_next(110, 11), on_next(210, 22)]

        assert xs.subscriptions == [ReactiveTest.subscribe(0, 310)]
        assert invoked[0] == 3
Exemple #9
0
    def test_map_completed(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(on_next(180, 1), on_next(210, 2),
                                             on_next(240, 3), on_next(290, 4),
                                             on_next(350,
                                                     5), on_completed(400),
                                             on_next(410, -1),
                                             on_completed(420),
                                             on_error(430, 'ex'))
        invoked = [0]

        def factory():
            def projection(x):
                invoked[0] += 1
                return x + 1

            return xs.map(projection)

        results = scheduler.start(factory)
        assert results.messages == [
            on_next(210, 3),
            on_next(240, 4),
            on_next(290, 5),
            on_next(350, 6),
            on_completed(400)
        ]
        assert xs.subscriptions == [ReactiveTest.subscribe(200, 400)]
        assert invoked[0] == 4
Exemple #10
0
    def test_starmap_mapper_with_args(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            # 100 create
            on_next(180, (1, 10)),
            # 200 subscribe
            on_next(210, (2, 20)),
            on_next(240, (3, 30)),
            on_next(290, (4, 40)),
            on_next(350, (5, 50)),
            on_completed(400),
            on_next(410, (-1, -10)),
            on_completed(420),
            on_error(430, 'ex'))

        invoked = [0]

        def factory():
            def mapper(*args):
                invoked[0] += 1
                return sum(args)

            return xs.pipe(ops.starmap(mapper))

        results = scheduler.start(factory)
        assert results.messages == [
            on_next(210, 22),
            on_next(240, 33),
            on_next(290, 44),
            on_next(350, 55),
            on_completed(400)
        ]

        assert xs.subscriptions == [ReactiveTest.subscribe(200, 400)]
        assert invoked[0] == 4
Exemple #11
0
    def test_starmap_no_mapper(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            # 100 create
            on_next(180, (1, 10)),
            # 200 subscribe
            on_next(210, (2, 20)),
            on_next(240, (3, 30)),
            on_next(290, (4, 40)),
            on_next(350, (5, 50)),
            on_completed(400),
            on_next(410, (-1, -10)),
            on_completed(420),
            on_error(430, 'ex'))

        def factory():
            return xs.pipe(ops.starmap())

        results = scheduler.start(factory)
        assert results.messages == [
            on_next(210, (2, 20)),
            on_next(240, (3, 30)),
            on_next(290, (4, 40)),
            on_next(350, (5, 50)),
            on_completed(400)
        ]

        assert xs.subscriptions == [ReactiveTest.subscribe(200, 400)]
Exemple #12
0
    def test_starmap_mapper_with_args(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            # 100 create
            on_next(180, (1, 10)),
            # 200 subscribe
            on_next(210, (2, 20)),
            on_next(240, (3, 30)),
            on_next(290, (4, 40)),
            on_next(350, (5, 50)),
            on_completed(400),
            on_next(410, (-1, -10)),
            on_completed(420),
            on_error(430, 'ex'))

        invoked = [0]

        def factory():
            def mapper(*args):
                invoked[0] += 1
                return sum(args)

            return xs.pipe(ops.starmap(mapper))

        results = scheduler.start(factory)
        assert results.messages == [
            on_next(210, 22),
            on_next(240, 33),
            on_next(290, 44),
            on_next(350, 55),
            on_completed(400)]

        assert xs.subscriptions == [ReactiveTest.subscribe(200, 400)]
        assert invoked[0] == 4
Exemple #13
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
Exemple #14
0
    def test_interval(self):
        scheduler = TestScheduler()
        on_next = ReactiveTest.on_next
        self.messages = scheduler.create_hot_observable(
            on_next(10, 'posted: 32: sell ABC 199 @ $1.23'),
            on_next(11, 'fulfilled: 32: sell ABC 199 @ $1.23'),
            on_next(101, 'posted: 32: buy ABC 199 @ $1.23'),
            on_next(102, 'fulfilled: 32: buy ABC 199 @ $1.23'),
            ReactiveTest.on_completed(750),
        )
        self.results = []

        def create(scheduler, state):
            self.aggregate = utils.aggregate_stock_orders_from_messages(
                self.messages)

        def subscribe(scheduler, state):
            self.subscription = self.aggregate.subscribe(
                lambda value: self.results.append(value))

        def dispose(scheduler, state):
            self.subscription.dispose()

        scheduler.schedule_absolute(1, create)
        scheduler.schedule_absolute(2, subscribe)
        scheduler.schedule_absolute(1000, dispose)

        scheduler.start().messages
        self.results
Exemple #15
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
Exemple #16
0
def test_to_observable_empty():
    scheduler = TestScheduler()

    def create():
        return OnCompleted().to_observable(scheduler)
    
    res = scheduler.start(create)
    res.messages.assert_equal(ReactiveTest.on_completed(201))
def test_to_observable_empty():
    scheduler = TestScheduler()

    def create():
        return OnCompleted().to_observable(scheduler)

    res = scheduler.start(create)
    assert res.messages == [ReactiveTest.on_completed(200)]
def test_to_observable_on_error():
    ex = 'ex'
    scheduler = TestScheduler()

    def create():
        return OnError(ex).to_observable(scheduler)

    res = scheduler.start(create)
    assert res.messages == [ReactiveTest.on_error(200, ex)]
Exemple #19
0
def test_to_observable_throw():
    ex = 'ex'
    scheduler = TestScheduler()

    def create():
        return OnError(ex).to_observable(scheduler)
    
    res = scheduler.start(create)    
    res.messages.assert_equal(ReactiveTest.on_error(201, ex))
Exemple #20
0
def messages_to_records(messages: List[Tuple]) -> List[Recorded]:
    """
    Helper function to convert messages returned by parse() to a list of
    Recorded.
    """
    records = []

    dispatcher = dict(N=lambda t, n: ReactiveTest.on_next(t, n.value),
                      E=lambda t, n: ReactiveTest.on_error(t, n.exception),
                      C=lambda t, n: ReactiveTest.on_completed(t))

    for message in messages:
        time, notification = message
        kind = notification.kind
        record = dispatcher[kind](time, notification)
        records.append(record)

    return records
def test_to_observable_on_error():
    ex = 'ex'
    scheduler = TestScheduler()

    def create():
        return OnError(ex).to_observable(scheduler)

    res = scheduler.start(create)
    assert res.messages == [ReactiveTest.on_error(200, ex)]
Exemple #22
0
def test_to_observable_throw():
    ex = 'ex'
    scheduler = TestScheduler()

    def create():
        return OnError(ex).to_observable(scheduler)

    res = scheduler.start(create)
    res.messages.assert_equal(ReactiveTest.on_error(201, ex))
Exemple #23
0
def messages_to_records(messages: List[Tuple]) -> List[Recorded]:
    """
    Helper function to convert messages returned by parse() to a list of
    Recorded.
    """
    records = []

    dispatcher = dict(
        N=lambda t, n: ReactiveTest.on_next(t, n.value),
        E=lambda t, n: ReactiveTest.on_error(t, n.exception),
        C=lambda t, n: ReactiveTest.on_completed(t)
        )

    for message in messages:
        time, notification = message
        kind = notification.kind
        record = dispatcher[kind](time, notification)
        records.append(record)

    return records
Exemple #24
0
    def test_starmap_never(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable()

        invoked = [0]

        def factory():
            def mapper(x, y):
                invoked[0] += 1
                return x + y

            return xs.pipe(ops.starmap(mapper))

        results = scheduler.start(factory)
        assert results.messages == []
        assert xs.subscriptions == [ReactiveTest.subscribe(200, 1000)]
        assert invoked[0] == 0
Exemple #25
0
    def test_starmap_never(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable()

        invoked = [0]

        def factory():
            def mapper(x, y):
                invoked[0] += 1
                return x + y

            return xs.pipe(ops.starmap(mapper))

        results = scheduler.start(factory)
        assert results.messages == []
        assert xs.subscriptions == [ReactiveTest.subscribe(200, 1000)]
        assert invoked[0] == 0
def test_select_completed():
    scheduler = TestScheduler()
    xs = scheduler.create_hot_observable(on_next(180, 1), on_next(210, 2), on_next(240, 3), on_next(290, 4), on_next(350, 5), on_completed(400), on_next(410, -1), on_completed(420), on_error(430, 'ex'))
    invoked = 0
    
    def factory():
        def projection(x):
            nonlocal invoked
            invoked += 1
            return x + 1

        return xs.select(projection)

    results = scheduler.start(factory)
    results.messages.assert_equal(on_next(210, 3), on_next(240, 4), on_next(290, 5), on_next(350, 6), on_completed(400))
    xs.subscriptions.assert_equal(ReactiveTest.subscribe(200, 400))
    assert invoked == 4
Exemple #27
0
    def test_starmap_empty(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            # 100 create
            # 200 subscribe
            on_completed(300), )

        invoked = [0]

        def factory():
            def mapper(x, y):
                invoked[0] += 1
                return x + y

            return xs.pipe(ops.starmap(mapper))

        results = scheduler.start(factory)
        assert results.messages == [on_completed(300)]
        assert xs.subscriptions == [ReactiveTest.subscribe(200, 300)]
        assert invoked[0] == 0
Exemple #28
0
    def test_starmap_empty(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            # 100 create
            # 200 subscribe
            on_completed(300),
            )

        invoked = [0]

        def factory():
            def mapper(x, y):
                invoked[0] += 1
                return x + y

            return xs.pipe(ops.starmap(mapper))

        results = scheduler.start(factory)
        assert results.messages == [on_completed(300)]
        assert xs.subscriptions == [ReactiveTest.subscribe(200, 300)]
        assert invoked[0] == 0
    def test_map_default_mapper(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(on_next(180, 1), on_next(210, 2),
                                             on_next(240, 3), on_next(290, 4),
                                             on_next(350,
                                                     5), on_completed(400),
                                             on_next(410, -1),
                                             on_completed(420),
                                             on_error(430, 'ex'))

        def factory():
            return xs.pipe(map())

        results = scheduler.start(factory)
        assert results.messages == [
            on_next(210, 2),
            on_next(240, 3),
            on_next(290, 4),
            on_next(350, 5),
            on_completed(400)
        ]
        assert xs.subscriptions == [ReactiveTest.subscribe(200, 400)]
Exemple #30
0
    def test_starmap_dispose_inside_mapper(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            # 100 create
            on_next(110, (1, 10)),
            # 200 subscribe
            on_next(210, (2, 20)),
            on_next(310, (3, 30)),
            on_next(410, (4, 40)))

        results = scheduler.create_observer()
        d = SerialDisposable()
        invoked = [0]

        def mapper(x, y):
            invoked[0] += 1
            if scheduler._clock > 250:
                d.dispose()
            return x + y

        d.disposable = xs.pipe(
            ops.starmap(mapper)
        ).subscribe(results, scheduler)

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

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

        assert results.messages == [
                on_next(110, 11),
                on_next(210, 22)]

        assert xs.subscriptions == [ReactiveTest.subscribe(0, 310)]
        assert invoked[0] == 3
Exemple #31
0
    def test_map_default_mapper(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(180, 1),
            on_next(210, 2),
            on_next(240, 3),
            on_next(290, 4),
            on_next(350, 5),
            on_completed(400),
            on_next(410, -1),
            on_completed(420),
            on_error(430, 'ex'))

        def factory():
            return xs.pipe(map())

        results = scheduler.start(factory)
        assert results.messages == [
            on_next(210, 2),
            on_next(240, 3),
            on_next(290, 4),
            on_next(350, 5),
            on_completed(400)]
        assert xs.subscriptions == [ReactiveTest.subscribe(200, 400)]