2012-05-22 21:00:12 -04:00
|
|
|
import nilmdb
|
2012-12-31 15:52:28 -05:00
|
|
|
from nilmdb.utils.printf import *
|
2012-05-22 21:00:12 -04:00
|
|
|
|
|
|
|
import nose
|
|
|
|
from nose.tools import *
|
|
|
|
from nose.tools import assert_raises
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
2013-01-05 15:00:34 -05:00
|
|
|
from testutil.helpers import *
|
2012-05-22 21:00:12 -04:00
|
|
|
|
|
|
|
def func_with_callback(a, b, callback):
|
|
|
|
callback(a)
|
|
|
|
callback(b)
|
|
|
|
callback(a+b)
|
2013-01-24 19:04:25 -05:00
|
|
|
return "return value"
|
2012-05-22 21:00:12 -04:00
|
|
|
|
|
|
|
class TestIteratorizer(object):
|
|
|
|
def test(self):
|
|
|
|
|
|
|
|
# First try it with a normal callback
|
|
|
|
self.result = ""
|
|
|
|
def cb(x):
|
|
|
|
self.result += str(x)
|
|
|
|
func_with_callback(1, 2, cb)
|
|
|
|
eq_(self.result, "123")
|
|
|
|
|
|
|
|
# Now make it an iterator
|
|
|
|
result = ""
|
2013-01-24 19:04:25 -05:00
|
|
|
f = lambda x: func_with_callback(1, 2, x)
|
|
|
|
with nilmdb.utils.Iteratorizer(f) as it:
|
|
|
|
for i in it:
|
|
|
|
result += str(i)
|
2012-05-22 21:00:12 -04:00
|
|
|
eq_(result, "123")
|
2013-01-24 19:04:25 -05:00
|
|
|
eq_(it.retval, "return value")
|
2012-05-22 21:00:12 -04:00
|
|
|
|
|
|
|
# Make sure things work when an exception occurs
|
|
|
|
result = ""
|
2013-01-24 19:04:25 -05:00
|
|
|
with nilmdb.utils.Iteratorizer(
|
|
|
|
lambda x: func_with_callback(1, "a", x)) as it:
|
|
|
|
with assert_raises(TypeError) as e:
|
|
|
|
for i in it:
|
|
|
|
result += str(i)
|
2012-05-22 21:00:12 -04:00
|
|
|
eq_(result, "1a")
|
|
|
|
|
|
|
|
# Now try to trigger the case where we stop iterating
|
|
|
|
# mid-generator, and expect the iteratorizer to clean up after
|
|
|
|
# itself. This doesn't have a particular result in the test,
|
|
|
|
# but gains coverage.
|
|
|
|
def foo():
|
2013-01-24 19:04:25 -05:00
|
|
|
with nilmdb.utils.Iteratorizer(f) as it:
|
|
|
|
it.next()
|
2012-05-22 21:00:12 -04:00
|
|
|
foo()
|
2013-01-24 19:04:25 -05:00
|
|
|
eq_(it.retval, None)
|
2013-01-24 19:06:20 -05:00
|
|
|
|
|
|
|
# Do the same thing when the curl hack is applied
|
|
|
|
def foo():
|
|
|
|
with nilmdb.utils.Iteratorizer(f, curl_hack = True) as it:
|
|
|
|
it.next()
|
|
|
|
foo()
|
|
|
|
eq_(it.retval, None)
|