def test_currentthread_scheduleaction(self): scheduler = CurrentThreadScheduler() ran = [False] def action(scheduler, state=None): ran[0] = True scheduler.schedule(action) assert ran[0] is True
def test_currentthread_scheduleaction(self): scheduler = CurrentThreadScheduler() ran = [False] def action(scheduler, state=None): ran[0] = True scheduler.schedule(action) assert ran[0] == True
def test_currentthread_scheduleaction(): scheduler = CurrentThreadScheduler() ran = False def action(scheduler, state=None): nonlocal ran ran = True scheduler.schedule(action) assert ran == True
def test_currentthread_schedule(self): scheduler = CurrentThreadScheduler() ran = False def action(scheduler, state=None): nonlocal ran ran = True scheduler.schedule(action) assert ran is True
def test_currentthread_schedule_error(self): scheduler = CurrentThreadScheduler() class MyException(Exception): pass def action(scheduler, state=None): raise MyException() with pytest.raises(MyException): scheduler.schedule(action)
def test_currentthread_scheduleactionnested(self): scheduler = CurrentThreadScheduler() ran = [False] def action(scheduler, state=None): def inner_action(scheduler, state=None): ran[0] = True return scheduler.schedule(inner_action) scheduler.schedule(action) assert ran[0] == True
def test_currentthread_schedule_block(self): scheduler = CurrentThreadScheduler() ran = False def action(scheduler, state=None): nonlocal ran ran = True t = scheduler.now scheduler.schedule_relative(0.2, action) t = scheduler.now - t assert ran is True assert t >= timedelta(seconds=0.2)
def test_currentthread_schedule_nested(self): scheduler = CurrentThreadScheduler() ran = False def action(scheduler, state=None): def inner_action(scheduler, state=None): nonlocal ran ran = True return scheduler.schedule(inner_action) scheduler.schedule(action) assert ran is True
def test_currentthread_scheduleactionerror(self): scheduler = CurrentThreadScheduler() class MyException(Exception): pass def action(scheduler, state=None): raise MyException() try: return scheduler.schedule(action) except MyException: assert True
def test_currentthread_scheduleactionnested(): scheduler = CurrentThreadScheduler() ran = False def action(scheduler, state=None): def inner_action(scheduler, state=None): nonlocal ran ran = True return scheduler.schedule(inner_action) scheduler.schedule(action) assert ran == True
def test_currentthread_schedule_error(self): scheduler = CurrentThreadScheduler() class MyException(Exception): pass def action(scheduler, state=None): raise MyException() exc = None try: scheduler.schedule(action) except Exception as e: exc = e finally: assert isinstance(exc, MyException)
def test_currentthread_singleton(self): scheduler = [CurrentThreadScheduler(), CurrentThreadScheduler()] assert scheduler[0] is scheduler[1] gate = [threading.Semaphore(0), threading.Semaphore(0)] scheduler = [None, None] def run(idx): scheduler[idx] = CurrentThreadScheduler() 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 not scheduler[1]
def test_currentthread_ensuretrampoline_nested(self): scheduler = CurrentThreadScheduler() ran1, ran2 = [False], [False] def outer_action(scheduler, state): def inner_action1(scheduler, state): ran1[0] = True scheduler.ensure_trampoline(inner_action1) def inner_action2(scheduler, state): ran2[0] = True return scheduler.ensure_trampoline(inner_action2) scheduler.ensure_trampoline(outer_action) assert ran1[0] == True assert ran2[0] == True
def test_spot_instance_checker_through_404(requests_mock): requests_mock.get(INSTANCE_ACTION_URL, text="test", status_code=404) o = spot_instance_check_observable(0.1).pipe(ops.merge(rx.timer(0.5)), ops.first()) def on_next(x): assert x == 0 o.subscribe(on_next=on_next, scheduler=CurrentThreadScheduler())
def test_currentthread_ensuretrampoline_and_cancel(self): scheduler = CurrentThreadScheduler() ran1, ran2 = [False], [False] def outer_action(scheduler, state): def inner_action1(scheduler, state): ran1[0] = True def inner_action2(scheduler, state): ran2[0] = True d = scheduler.schedule(inner_action2) d.dispose() return scheduler.schedule(inner_action1) scheduler.ensure_trampoline(outer_action) assert ran1[0] == True assert ran2[0] == False
def test_currentthread_ensuretrampoline_nested(): scheduler = CurrentThreadScheduler() ran1, ran2 = False, False def outer_action(scheduler, state): def inner_action1(scheduler, state): nonlocal ran1 ran1 = True scheduler.ensure_trampoline(inner_action1) def inner_action2(scheduler, state): nonlocal ran2 ran2 = True return scheduler.ensure_trampoline(inner_action2) scheduler.ensure_trampoline(outer_action) assert ran1 == True assert ran2 == True
def test_currentthread_ensuretrampoline(self): scheduler = CurrentThreadScheduler() ran1, ran2 = False, False def outer_action(scheduer, state=None): def action1(scheduler, state=None): nonlocal ran1 ran1 = True scheduler.schedule(action1) def action2(scheduler, state=None): nonlocal ran2 ran2 = True return scheduler.schedule(action2) scheduler.ensure_trampoline(outer_action) assert ran1 is True assert ran2 is True
def test_currentthread_ensuretrampoline_and_cancel(): scheduler = CurrentThreadScheduler() ran1, ran2 = False, False def outer_action(scheduler, state): def inner_action1(scheduler, state): nonlocal ran1 ran1 = True def inner_action2(scheduler, state): nonlocal ran2 ran2 = True d = scheduler.schedule(inner_action2) d.dispose() return scheduler.schedule(inner_action1) scheduler.ensure_trampoline(outer_action) assert ran1 == True assert ran2 == False
def test_currentthread_ensuretrampoline_and_cancel(self): scheduler = CurrentThreadScheduler() ran1, ran2 = False, False def outer_action(scheduler, state): def inner_action1(scheduler, state): nonlocal ran1 ran1 = True def inner_action2(scheduler, state): nonlocal ran2 ran2 = True d = scheduler.schedule(inner_action2) d.dispose() return scheduler.schedule(inner_action1) scheduler.ensure_trampoline(outer_action) assert ran1 is True assert ran2 is False
def test_currentthread_ensuretrampoline_and_canceltimed(): scheduler = CurrentThreadScheduler() ran1, ran2 = False, False def outer_action(scheduler, state): def inner_action1(scheduler, state): nonlocal ran1 ran1 = True def inner_action2(scheduler, state): nonlocal ran2 ran2 = True d = scheduler.schedule_relative(timedelta(milliseconds=500), inner_action2) d.dispose() return scheduler.schedule(inner_action1) scheduler.ensure_trampoline(outer_action) assert ran1 == True assert ran2 == False
def concat(cls, *args): """Concatenates all the observable sequences. 1 - res = Observable.concat(xs, ys, zs) 2 - res = Observable.concat([xs, ys, zs]) Returns an observable sequence that contains the elements of each given sequence, in sequential order. """ scheduler = CurrentThreadScheduler() if isinstance(args[0], list) or isinstance(args[0], Enumerable): sources = args[0] else: sources = list(args) def subscribe(observer): subscription = SerialDisposable() cancelable = SerialDisposable() enum = iter(sources) is_disposed = [] def action(action1, state=None): if is_disposed: return def on_completed(): cancelable.disposable = scheduler.schedule(action) try: current = next(enum) except StopIteration: observer.on_completed() except Exception as ex: observer.on_error(ex) else: d = SingleAssignmentDisposable() subscription.disposable = d d.disposable = current.subscribe(observer.on_next, observer.on_error, on_completed) cancelable.disposable = scheduler.schedule(action) def dispose(): is_disposed.append(True) return CompositeDisposable(subscription, cancelable, Disposable.create(dispose)) return AnonymousObservable(subscribe)
def test_spot_instance_checker_terminate(requests_mock): body = {"action": "terminate", "time": "2017-09-18T08:22:00Z"} requests_mock.get(INSTANCE_ACTION_URL, json=body) o = spot_instance_check_observable(0.1).pipe(ops.merge(rx.timer(0.5)), ops.first()) def on_next(x): assert isinstance(x, CheckerMessage) assert x.checker_type == "spot_instance" assert x.body == f'"spot/instance-action": {body}' o.subscribe(on_next=on_next, scheduler=CurrentThreadScheduler())
def test_currentthread_ensuretrampoline_and_canceltimed(self): scheduler = CurrentThreadScheduler() ran1, ran2 = False, False def outer_action(scheduler, state): def inner_action1(scheduler, state): nonlocal ran1 ran1 = True def inner_action2(scheduler, state): nonlocal ran2 ran2 = True t = scheduler.now + timedelta(seconds=0.5) d = scheduler.schedule_absolute(t, inner_action2) d.dispose() return scheduler.schedule(inner_action1) scheduler.ensure_trampoline(outer_action) assert ran1 is True assert ran2 is False
def test_spot_instance_checker_observer(requests_mock): with mock.patch("igata.checkers.aws.observers.logger.info") as mock_method: requests_mock.get(INSTANCE_ACTION_URL, json={ "action": "terminate", "time": "2017-09-18T08:22:00Z" }) o = spot_instance_check_observable(0.1).pipe( ops.merge(rx.timer(0.3)), ops.take(2), ops.filter(lambda x: isinstance(x, CheckerMessage))) o.subscribe(CheckersObserver(), scheduler=CurrentThreadScheduler()) mock_method.assert_called_once()
def to_iterable(source, scheduler): condition = config["concurrency"].Condition() notifications = [] def send_notification(n): condition.acquire() notifications.append(n) condition.notify() # signal that a new item is available condition.release() def on_next(v): send_notification(('N', v)) return Continue() def on_error(exc): send_notification(('E', exc)) def on_completed(): send_notification(('C', None)) observer = AnonymousObserver(on_next=on_next, on_error=on_error, on_completed=on_completed) source.subscribe(observer, scheduler, CurrentThreadScheduler()) def gen(): while True: condition.acquire() while not len(notifications): condition.wait() kind, value = notifications.pop(0) if kind == "E": raise value if kind == "C": return # StopIteration condition.release() yield value return Enumerator(gen())
def __init__(self, fun, n_workers): """ :param pauser: :param fun: wchr -> lbv.apply_async() """ self.fun = fun self.curr_tr_sched = CurrentThreadScheduler() self.thr_pool_sched = ThreadPoolScheduler() self.sub_new_jobs = Subject() self.sub_done_jobs = Subject() self.sub_freeworkers = Subject() self.n_workers = n_workers self.sub_new_jobs \ .map(self.on_next_njob_map) \ .flat_map(lambda ar: Observable.just(ar).observe_on(self.thr_pool_sched).map(lambda ar2: ar2.result())) \ .observe_on(self.curr_tr_sched) \ .subscribe(on_next=self.job_done)
def wait(self, timeout=-1): """ Wait until the result is available or until `timeout` miliseconds pass. Возвращает 'beep beep', если сработал таймер (таймер истек раньше, чем закончились расчеты), или 0, если расчеты закончились раньше """ if self.stream is None: return 0 if timeout < 0: lasty = self.stream.to_blocking().last_or_default(0) return 0 timer_message = 'beep beep' timer = Observable.timer(timeout).map(lambda x: timer_message) lasty = self.stream.last_or_default(0) \ .amb(timer) \ .observe_on(CurrentThreadScheduler()) \ .to_blocking() \ .first() return timer_message if lasty == timer_message else 0
def abort(self): """ Отменяет расчет, врзвращает количество отмененных задач """ try: if self.async_res is None: return 0 if self.stream is None: return 0 # if self.async_res. results = [] self.stream \ .observe_on(CurrentThreadScheduler()) \ .filter(lambda x: not isinstance(x, tuple)) \ .subscribe(on_next=lambda x: results.append(x)) self.async_res.abort() self.wait() return len(results) except: return 0
def on_next_left(left_elem): ack = Ack() with self.lock: if isinstance(state[0], self.WaitForLeftOrRight): new_state = self.WaitForRightOrInner(left_ack=ack) elif isinstance(state[0], self.WaitForLeft): state_: source.WaitForLeft = state[0] new_state = self.Active(left_ack=ack, right_elem=state_.right_elem, right_ack=state_.right_ack, upper_ack=Continue()) elif isinstance(state[0], source.Completed): return stop_ack else: raise NotImplementedError state[0] = new_state class ChildObserver(Observer): def __init__(self, out: Observer, scheduler): self.out = out self.scheduler = scheduler def on_next(self, inner_left): ack = Ack() has_right_elem = False right_elem = None with source.lock: if isinstance(state[0], source.WaitForRightOrInner): # not much to do state_typed: source.WaitForRightOrInner = state[0] state[0] = source.WaitForRight( inner_left_elem=inner_left, left_ack=state_typed.left_ack, inner_left_ack=ack, left_elem=source.selector_left(left_elem)) elif isinstance(state[0], source.Active): # send zipped item to observer state_typed: source.Active = state[0] has_right_elem = True right_elem = state_typed.right_elem state[0] = source.Active( left_ack=state_typed.left_ack, right_elem=state_typed.right_elem, right_ack=state_typed.right_ack, upper_ack=ack) elif isinstance(state[0], source.Completed): return stop_ack else: raise NotImplementedError if has_right_elem: # send triple to observer zipped_elem = source.selector( source.selector_left(left_elem), right_elem, inner_left) upper_ack = observer.on_next(zipped_elem) # race condition with on_completed if isinstance(state[0], source.Active): typed_state: source.Active = state[0] typed_state.upper_ack = upper_ack if isinstance(upper_ack, Stop): with source.lock: state[0] = source.Completed() else: def _(v): if isinstance(v, Stop): with source.lock: state[0] = source.Completed() upper_ack.observe_on(scheduler).subscribe(_) return upper_ack else: return ack def on_error(self, err): with source.lock: state[0] = source.Completed() observer.on_error(err) def on_completed(self): back_pressure_right = False back_pressure_left = False left_ack = None right_ack = None upper_ack = None complete_observer = False with source.lock: if isinstance(state[0], source.Active): # normal complete if right_completed[0]: state[0] = source.Completed() complete_observer = True else: # request new left and new right state_typed: source.Active = state[0] back_pressure_right = True back_pressure_left = True left_ack = state_typed.left_ack right_ack = state_typed.right_ack upper_ack = state_typed.upper_ack state[0] = source.WaitForLeftOrRight() elif isinstance(state[0], source.WaitForRightOrInner): # empty inner observable state_typed: source.WaitForRight = state[0] # count up number of inner completed (without right completed) inner_left_completed[0] += 1 state[0] = source.WaitForLeftOrRight() back_pressure_left = True left_ack = state_typed.left_ack upper_ack = Continue() if complete_observer: observer.on_completed() if back_pressure_left or back_pressure_right: def _(v): if isinstance(v, Stop): with source.lock: state[0] = source.Completed() upper_ack.subscribe(_) if back_pressure_left: # upper_ack should not be Stop if isinstance(upper_ack, Continue): left_ack.on_next(upper_ack) left_ack.on_completed() else: upper_ack.observe_on(scheduler).subscribe(left_ack) if back_pressure_right: if isinstance(upper_ack, Continue): right_ack.on_next(upper_ack) right_ack.on_completed() else: upper_ack.observe_on(scheduler).subscribe( right_ack) child = self.selector_inner(left_elem) child_observer = ChildObserver(observer, scheduler) disposable = child.subscribe(child_observer, scheduler, CurrentThreadScheduler()) inner_disposable.disposable = disposable return ack
def test_currentthread_now(self): scheduler = CurrentThreadScheduler() diff = scheduler.now - default_now() assert abs(diff) < timedelta(milliseconds=1)
def test_currentthread_now_units(self): scheduler = CurrentThreadScheduler() diff = scheduler.now sleep(0.1) diff = scheduler.now - diff assert timedelta(milliseconds=80) < diff < timedelta(milliseconds=180)
def run(idx): scheduler[idx] = CurrentThreadScheduler() gate[idx].release()
def test_currentthread_now(self): scheduler = CurrentThreadScheduler() res = scheduler.now - datetime.utcnow() assert res < timedelta(milliseconds=1000)
def test_currentthread_now(self): scheduler = CurrentThreadScheduler() res = scheduler.now() - datetime.utcnow() assert res < timedelta(milliseconds=1000)