-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_bag.py
54 lines (46 loc) · 1.17 KB
/
test_bag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import inspect
import asyncio
from aioutils import Bag, OrderedBag, Group
def test_bag():
chars = 'abcdefg'
def g():
b = Bag()
@asyncio.coroutine
def f(c):
yield from asyncio.sleep(random.random()/10)
b.put(c)
def schedule():
for c in chars:
b.spawn(f(c))
b.join()
b.schedule(schedule)
yield from b.yielder()
chars2 = g()
assert inspect.isgenerator(chars2)
chars2 = list(chars2)
assert set(chars) == set(chars2)
def test_orderedbag():
chars = 'abcdefg'
def g():
b = OrderedBag(Group())
@asyncio.coroutine
def f(c):
yield from asyncio.sleep(random.random()*0.1)
b.put(c)
def schedule():
for c in chars:
b.spawn(f(c))
b.join()
b.schedule(schedule)
yield from b.yielder()
chars2 = g()
assert inspect.isgenerator(chars2)
chars2 = list(chars2)
for c1, c2 in zip(chars, chars2):
assert c1 == c2
if __name__ == '__main__':
test_bag()
test_orderedbag()