async def test_decrease(self): resources = Resources(a=40, b=40) async with resources.borrow(a=20, b=20): await resources.decrease(a=10, b=10) async with until(time == 10): async with resources.borrow(a=20, b=20): assert False assert time == 10 async with resources.borrow(a=30, b=30): assert True async with until(time == 20): async with resources.borrow(a=40, b=40): assert False assert time == 20
async def test_for_interval(): expected_time = 5 async with until(time == 60): async for _ in interval(5): assert time.now == expected_time expected_time += 5 assert time.now == 60
async def test_release_self(self): """Condition is set synchronously""" flag = Flag() entered, exited = False, False async with until(flag): entered = True await flag.set() # get interrupted immediately exited = True assert entered and not exited
async def test_before(self): start = time.now assert not (time < time.now) assert (time < time.now + 20) assert ~(time < time.now) assert not ~(time < time.now + 20) await (time < time.now + 20) async with until(time + 20): await (time < time.now) assert time.now == start + 20
async def test_release_early(self): """Condition is set before entering interrupt scope""" flag = Flag() await flag.set() entered, exited = False, False async with until(flag): entered = True await instant exited = True assert entered and not exited
async def car(end: float): async with until(time == end): while True: print('Start parking at %d' % time.now) parking_duration = 5 await (time + parking_duration) print('Start driving at %d' % time.now) trip_duration = 2 await (time + trip_duration)
async def test_release_concurrent(self): """Condition is set concurrently""" flag = Flag() entered, exited = False, False async with until(flag) as scope: scope.do(flag.set()) await instant # start task entered = True await instant exited = True assert entered and not exited
async def _simulate(self, end): print(f"Starting simulation at {time.now}") async with until(time == end) if end else Scope() as while_running: for pool in self.pools: while_running.do(pool.run(), volatile=True) for job_input, job_reader in self._job_generators: while_running.do(self._queue_jobs(job_input, job_reader)) while_running.do(self.job_scheduler.run()) for controller in self.controllers: while_running.do(controller.run(), volatile=True) while_running.do(self.monitoring.run(), volatile=True) self.duration = time.now print(f"Finished simulation at {self.duration}")
async def test_reuse(): async def make_job(): async with Scope() as scope: running_job = scope.do(run_job()) running_job.cancel() await running_job.done async def run_job(): await (time + 100) async with until(time == 500) as running: activity = running.do(make_job()) await activity
async def test_until(): async def scheduler(): async with Scope() as scope: while True: scope.do(run_job()) await (time + 500) async def run_job(): await (time + 1000) async with until(time == 500) as running: activity = running.do(scheduler()) assert time.now == 500 with pytest.raises(TaskClosed): await activity
async def test_previous_before(self): start, delay = time.now, 20 async with until(time == start + delay): await (time < start - 5) # await moment in the past assert False, "Before in the past should never pass" assert start + delay == time.now
async def test_previous_after(self): start, delay = time.now, 20 async with until(time == start + delay): await (time >= start - 5) # await moment in the past assert True, "After in the past should always pass" assert start == time.now
async def test_representable(): async with Scope() as scope: str(scope), repr(scope) async with until(time == 200) as scope: str(scope), repr(scope)