def test_Cond_signal_generation(): def test(i, m, c, running, awake): m.lock() running.send(True) c.wait() awake.send(i) m.unlock() @maintask def main(): m = Mutex() c = Cond(m) n = 100 running = makechan(n) awake = makechan(n) for i in range(n): go(test, i, m, c, running, awake) if i > 0: a = awake.recv() assert a == (i - 1), "wrong coroutine woke up: want %d, got %d" % (i-1, a) running.recv() with m: c.signal() run()
def test_RWMutex_concurrent_readers(): def reader(m, clocked, cunlock, cdone): m.rlock() clocked.send(True) cunlock.recv() m.runlock() cdone.send(True) def test_readers(num): m = RWMutex() clocked = makechan() cunlock = makechan() cdone = makechan() for i in range(num): go(reader, m, clocked, cunlock, cdone) for i in range(num): clocked.recv() for i in range(num): cunlock.send(True) for i in range(num): cdone.recv() @maintask def main(): test_readers(1) test_readers(3) test_readers(4) run()
def test_RLocker(): wl = RWMutex() rl = wl.RLocker() wlocked = makechan(1) rlocked = makechan(1) n = 10 def test(): for i in range(n): rl.lock() rl.lock() rlocked.send(True) wl.lock() wlocked.send(True) @maintask def main(): go(test) for i in range(n): rlocked.recv() rl.unlock() ret = select(wlocked.if_recv(), default) assert ret != wlocked.if_recv(), "RLocker didn't read-lock it" rl.unlock() wlocked.recv() ret = select(rlocked.if_recv(), default) assert ret != rlocked.if_recv(), "RLocker didn't respect the write lock" wl.unlock() run()
def test_select_simple(): rlist = [] def fibonacci(c, quit): x, y = 0, 1 while True: ret = select(c.if_send(x), quit.if_recv()) if ret == c.if_send(x): x, y = y, x+y elif ret == quit.if_recv(): return @maintask def main(): c = makechan() quit = makechan() def f(): for i in range(10): rlist.append(c.recv()) print(rlist) quit.send(0) go(f) fibonacci(c, quit) run() assert rlist == [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
def test_async_with_blocking_channel(self): def sender(c): unblocked_sent = 0 for i in range(10): c.send(True) unblocked_sent += 1 assert unblocked_sent == 10 c.send(True) @maintask def main(): c = makechan(10) go(sender, c) unblocked_recv = [] for i in range(11): unblocked_recv.append(c.recv()) assert len(unblocked_recv) == 11 run()
def test_recv_counter(self): import random numbers = list(range(20)) random.shuffle(numbers) rlist = [] def counter(n, ch): ch.recv() rlist.append(n) @maintask def main(): ch = makechan() for each in numbers: go(counter, each, ch) for each in numbers: ch.send(None) run() numbers.sort() rlist.sort() assert rlist == numbers
def test_select_buffer(): rlist = [] def test(c, quit): x = 0 while True: ret = select(c.if_send(x), quit.if_recv()) if ret == c.if_send(x): x = x + 1 elif ret == quit.if_recv(): return @maintask def main(): c = makechan(5, label="c") quit = makechan(label="quit") def f(): for i in range(5): v = c.recv() rlist.append(v) quit.send(0) go(f) test(c, quit) run() assert rlist == [0, 1, 2, 3, 4]
def test_select_simple(): rlist = [] def fibonacci(c, quit): x, y = 0, 1 while True: ret = select(c.if_send(x), quit.if_recv()) if ret == c.if_send(x): x, y = y, x + y elif ret == quit.if_recv(): return @maintask def main(): c = makechan() quit = makechan() def f(): for i in range(10): rlist.append(c.recv()) print(rlist) quit.send(0) go(f) fibonacci(c, quit) run() assert rlist == [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
def test_multiple_sender(self): rlist = [] sent = [] def f(c): c.send("ok") def f1(c): c.send("eof") def f2(c): while True: data = c.recv() sent.append(data) if data == "eof": return rlist.append(data) @maintask def main(): c = makechan() go(f, c) go(f1, c) go(f2, c) run() assert rlist == ['ok'] assert len(sent) == 2 assert "eof" in sent
def test_cooperative(): output = [] def print_(*args): output.append(args) def Loop(i): for x in range(3): gosched() print_("schedule", i) @maintask def main(): go(Loop, 1) go(Loop, 2) run() assert output == [ ('schedule', 1), ('schedule', 2), ('schedule', 1), ('schedule', 2), ('schedule', 1), ('schedule', 2), ]
def test_multiple_timer(): r1 = [] def f1(now, t, g): r1.append(now) g.ready() r2 = [] def f2(now, t): r2.append(now) @maintask def main(): T1 = 0.4 * SECOND T2 = 0.1 * SECOND t1 = Timer(f1, T1, args=(proc.current(),)) t2 = Timer(f2, T2) now = nanotime() t1.start() t2.start() park() assert r1[0] > r2[0] assert (now + T1 - DELTA0) <= r1[0] <= (now + T1 + DELTA), r1[0] assert (now + T2 - DELTA0) <= r2[0] <= (now + T2 + DELTA), r2[0] run()
def test_multiple_timer(): r1 = [] def f1(now, t, g): r1.append(now) g.ready() r2 = [] def f2(now, t): r2.append(now) @maintask def main(): T1 = 0.4 * SECOND T2 = 0.1 * SECOND t1 = Timer(f1, T1, args=(proc.current(), )) t2 = Timer(f2, T2) now = nanotime() t1.start() t2.start() park() assert r1[0] > r2[0] assert (now + T1 - DELTA0) <= r1[0] <= (now + T1 + DELTA), r1[0] assert (now + T2 - DELTA0) <= r2[0] <= (now + T2 + DELTA), r2[0] run()
def test_WaitGroup(): def test_waitgroup(wg1, wg2): n = 16 wg1.add(n) wg2.add(n) exited = makechan(n) def f(i): wg1.done() wg2.wait() exited.send(True) for i in range(n): go(f, i) wg1.wait() for i in range(n): ret = select(exited.if_recv(), default) assert ret != exited.if_recv(), "WaitGroup released group too soon" wg2.done() for i in range(16): exited.recv() @maintask def main(): wg1 = WaitGroup() wg2 = WaitGroup() for i in range(8): test_waitgroup(wg1, wg2) run()
def test_sleep(): @maintask def main(): PERIOD = 0.1 * SECOND start = nanotime() sleep(PERIOD) diff = nanotime() - start assert PERIOD - DELTA0 <= diff <= PERIOD + DELTA run()
def test_WaitGroup_raises(): @maintask def main(): wg = WaitGroup() with raises(PanicError): wg.add(1) wg.done() wg.done() run()
def test_RWMutex(): activity = AtomicLong(0) def reader(rwm, num_iterations, activity, cdone): print("reader") for i in range(num_iterations): rwm.rlock() n = activity.add(1) assert n >= 1 and n < 10000, "rlock %d" % n for i in range(100): continue activity.add(-1) rwm.runlock() cdone.send(True) def writer(rwm, num_iterations, activity, cdone): for i in range(num_iterations): rwm.lock() n = activity.add(10000) assert n == 10000, "wlock %d" % n for i in range(100): continue activity.add(-10000) rwm.unlock() cdone.send(True) def hammer_rwmutex(num_readers, num_iterations): activity = AtomicLong(0) rwm = RWMutex() cdone = makechan() go(writer, rwm, num_iterations, activity, cdone) for i in range(int(num_readers / 2)): go(reader, rwm, num_iterations, activity, cdone) go(writer, rwm, num_iterations, activity, cdone) for i in range(num_readers): go(reader, rwm, num_iterations, activity, cdone) for i in range(2 + num_readers): cdone.recv() @maintask def main(): n = 1000 hammer_rwmutex(1, n) hammer_rwmutex(3, n) hammer_rwmutex(10, n) run()
def test_Tick(): rlist = [] @maintask def main(): ticker_chan = Tick(0.1 * SECOND) for i in range(3): rlist.append(ticker_chan.recv()) run() assert len(rlist) == 3
def test_run(): output = [] def print_(*args): output.append(args) def f(i): print_(i) go(f, 1) go(f, 2) run() assert output == [(1,), (2,)]
def test_simple_pipe(self): def pipe(X_in, X_out): foo = X_in.recv() X_out.send(foo) @maintask def main(): X, Y = makechan(), makechan() go(pipe, X, Y) X.send(42) assert Y.recv() == 42 run()
def test_After(): rlist = [] @maintask def main(): PERIOD = 0.1 * SECOND now = nanotime() c = After(PERIOD) rlist.append(c.recv()) diff = nanotime() - rlist[0] assert PERIOD - DELTA0 <= diff <= PERIOD + DELTA run()
def test_Ticker(): rlist = [] @maintask def main(): ticker = Ticker(0.1 * SECOND) for i in range(3): rlist.append(ticker.c.recv()) ticker.stop() run() assert len(rlist) == 3
def test_run(): output = [] def print_(*args): output.append(args) def f(i): print_(i) go(f, 1) go(f, 2) run() assert output == [(1, ), (2, )]
def test_Timer_reset(): rlist = [] @maintask def main(): PERIOD = 10 * SECOND t = Timer(PERIOD) now = nanotime() t.reset(0.1 * SECOND) rlist.append(t.c.recv()) diff = nanotime() - rlist[0] assert PERIOD - DELTA0 <= diff <= PERIOD + DELTA run()
def test_Cond_broadcast(): m = Mutex() c = Cond(m) n = 200 running = makechan(n) awake = makechan(n) exit = False def test(i): m.lock() while not exit: running.send(i) c.wait() awake.send(i) m.unlock() @maintask def main(): for i in range(n): go(test, i) for i in range(n): for i in range(n): running.recv() if i == n -1: m.lock() exit = True m.unlock() ret = select(awake.if_recv(), default) assert ret != awake.if_recv(), "coroutine not asleep" m.lock() c.broadcast() m.unlock() seen = {} for i in range(n): g = awake.recv() assert g not in seen, "coroutine woke up twice" seen[g] = True ret = select(running.if_recv(), default) assert ret != running.if_recv(), "coroutine did not exist" c.broadcast() run()
def test_run_class(): output = [] def print_(*args): output.append(args) class Test(object): def __call__(self, i): print_(i) t = Test() go(t, 1) go(t, 2) run() assert output == [(1,), (2,)]
def test_send_exception(self): def exp_sender(chan): chan.send_exception(Exception, 'test') def exp_recv(chan): try: val = chan.recv() except Exception as exp: assert exp.__class__ is Exception assert str(exp) == 'test' @maintask def main(): chan = makechan() go(exp_recv, chan) go(exp_sender, chan) run()
def test_run_class(): output = [] def print_(*args): output.append(args) class Test(object): def __call__(self, i): print_(i) t = Test() go(t, 1) go(t, 2) run() assert output == [(1, ), (2, )]
def test_construction(): output = [] def print_(*args): output.append(args) def aCallable(value): print_("aCallable:", value) go(aCallable, 'Inline using setup') run() assert output == [("aCallable:", 'Inline using setup')] del output[:] go(aCallable, 'Inline using ()') run() assert output == [("aCallable:", 'Inline using ()')]
def test_cooperative(): output = [] def print_(*args): output.append(args) def Loop(i): for x in range(3): gosched() print_("schedule", i) @maintask def main(): go(Loop, 1) go(Loop, 2) run() assert output == [('schedule', 1), ('schedule', 2), ('schedule', 1), ('schedule', 2), ('schedule', 1), ('schedule', 2),]
def test_async_channel(self): @maintask def main(): c = makechan(100) unblocked_sent = 0 for i in range(100): c.send(True) unblocked_sent += 1 assert unblocked_sent == 100 unblocked_recv = [] for i in range(100): unblocked_recv.append(c.recv()) assert len(unblocked_recv) == 100 run()
def test_simple_timer(): def _func(now, t, rlist, g): rlist.append(now) g.ready() @maintask def main(): rlist = [] period = 0.1 * SECOND t = Timer(_func, period, args=(rlist, proc.current())) now = nanotime() t.start() park() delay = rlist[0] assert (now + period - DELTA0) <= delay <= (now + period + DELTA), delay run()
def test_wait_two(self): """ A tasklets/channels adaptation of the test_wait_two from the logic object space """ def sleep(X, Y): dprint('twt_S ==== 1') value = X.recv() dprint('twt_S ==== 2') Y.send((X, value)) dprint('twt_S ==== 3') def wait_two(X, Y, Ret_chan): Barrier = makechan() go(sleep, X, Barrier) go(sleep, Y, Barrier) dprint('twt_W ==== 1') ret = Barrier.recv() dprint('twt_W ==== 2') if ret[0] == X: Ret_chan.send((1, ret[1])) else: Ret_chan.send((2, ret[1])) dprint('twt_W ==== 3') @maintask def main(): X, Y = makechan(), makechan() Ret_chan = makechan() go(wait_two, X, Y, Ret_chan) dprint('twt ==== 1') Y.send(42) dprint('twt ==== 2') X.send(42) dprint('twt ==== 3') value = Ret_chan.recv() dprint('twt ==== 4') assert value == (2, 42) run()
def test_Mutex(): def hammer_mutex(m, loops, cdone): for i in range(loops): m.lock() m.unlock() cdone.send(True) @maintask def main(): m = Mutex() c = makechan() for i in range(10): go(hammer_mutex, m, 1000, c) for i in range(10): c.recv() run()
def test_repeat(): r = [] def f(now, t, g): if len(r) == 3: t.stop() g.ready() else: r.append(now) @maintask def main(): t = Timer(f, 0.01 * SECOND, 0.01 * SECOND, args=(proc.current(), )) t.start() park() assert len(r) == 3 assert r[2] > r[1] assert r[1] > r[0] run()
def test_simple(): rlist = [] def f(): rlist.append('f') def g(): rlist.append('g') gosched() @maintask def main(): rlist.append('m') cg = go(g) cf = go(f) gosched() rlist.append('m') run() assert rlist == 'm g f m'.split()
def test_repeat(): r = [] def f(now, t, g): if len(r) == 3: t.stop() g.ready() else: r.append(now) @maintask def main(): t = Timer(f, 0.01 * SECOND, 0.01 * SECOND, args=(proc.current(),)) t.start() park() assert len(r) == 3 assert r[2] > r[1] assert r[1] > r[0] run()
def test_multiple_sleep(): T1 = 0.4 * SECOND T2 = 0.1 * SECOND r1 = [] def f1(): sleep(T1) r1.append(nanotime()) r2 = [] def f2(): sleep(T2) r2.append(nanotime()) go(f1) go(f2) now = nanotime() run() assert r1[0] > r2[0] assert (now + T1 - DELTA0) <= r1[0] <= (now + T1 + DELTA), r1[0] assert (now + T2 - DELTA0) <= r2[0] <= (now + T2 + DELTA), r2[0]
def test_AfterFunc(): rlist = [] @maintask def main(): i = 10 c = makechan() def f(): i -= 1 if i >= 0: AfterFunc(0, f) sleep(1 * SECOND) else: c.send(True) AfterFunc(0, f) c.recv() assert i == 0 run()