Ejemplo n.º 1
0
    def test_immediate_scheduleaction(self):
        scheduler = ImmediateScheduler()
        ran = False

        def action(scheduler, state=None):
            nonlocal ran
            ran = True

        scheduler.schedule(action)
        assert ran
Ejemplo n.º 2
0
    def test_immediate_schedule_action_error(self):
        scheduler = ImmediateScheduler()

        class MyException(Exception):
            pass

        def action(scheduler, state=None):
            raise MyException()

        with pytest.raises(MyException):
            return scheduler.schedule(action)
Ejemplo n.º 3
0
    def test_immediate_simple2(self):
        scheduler = ImmediateScheduler()
        xx = 0

        def action(scheduler, state=None):
            nonlocal xx
            xx = state
            return Disposable()

        scheduler.schedule_absolute(default_now(), action, 42)
        assert xx == 42
Ejemplo n.º 4
0
    def test_immediate_simple3(self):
        scheduler = ImmediateScheduler()
        xx = 0

        def action(scheduler, state=None):
            nonlocal xx
            xx = state
            return Disposable()

        scheduler.schedule_relative(DELTA_ZERO, action, 42)
        assert xx == 42
Ejemplo n.º 5
0
    def test_immediate_simple1(self):
        scheduler = ImmediateScheduler()
        xx = 0

        def action(scheduler, state=None):
            nonlocal xx
            xx = state
            return Disposable()

        scheduler.schedule(action, 42)
        assert xx == 42
Ejemplo n.º 6
0
    def test_immediate_schedule_action_due_error(self):
        scheduler = ImmediateScheduler()
        ran = False

        def action(scheduler, state=None):
            nonlocal ran
            ran = True

        with pytest.raises(WouldBlockException):
            scheduler.schedule_relative(0.1, action)

        assert ran is False
Ejemplo n.º 7
0
 def test_immediate_now_units(self):
     scheduler = ImmediateScheduler()
     diff = scheduler.now
     sleep(1.1)
     diff = scheduler.now - diff
     assert timedelta(milliseconds=1000) < diff < timedelta(
         milliseconds=1300)
Ejemplo n.º 8
0
    def to_observable(
        self, scheduler: Optional[abc.SchedulerBase] = None
    ) -> abc.ObservableBase[_T]:
        """Returns an observable sequence with a single notification,
        using the specified scheduler, else the immediate scheduler.

        Args:
            scheduler: [Optional] Scheduler to send out the
                notification calls on.

        Returns:
            An observable sequence that surfaces the behavior of the
            notification upon subscription.
        """

        _scheduler = scheduler or ImmediateScheduler.singleton()

        def subscribe(
            observer: abc.ObserverBase[_T],
            scheduler: Optional[abc.SchedulerBase] = None,
        ) -> abc.DisposableBase:
            def action(scheduler: abc.SchedulerBase, state: Any) -> None:
                self._accept_observer(observer)
                if self.kind == "N":
                    observer.on_completed()

            __scheduler = scheduler or _scheduler
            return __scheduler.schedule(action)

        return Observable(subscribe)
Ejemplo n.º 9
0
    def test_observe_on_forward_subscribe_scheduler(self):
        scheduler = ImmediateScheduler()
        expected_subscribe_scheduler = ImmediateScheduler()

        actual_subscribe_scheduler = None

        def subscribe(observer, scheduler):
            nonlocal actual_subscribe_scheduler
            actual_subscribe_scheduler = scheduler
            observer.on_completed()

        xs = reactivex.create(subscribe)

        xs.pipe(ops.observe_on(scheduler)).subscribe(
            scheduler=expected_subscribe_scheduler)

        assert expected_subscribe_scheduler == actual_subscribe_scheduler
Ejemplo n.º 10
0
    def test_immediate_singleton(self):
        scheduler = [ImmediateScheduler(), ImmediateScheduler.singleton()]
        assert scheduler[0] is scheduler[1]

        gate = [threading.Semaphore(0), threading.Semaphore(0)]
        scheduler = [None, None]

        def run(idx):
            scheduler[idx] = ImmediateScheduler()
            gate[idx].release()

        for idx in (0, 1):
            threading.Thread(target=run, args=(idx, )).start()
            gate[idx].acquire()

        assert scheduler[0] is not None
        assert scheduler[1] is not None
        assert scheduler[0] is scheduler[1]
Ejemplo n.º 11
0
    def test_immediate_recursive1(self):
        scheduler = ImmediateScheduler()
        xx = 0
        yy = 0

        def action(scheduler, state=None):
            nonlocal xx
            xx = state

            def inner_action(scheduler, state=None):
                nonlocal yy
                yy = state
                return Disposable()

            return scheduler.schedule(inner_action, 43)

        scheduler.schedule(action, 42)
        assert xx == 42
        assert yy == 43
Ejemplo n.º 12
0
    def subscribe(
            observer: abc.ObserverBase[Any],
            scheduler: Optional[abc.SchedulerBase] = None
    ) -> abc.DisposableBase:
        _scheduler = scheduler or ImmediateScheduler.singleton()

        def action(scheduler: abc.SchedulerBase, state: Any) -> None:
            observer.on_error(exception_)

        return _scheduler.schedule(action)
Ejemplo n.º 13
0
    def test_immediate_extend(self):
        class MyScheduler(ImmediateScheduler):
            pass

        scheduler = [
            MyScheduler(),
            MyScheduler.singleton(),
            ImmediateScheduler.singleton(),
        ]
        assert scheduler[0] is scheduler[1]
        assert scheduler[0] is not scheduler[2]
Ejemplo n.º 14
0
    def subscribe(
            observer: abc.ObserverBase[Any],
            scheduler_: Optional[abc.SchedulerBase] = None
    ) -> abc.DisposableBase:

        _scheduler = scheduler or scheduler_ or ImmediateScheduler.singleton()

        def action(_: abc.SchedulerBase, __: Any) -> None:
            observer.on_completed()

        return _scheduler.schedule(action)
Ejemplo n.º 15
0
    def subscribe(
            observer: abc.ObserverBase[_T],
            scheduler: Optional[abc.SchedulerBase] = None
    ) -> abc.DisposableBase:
        try:
            result = factory(scheduler or ImmediateScheduler.singleton())
        except Exception as ex:  # By design. pylint: disable=W0703
            return throw(ex).subscribe(observer)

        result = from_future(result) if isinstance(result, Future) else result
        return result.subscribe(observer, scheduler=scheduler)
Ejemplo n.º 16
0
 def test_immediate_now(self):
     scheduler = ImmediateScheduler()
     diff = scheduler.now - default_now()
     assert abs(diff) <= timedelta(milliseconds=1)
Ejemplo n.º 17
0
        def subscribe(
            observer: abc.ObserverBase[_T],
            scheduler: Optional[abc.SchedulerBase] = None,
        ) -> abc.DisposableBase:
            scheduler = scheduler or ImmediateScheduler.singleton()

            queue: List[Observable[_T]] = []
            m = SerialDisposable()
            d = CompositeDisposable(m)
            active_count = 0
            is_acquired = False

            def ensure_active():
                nonlocal is_acquired

                is_owner = False
                if queue:
                    is_owner = not is_acquired
                    is_acquired = True

                def action(scheduler: abc.SchedulerBase, state: Any = None):
                    nonlocal is_acquired, active_count

                    if queue:
                        work = queue.pop(0)
                    else:
                        is_acquired = False
                        return

                    sad = SingleAssignmentDisposable()
                    d.add(sad)

                    def on_next(value: _T) -> None:
                        nonlocal active_count

                        observer.on_next(value)
                        result = None
                        try:
                            result = mapper(value)
                        except Exception as ex:
                            observer.on_error(ex)
                            return

                        queue.append(result)
                        active_count += 1
                        ensure_active()

                    def on_complete() -> None:
                        nonlocal active_count

                        d.remove(sad)
                        active_count -= 1
                        if active_count == 0:
                            observer.on_completed()

                    sad.disposable = work.subscribe(on_next,
                                                    observer.on_error,
                                                    on_complete,
                                                    scheduler=scheduler)
                    m.disposable = scheduler.schedule(action)

                if is_owner:
                    m.disposable = scheduler.schedule(action)

            queue.append(source)
            active_count += 1
            ensure_active()
            return d
Ejemplo n.º 18
0
 def run(idx):
     scheduler[idx] = ImmediateScheduler()
     gate[idx].release()