def test_ticker_time(): Tstart = time.now() tx = time.Ticker(3 * dt) tnow = tx.c.recv() Tend = time.now() assert type(tnow) is type(Tstart) assert Tstart < tnow < Tend assert ((tnow - Tstart) / dt) >= 3
def waitfor(cond): tstart = time.now() while 1: if cond(): return t = time.now() if (t - tstart) > 1*time.second: raise AssertionError("timeout waiting") time.sleep(1E-6) # NOTE sleep(0) consumes lot of CPU under gevent
def test_already_canceled(): parent, pcancel = context.with_cancel(bg) assertCtx(parent, Z) pcancel() assertCtx(parent, Z, err=C, done=Y) ctxC, _ = context.with_cancel(parent) assert ctxC.done() != parent.done() assertCtx(parent, Z, err=C, done=Y) # no ctxC in children assertCtx(ctxC, Z, err=C, done=Y) ctxT, _ = context.with_timeout(parent, 10 * dt) d = ctxT.deadline() assert ctxT.done() != parent.done() assertCtx(parent, Z, err=C, done=Y) # no ctxT in children assertCtx(ctxT, Z, deadline=d, err=C, done=Y) d = time.now() + 10 * dt ctxD, _ = context.with_deadline(parent, d) assert ctxD.done() != parent.done() assertCtx(parent, Z, err=C, done=Y) # no ctxD in children assertCtx(ctxD, Z, deadline=d, err=C, done=Y) ctxM, _ = context.merge(parent, bg) assert ctxM.done() != parent.done() assertCtx(parent, Z, err=C, done=Y) # no ctxM in children assertCtx(ctxM, Z, err=C, done=Y) ctxV = context.with_value(parent, kHello, "world") assert ctxV.done() == parent.done() assert ctxV.value(kHello) == "world" assertCtx(parent, Z, err=C, done=Y) # no ctxV in children assertCtx(ctxV, Z, err=C, done=Y)
def stop_timer(self): if self._t_start is None: return t = time.now() self._t_total += t - self._t_start self._t_start = None
def test_now(): import time as stdtime assert stdtime is not time def tick(): # cpython 2.7 time.time uses max microsecond precision time.sleep(1 * time.microsecond) t1 = stdtime.time() tick() t2 = time.now() tick() t3 = stdtime.time() tick() t4 = time.now() tick() assert t1 < t2 assert t2 < t3 assert t3 < t4
def test_timer(): # start timers at x5, x7 and x11 intervals an verify that the timers fire # in expected sequence. The times when the timers fire do not overlap in # checked range because intervals are prime and chosen so that they start # overlapping only after 35 (=5·7). tv = [] # timer events Tstart = time.now() t23 = time.Timer(23 * dt) t5 = time.Timer(5 * dt) def _(): tv.append(7) t7f.reset(7 * dt) t7f = time.Timer(7 * dt, f=_) tx11 = time.Ticker(11 * dt) while 1: _, _rx = select( t23.c.recv, # 0 t5.c.recv, # 1 t7f.c.recv, # 2 tx11.c.recv, # 3 ) if _ == 0: tv.append(23) break if _ == 1: tv.append(5) t5.reset(5 * dt) if _ == 2: assert False, "t7f sent to channel; must only call func" if _ == 3: tv.append(11) Tend = time.now() assert (Tend - Tstart) >= 23 * dt assert tv == [5, 7, 5, 11, 7, 5, 5, 7, 11, 23]
def test_timer_misc(): tv = [] Tstart = time.now() c23 = time.after(23 * dt) c5 = time.after(5 * dt) def _(): tv.append(7) t7f.reset(7 * dt) t7f = time.after_func(7 * dt, _) cx11 = time.tick(11 * dt) while 1: _, _rx = select( c23.recv, # 0 c5.recv, # 1 t7f.c.recv, # 2 cx11.recv, # 3 ) if _ == 0: tv.append(23) break if _ == 1: tv.append(5) # NOTE 5 does not rearm in this test because there is no way to # rearm timer create by time.after(). if _ == 2: assert False, "t7f sent to channel; must only call func" if _ == 3: tv.append(11) Tend = time.now() assert (Tend - Tstart) >= 23 * dt assert tv == [5, 7, 11, 7, 7, 11, 23]
def test_deadline(): t0 = time.now() d1 = t0 + 10 * dt d2 = t0 + 20 * dt d3 = t0 + 30 * dt ctx1, cancel1 = context.with_deadline(bg, d2) assert ctx1.done() != bg.done() assertCtx(ctx1, Z, deadline=d2) ctx11 = context.with_value(ctx1, kA, "b") assert ctx11.done() == ctx1.done() assert ctx11.value(kA) == "b" assertCtx(ctx1, {ctx11}, deadline=d2) assertCtx(ctx11, Z, deadline=d2) ctx111, cancel111 = context.with_cancel(ctx11) assert ctx111.done() != ctx11.done assertCtx(ctx1, {ctx11}, deadline=d2) assertCtx(ctx11, {ctx111}, deadline=d2) assertCtx(ctx111, Z, deadline=d2) ctx1111, cancel1111 = context.with_deadline(ctx111, d3) # NOTE deadline > parent assert ctx1111.done() != ctx111.done() assertCtx(ctx1, {ctx11}, deadline=d2) assertCtx(ctx11, {ctx111}, deadline=d2) assertCtx(ctx111, {ctx1111}, deadline=d2) assertCtx(ctx1111, Z, deadline=d2) # NOTE not d3 ctx12, cancel12 = context.with_deadline(ctx1, d1) assert ctx12.done() != ctx1.done() assertCtx(ctx1, {ctx11, ctx12}, deadline=d2) assertCtx(ctx11, {ctx111}, deadline=d2) assertCtx(ctx111, {ctx1111}, deadline=d2) assertCtx(ctx1111, Z, deadline=d2) assertCtx(ctx12, Z, deadline=d1) ctxM, cancelM = context.merge(ctx1111, ctx12) assert ctxM.done() != ctx1111.done() assert ctxM.done() != ctx12.done() assert ctxM.value(kA) == "b" assertCtx(ctx1, {ctx11, ctx12}, deadline=d2) assertCtx(ctx11, {ctx111}, deadline=d2) assertCtx(ctx111, {ctx1111}, deadline=d2) assertCtx(ctx1111, {ctxM}, deadline=d2) assertCtx(ctx12, {ctxM}, deadline=d1) assertCtx(ctxM, Z, deadline=d1) time.sleep(11 * dt) assertCtx(ctx1, {ctx11}, deadline=d2) assertCtx(ctx11, {ctx111}, deadline=d2) assertCtx(ctx111, {ctx1111}, deadline=d2) assertCtx(ctx1111, Z, deadline=d2) assertCtx(ctx12, Z, deadline=d1, err=D, done=Y) assertCtx(ctxM, Z, deadline=d1, err=D, done=Y) # explicit cancel first -> err=canceled instead of deadlineExceeded for i in range(2): cancel1() assertCtx(ctx1, Z, deadline=d2, err=C, done=Y) assertCtx(ctx11, Z, deadline=d2, err=C, done=Y) assertCtx(ctx111, Z, deadline=d2, err=C, done=Y) assertCtx(ctx1111, Z, deadline=d2, err=C, done=Y) assertCtx(ctx12, Z, deadline=d1, err=D, done=Y) assertCtx(ctxM, Z, deadline=d1, err=D, done=Y) # with_timeout ctx, cancel = context.with_timeout(bg, 10 * dt) assert ctx.done() != bg.done() d = ctx.deadline() assert abs(d - (time.now() + 10 * dt)) < 1 * dt assertCtx(ctx, Z, deadline=d) time.sleep(11 * dt) assertCtx(ctx, Z, deadline=d, err=D, done=Y)
def start_timer(self): if self._t_start is not None: return self._t_start = time.now()