test_win32event.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import unittest
  2. import win32event
  3. import pywintypes
  4. import time
  5. import os
  6. import sys
  7. from pywin32_testutil import int2long
  8. class TestWaitableTimer(unittest.TestCase):
  9. def testWaitableFireLong(self):
  10. h = win32event.CreateWaitableTimer(None, 0, None)
  11. dt = int2long(-160) # 160 ns.
  12. win32event.SetWaitableTimer(h, dt, 0, None, None, 0)
  13. rc = win32event.WaitForSingleObject(h, 1000)
  14. self.failUnlessEqual(rc, win32event.WAIT_OBJECT_0)
  15. def testWaitableFire(self):
  16. h = win32event.CreateWaitableTimer(None, 0, None)
  17. dt = -160 # 160 ns.
  18. win32event.SetWaitableTimer(h, dt, 0, None, None, 0)
  19. rc = win32event.WaitForSingleObject(h, 1000)
  20. self.failUnlessEqual(rc, win32event.WAIT_OBJECT_0)
  21. def testWaitableTrigger(self):
  22. h = win32event.CreateWaitableTimer(None, 0, None)
  23. # for the sake of this, pass a long that doesn't fit in an int.
  24. dt = -2000000000
  25. win32event.SetWaitableTimer(h, dt, 0, None, None, 0)
  26. rc = win32event.WaitForSingleObject(h, 10) # 10 ms.
  27. self.failUnlessEqual(rc, win32event.WAIT_TIMEOUT)
  28. def testWaitableError(self):
  29. h = win32event.CreateWaitableTimer(None, 0, None)
  30. h.close()
  31. self.assertRaises(pywintypes.error, win32event.SetWaitableTimer,
  32. h, -42, 0, None, None, 0)
  33. class TestWaitFunctions(unittest.TestCase):
  34. def testMsgWaitForMultipleObjects(self):
  35. # this function used to segfault when called with an empty list
  36. res = win32event.MsgWaitForMultipleObjects([], 0, 0, 0)
  37. self.assertEquals(res, win32event.WAIT_TIMEOUT)
  38. def testMsgWaitForMultipleObjects2(self):
  39. # test with non-empty list
  40. event = win32event.CreateEvent(None, 0, 0, None)
  41. res = win32event.MsgWaitForMultipleObjects([event], 0, 0, 0)
  42. self.assertEquals(res, win32event.WAIT_TIMEOUT)
  43. def testMsgWaitForMultipleObjectsEx(self):
  44. # this function used to segfault when called with an empty list
  45. res = win32event.MsgWaitForMultipleObjectsEx([], 0, 0, 0)
  46. self.assertEquals(res, win32event.WAIT_TIMEOUT)
  47. def testMsgWaitForMultipleObjectsEx2(self):
  48. # test with non-empty list
  49. event = win32event.CreateEvent(None, 0, 0, None)
  50. res = win32event.MsgWaitForMultipleObjectsEx([event], 0, 0, 0)
  51. self.assertEquals(res, win32event.WAIT_TIMEOUT)
  52. class TestEvent(unittest.TestCase):
  53. def assertSignaled(self, event):
  54. self.assertEquals(win32event.WaitForSingleObject(event, 0),
  55. win32event.WAIT_OBJECT_0)
  56. def assertNotSignaled(self, event):
  57. self.assertEquals(win32event.WaitForSingleObject(event, 0),
  58. win32event.WAIT_TIMEOUT)
  59. def testCreateEvent(self):
  60. event = win32event.CreateEvent(None, False, False, None)
  61. self.assertNotSignaled(event)
  62. event = win32event.CreateEvent(None, False, True, None)
  63. self.assertSignaled(event)
  64. self.assertNotSignaled(event)
  65. event = win32event.CreateEvent(None, True, True, None)
  66. self.assertSignaled(event)
  67. self.assertSignaled(event)
  68. def testSetEvent(self):
  69. event = win32event.CreateEvent(None, True, False, None)
  70. self.assertNotSignaled(event)
  71. res = win32event.SetEvent(event)
  72. self.assertEquals(res, None)
  73. self.assertSignaled(event)
  74. event.close()
  75. self.assertRaises(pywintypes.error, win32event.SetEvent, event)
  76. def testResetEvent(self):
  77. event = win32event.CreateEvent(None, True, True, None)
  78. self.assertSignaled(event)
  79. res = win32event.ResetEvent(event)
  80. self.assertEquals(res, None)
  81. self.assertNotSignaled(event)
  82. event.close()
  83. self.assertRaises(pywintypes.error, win32event.ResetEvent, event)
  84. class TestMutex(unittest.TestCase):
  85. def testReleaseMutex(self):
  86. mutex = win32event.CreateMutex(None, True, None)
  87. res = win32event.ReleaseMutex(mutex)
  88. self.assertEqual(res, None)
  89. res = win32event.WaitForSingleObject(mutex, 0)
  90. self.assertEqual(res, win32event.WAIT_OBJECT_0)
  91. mutex.close()
  92. self.assertRaises(pywintypes.error, win32event.ReleaseMutex, mutex)
  93. if __name__=='__main__':
  94. unittest.main()