You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

62 lines
1.7 KiB

  1. import nilmdb
  2. from nilmdb.utils.printf import *
  3. import nose
  4. from nose.tools import *
  5. from nose.tools import assert_raises
  6. import threading
  7. import time
  8. from testutil.helpers import *
  9. def func_with_callback(a, b, callback):
  10. callback(a)
  11. callback(b)
  12. callback(a+b)
  13. return "return value"
  14. class TestIteratorizer(object):
  15. def test(self):
  16. # First try it with a normal callback
  17. self.result = ""
  18. def cb(x):
  19. self.result += str(x)
  20. func_with_callback(1, 2, cb)
  21. eq_(self.result, "123")
  22. # Now make it an iterator
  23. result = ""
  24. f = lambda x: func_with_callback(1, 2, x)
  25. with nilmdb.utils.Iteratorizer(f) as it:
  26. for i in it:
  27. result += str(i)
  28. eq_(result, "123")
  29. eq_(it.retval, "return value")
  30. # Make sure things work when an exception occurs
  31. result = ""
  32. with nilmdb.utils.Iteratorizer(
  33. lambda x: func_with_callback(1, "a", x)) as it:
  34. with assert_raises(TypeError) as e:
  35. for i in it:
  36. result += str(i)
  37. eq_(result, "1a")
  38. # Now try to trigger the case where we stop iterating
  39. # mid-generator, and expect the iteratorizer to clean up after
  40. # itself. This doesn't have a particular result in the test,
  41. # but gains coverage.
  42. def foo():
  43. with nilmdb.utils.Iteratorizer(f) as it:
  44. it.next()
  45. foo()
  46. eq_(it.retval, None)
  47. # Do the same thing when the curl hack is applied
  48. def foo():
  49. with nilmdb.utils.Iteratorizer(f, curl_hack = True) as it:
  50. it.next()
  51. foo()
  52. eq_(it.retval, None)