def setUp(self) -> None: self.event_loop: AbstractEventLoop = asyncio.get_event_loop() self.decider: ReplayDecider = Mock() self.decider.get_and_increment_next_id = MagicMock(return_value="0") self.decider.event_loop = Mock() self.future = self.event_loop.create_future() self.decider.event_loop.create_future = MagicMock( return_value=self.future) self.itask = ITask(decider=self.decider)
def get_version(change_id: str, min_supported: int, max_supported: int): from cadence.decision_loop import ITask from cadence.decision_loop import DecisionContext task: ITask = ITask.current() decision_context: DecisionContext = task.decider.decision_context return decision_context.get_version(change_id, min_supported, max_supported)
def new_activity_stub(activities_cls): from cadence.decision_loop import ITask task: ITask = ITask.current() assert task cls = activities_cls() cls._decision_context = task.decider.decision_context return cls
def new_activity_stub(activities_cls, retry_parameters: RetryParameters = None): from cadence.decision_loop import ITask task: ITask = ITask.current() assert task cls = activities_cls() cls._decision_context = task.decider.decision_context cls._retry_parameters = retry_parameters return cls
class TestAwaitTill(TestCase): def setUp(self) -> None: self.event_loop: AbstractEventLoop = asyncio.get_event_loop() self.decider: ReplayDecider = Mock() self.decider.get_and_increment_next_id = MagicMock(return_value="0") self.decider.event_loop = Mock() self.future = self.event_loop.create_future() self.decider.event_loop.create_future = MagicMock( return_value=self.future) self.itask = ITask(decider=self.decider) def tearDown(self) -> None: self.task.cancel() def test_await_till(self): self.task = self.event_loop.create_task( self.itask.await_till(lambda *args: None)) run_once(self.event_loop) assert self.itask.awaited def test_await_till_no_progress(self): self.task = self.event_loop.create_task( self.itask.await_till(lambda *args: None)) run_once(self.event_loop) assert self.itask.awaited run_once(self.event_loop) assert self.itask.awaited def test_unblock(self): blocked = True def check_blocked(): nonlocal blocked return not blocked self.task = self.event_loop.create_task( self.itask.await_till(check_blocked)) run_once(self.event_loop) blocked = False self.itask.unblock() run_once(self.event_loop) assert not self.itask.awaited
def get_logger(name): from cadence.decision_loop import ITask task: ITask = ITask.current() return task.decider.decision_context.get_logger(name)
def new_random() -> random.Random: from cadence.decision_loop import ITask task: ITask = ITask.current() return task.decider.decision_context.new_random()
def random_uuid() -> uuid.UUID: from cadence.decision_loop import ITask task: ITask = ITask.current() return task.decider.decision_context.random_uuid()
def now() -> datetime.datetime: from cadence.decision_loop import ITask task: ITask = ITask.current() now_in_ms = task.decider.decision_context.current_time_millis() return datetime.datetime.fromtimestamp(now_in_ms / 1000)
def current_time_millis() -> int: from cadence.decision_loop import ITask task: ITask = ITask.current() return task.decider.decision_context.current_time_millis()
async def sleep(seconds: int): from cadence.decision_loop import ITask task: ITask = ITask.current() await task.decider.decision_context.schedule_timer(seconds)
async def await_till(c: Callable, timeout_seconds: int = 0) -> bool: from cadence.decision_loop import ITask task: ITask = ITask.current() assert task return await task.await_till(c, timeout_seconds)
def interceptor(*args, **kwargs): from cadence.decision_loop import ITask task: ITask = ITask.current() if not task.decider.decision_context.is_replaying(): return fn(*args, **kwargs)
def get_execution_id(): from cadence.decision_loop import ITask task: ITask = ITask.current() return task.decider.execution_id
def get_workflow_id(): from cadence.decision_loop import ITask task: ITask = ITask.current() return task.decider.workflow_id