Skip to content
This repository
Browse code

Fleshing out a test suite.

  • Loading branch information...
commit f2392a78a5cb2f01f91763480aa48f04f172a7ad 1 parent 1b10d78
Jamie Turner authored
11 Makefile
... ... @@ -0,0 +1,11 @@
  1 +PYSETUP=python setup.py
  2 +
  3 +default: install test
  4 +
  5 +install:
  6 + $(PYSETUP) install
  7 +
  8 +test: test-basic
  9 +
  10 +test-basic:
  11 + $(MAKE) -C tests test-basic
0  diesel/contrib/__init__.py
No changes.
8 tests/Makefile
... ... @@ -0,0 +1,8 @@
  1 +WV=./wvtestrun ./wvtest.py
  2 +default: test-basic
  3 +
  4 +test-basic:
  5 + $(WV) diesel_test_basic_*.py
  6 +
  7 +test-short:
  8 + $(WV) `ls diesel_test_basic_*.py | grep -v _fork`
47 tests/diesel_test_basic_buffer.py
... ... @@ -0,0 +1,47 @@
  1 +from diesel.buffer import Buffer
  2 +from wvtest import *
  3 +
  4 +@wvtest
  5 +def test_feed():
  6 + b = Buffer()
  7 + WVPASS(b.feed("rock") == None)
  8 + WVPASS(b.feed("rockandroll") == None)
  9 +
  10 +@wvtest
  11 +def test_read_bytes():
  12 + b = Buffer()
  13 + b.set_term(10)
  14 + WVPASS(b.feed("rock") == None)
  15 + WVPASS(b.feed("rockandroll") == "rockrockan")
  16 + # buffer left.. droll
  17 + WVPASS(b.check() == None)
  18 + b.set_term(10)
  19 + WVPASS(b.check() == None)
  20 + WVPASS(b.feed("r") == None)
  21 + WVPASS(b.feed("r") == None)
  22 + WVPASS(b.feed("r") == None)
  23 + WVPASS(b.feed("r") == None)
  24 + WVPASS(b.feed("r") == "drollrrrrr")
  25 + WVPASS(b.feed("x" * 10000) == None) # no term (re-)established
  26 +
  27 +@wvtest
  28 +def test_read_sentinel():
  29 + b = Buffer()
  30 + WVPASS(b.feed("rock and") == None)
  31 + b.set_term("\r\n")
  32 + WVPASS(b.feed(" roll\r") == None)
  33 + WVPASS(b.feed("\nrock ") == "rock and roll\r\n")
  34 + WVPASS(b.feed("and roll 2\r\n") == None)
  35 + b.set_term("\r\n")
  36 + WVPASS(b.check() == "rock and roll 2\r\n")
  37 +
  38 +@wvtest
  39 +def test_read_hybrid():
  40 + b = Buffer()
  41 + WVPASS(b.feed("rock and") == None)
  42 + b.set_term("\r\n")
  43 + WVPASS(b.feed(" roll\r") == None)
  44 + WVPASS(b.feed("\n012345678") == "rock and roll\r\n")
  45 + b.set_term(16)
  46 + WVPASS(b.check() == None)
  47 + WVPASS(b.feed("9abcdefgh") == "0123456789abcdef")
64 tests/diesel_test_basic_fire.py
... ... @@ -0,0 +1,64 @@
  1 +from diesel import fork, fire, wait, sleep, quickstart, quickstop, ParentDiedException
  2 +from wvtest import *
  3 +
  4 +@wvtest
  5 +def test_basic_fire():
  6 + v = [0]
  7 + def w():
  8 + while True:
  9 + wait("boom!")
  10 + v[0] += 1
  11 +
  12 + def f():
  13 + sleep(0.05)
  14 + fire("boom!")
  15 + sleep(0.05)
  16 + WVPASS(v[0] == 1)
  17 + sleep(0.05)
  18 + fire("boom!")
  19 + sleep(0.05)
  20 + WVPASS(v[0] == 2)
  21 + quickstop()
  22 +
  23 + quickstart(f, w)
  24 +
  25 +@wvtest
  26 +def test_fire_multiple():
  27 + v = [0]
  28 + def w():
  29 + while True:
  30 + wait("boom!")
  31 + v[0] += 1
  32 +
  33 + def f():
  34 + sleep(0.05)
  35 + fire("boom!")
  36 + sleep(0.05)
  37 + WVPASS(v[0] == 2)
  38 + sleep(0.05)
  39 + fire("boom!")
  40 + sleep(0.05)
  41 + WVPASS(v[0] == 4)
  42 + quickstop()
  43 +
  44 + quickstart(f, w, w)
  45 +
  46 +@wvtest
  47 +def test_fire_miss():
  48 + v = [0]
  49 + def w():
  50 + while True:
  51 + wait("boom!")
  52 + v[0] += 1
  53 +
  54 + def f():
  55 + sleep(0.05)
  56 + fire("fizz!")
  57 + sleep(0.05)
  58 + WVPASS(v[0] == 0) # should not have woken up!
  59 + quickstop()
  60 +
  61 + quickstart(f, w, w)
  62 +
  63 +if __name__ == '__main__':
  64 + test_basic_fire()
93 tests/diesel_test_basic_fork.py
... ... @@ -0,0 +1,93 @@
  1 +from diesel import fork, fork_child, sleep, quickstart, quickstop, ParentDiedException
  2 +from diesel import Application, Loop
  3 +from wvtest import *
  4 +
  5 +def tottering_child(v):
  6 + v[0] += 1
  7 + sleep(10)
  8 +
  9 +@wvtest
  10 +def test_basic_fork():
  11 + def parent():
  12 + v = [0]
  13 + fork(tottering_child, v)
  14 + sleep(0.1)
  15 + WVPASS(v[0] == 1)
  16 + quickstop()
  17 + quickstart(parent)
  18 +
  19 +@wvtest
  20 +def test_fork_many():
  21 + def parent():
  22 + COUNT = 10000
  23 + v = [0]
  24 + for x in xrange(COUNT):
  25 + fork(tottering_child, v)
  26 + sleep(8) # long enough on core 2-era
  27 + WVPASS(v[0] == COUNT)
  28 + quickstop()
  29 + quickstart(parent)
  30 +
  31 +
  32 +def dependent_child():
  33 + try:
  34 + sleep(50)
  35 + except ParentDiedException:
  36 + got_exception = 1
  37 + else:
  38 + got_exception = 0
  39 + WVPASS(got_exception)
  40 + quickstop()
  41 +
  42 +@wvtest
  43 +def test_fork_child_normal_death():
  44 +
  45 + def parent():
  46 + fork_child(dependent_child)
  47 + sleep(0.1)
  48 + # implied, I end..
  49 +
  50 + quickstart(parent)
  51 +
  52 +@wvtest
  53 +def test_fork_child_exception():
  54 +
  55 + def parent():
  56 + fork_child(dependent_child)
  57 + sleep(0.1)
  58 + a = b # undef
  59 +
  60 + quickstart(parent)
  61 +
  62 +@wvtest
  63 +def test_loop_keep_alive_normal_death():
  64 + v = [0]
  65 + def l():
  66 + v[0] += 1
  67 +
  68 + def p():
  69 + sleep(0.9)
  70 + WVPASS(v[0] > 1)
  71 + a.halt()
  72 +
  73 + a = Application()
  74 + a.add_loop(Loop(l), keep_alive=True)
  75 + a.add_loop(Loop(p))
  76 + a.run()
  77 +
  78 +@wvtest
  79 +def test_loop_keep_alive_exception():
  80 + v = [0]
  81 + def l():
  82 + v[0] += 1
  83 + a = b # exception!
  84 +
  85 + def p():
  86 + sleep(0.9)
  87 + WVPASS(v[0] > 1)
  88 + a.halt()
  89 +
  90 + a = Application()
  91 + a.add_loop(Loop(l), keep_alive=True)
  92 + a.add_loop(Loop(p))
  93 + a.run()
53 tests/diesel_test_basic_locking.py
... ... @@ -0,0 +1,53 @@
  1 +from diesel import fork, fire, wait, sleep, quickstart, quickstop, ParentDiedException
  2 +from diesel.util.queue import Queue
  3 +from wvtest import *
  4 +
  5 +@wvtest
  6 +def test_queue_put_noloss():
  7 + q = Queue()
  8 + c = 10000
  9 + done = [0]
  10 + def g():
  11 + for x in xrange(c):
  12 + v = q.get()
  13 + WVPASS(v == x)
  14 +
  15 + done[0] = 1
  16 +
  17 + def p():
  18 + for x in xrange(c):
  19 + q.put(x)
  20 + sleep(1)
  21 + WVPASS(done[0] == 1)
  22 +
  23 + quickstop()
  24 + quickstart(p, g)
  25 +
  26 +@wvtest
  27 +def test_queue_multi_consumer():
  28 + q = Queue()
  29 + c = 10000
  30 + s1 = [0]
  31 + s2 = [0]
  32 + def g(seen):
  33 + def run():
  34 + for x in xrange(c):
  35 + v = q.get()
  36 + seen[0] += 1
  37 + sleep()
  38 + return run
  39 +
  40 + def p():
  41 + for x in xrange(c):
  42 + q.put(x)
  43 + sleep(1)
  44 + print s1, s2
  45 + WVPASS(s1[0] < c)
  46 + WVPASS(s2[0] < c)
  47 + WVPASS(s1[0] + s2[0] == c)
  48 +
  49 + quickstop()
  50 + quickstart(p, g(s1), g(s2))
  51 +
  52 +
  53 +# XXX TODO -- test queue fairness (need dowski commit)
148 tests/diesel_test_basic_pipeline.py
... ... @@ -0,0 +1,148 @@
  1 +#12345678
  2 +from diesel.pipeline import Pipeline, PipelineClosed, PipelineCloseRequest
  3 +from cStringIO import StringIO
  4 +from wvtest import *
  5 +
  6 +FILE = __file__
  7 +if FILE.endswith('.pyc') or FILE.endswith('.pyo'):
  8 + FILE = FILE[:-1]
  9 +
  10 +@wvtest
  11 +def test_add_string():
  12 + p = Pipeline()
  13 + WVPASS(p.add("foo") == None)
  14 + WVPASS(not p.empty)
  15 +
  16 +@wvtest
  17 +def test_add_file():
  18 + p = Pipeline()
  19 + WVPASS(p.add(open(FILE)) == None)
  20 + WVPASS(not p.empty)
  21 +
  22 +@wvtest
  23 +def test_add_filelike():
  24 + p = Pipeline()
  25 + sio = StringIO()
  26 + WVPASS(p.add(sio) == None)
  27 + WVPASS(not p.empty)
  28 +
  29 +@wvtest
  30 +def test_add_badtypes():
  31 + p = Pipeline()
  32 + WVEXCEPT(ValueError, p.add, 3)
  33 + WVEXCEPT(ValueError, p.add, [])
  34 + class Whatever(object): pass
  35 + WVEXCEPT(ValueError, p.add, Whatever())
  36 + WVPASS(p.empty)
  37 +
  38 +
  39 +@wvtest
  40 +def test_read_empty():
  41 + p = Pipeline()
  42 + WVPASS(p.read(500) == '')
  43 +
  44 +@wvtest
  45 +def test_read_string():
  46 + p = Pipeline()
  47 + p.add("foo")
  48 + WVPASS(p.read(3) == "foo")
  49 + WVPASS(p.empty)
  50 +
  51 +@wvtest
  52 +def test_read_file():
  53 + p = Pipeline()
  54 + p.add(open(FILE))
  55 + WVPASS(p.read(5) == "#1234")
  56 +
  57 +@wvtest
  58 +def test_read_filelike():
  59 + p = Pipeline()
  60 + p.add(StringIO('abcdef'))
  61 + WVPASS(p.read(5) == 'abcde')
  62 +
  63 +@wvtest
  64 +def test_read_twice():
  65 + p = Pipeline()
  66 + p.add("foo")
  67 + WVPASS(p.read(2) == "fo")
  68 + WVPASS(p.read(2) == "o")
  69 +
  70 +@wvtest
  71 +def test_read_twice_empty():
  72 + p = Pipeline()
  73 + p.add("foo")
  74 + WVPASS(p.read(2) == "fo")
  75 + WVPASS(p.read(2) == "o")
  76 + WVPASS(p.read(2) == "")
  77 +
  78 +@wvtest
  79 +def test_read_backup():
  80 + p = Pipeline()
  81 + p.add("foo")
  82 + WVPASS(p.read(2) == "fo")
  83 + p.backup("fo")
  84 + WVPASS(p.read(2) == "fo")
  85 + WVPASS(p.read(2) == "o")
  86 +
  87 +@wvtest
  88 +def test_read_backup_extra():
  89 + p = Pipeline()
  90 + p.add("foo")
  91 + WVPASS(p.read(2) == "fo")
  92 + p.backup("foobar")
  93 + WVPASS(p.read(500) == "foobaro")
  94 +
  95 +def test_read_hybrid_objects():
  96 + p = Pipeline()
  97 + p.add("foo,")
  98 + p.add(StringIO("bar,"))
  99 + p.add(open(FILE))
  100 +
  101 + WVPASS(p.read(10) == "foo,bar,#1")
  102 + WVPASS(p.read(4) == "2345")
  103 + p.backup("rock") # in the middle of the "file"
  104 + WVPASS(p.read(6) == "rock67")
  105 +
  106 +def test_close():
  107 + p = Pipeline()
  108 + p.add("foo")
  109 + p.add(StringIO("bar"))
  110 + p.close_request()
  111 + WVPASS(p.read(1000) == "foobar")
  112 + WVEXCEPT(PipelineCloseRequest, p.read, 1000)
  113 +
  114 +def test_long_1():
  115 + p = Pipeline()
  116 + p.add("foo")
  117 + WVPASS(p.read(2) == "fo")
  118 + p.add("bar")
  119 + WVPASS(p.read(3) == "oba")
  120 + p.backup("rocko")
  121 + p.add(StringIO("soma"))
  122 + WVPASS(p.read(1000) == "rockorsoma")
  123 + WVPASS(p.read(1000) == "")
  124 + WVPASS(p.empty)
  125 + p.add("X" * 10000)
  126 + p.close_request()
  127 + WVPASS(p.read(5000) == 'X' * 5000)
  128 + p.backup('XXX')
  129 + WVEXCEPT(PipelineClosed, p.add, "newstuff")
  130 + WVPASS(not p.empty)
  131 + WVPASS(p.read(100000) == 'X' * 5003)
  132 + WVPASS(not p.empty)
  133 + WVEXCEPT(PipelineCloseRequest, p.read, 1000)
  134 + WVPASS(not p.empty)
  135 + WVEXCEPT(PipelineCloseRequest, p.read, 1000)
  136 +
  137 +def test_pri_clean():
  138 + p = Pipeline()
  139 + p.add("two")
  140 + p.add("three")
  141 + p.add("one")
  142 + WVPASS(p.read(18) == "twothreeone")
  143 +
  144 + p.add("two", 2)
  145 + p.add("three", 3)
  146 + p.add("six", 2)
  147 + p.add("one", 1)
  148 + WVPASS(p.read(18) == "threetwosixone")
54 tests/diesel_test_basic_sleep.py
... ... @@ -0,0 +1,54 @@
  1 +from time import time
  2 +from diesel import fork, fork_child, sleep, quickstart, quickstop, ParentDiedException
  3 +from wvtest import *
  4 +
  5 +@wvtest
  6 +def test_basic_sleep():
  7 + def l():
  8 + t = time()
  9 + sleep(0.3)
  10 + delt = time() - t
  11 + print delt
  12 + WVPASS(delt > 0.275 and delt < 0.325)
  13 + quickstop()
  14 + quickstart(l)
  15 +
  16 +@wvtest
  17 +def test_sleep_independence():
  18 + v = [0]
  19 + def i():
  20 + v[0] += 1
  21 + sleep(0.1)
  22 + v[0] += 1
  23 +
  24 + def l():
  25 + t = time()
  26 + sleep(0.05)
  27 + WVPASS(v[0] == 2)
  28 + sleep(0.1)
  29 + WVPASS(v[0] == 4)
  30 + quickstop()
  31 +
  32 + quickstart(l, i, i)
  33 +
  34 +@wvtest
  35 +def test_sleep_zero():
  36 + '''Sleep w/out argument allows other loops to run
  37 + '''
  38 +
  39 + v = [0]
  40 + def i():
  41 + x = 0
  42 + while True:
  43 + v[0] += 1
  44 + sleep()
  45 +
  46 + def l():
  47 + sleep(0.05)
  48 + cur = v[0]
  49 + sleep()
  50 + now = v[0]
  51 + WVPASS(now == cur + 1)
  52 + quickstop()
  53 +
  54 + quickstart(l, i)
0  diesel/contrib/wvtest.py → tests/wvtest.py
File renamed without changes
0  tools/wvtestrun → tests/wvtestrun
File renamed without changes

0 comments on commit f2392a7

Please sign in to comment.
Something went wrong with that request. Please try again.