test_syslog.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from test.support import import_helper, threading_helper
  2. syslog = import_helper.import_module("syslog") #skip if not supported
  3. from test import support
  4. import sys
  5. import threading
  6. import time
  7. import unittest
  8. # XXX(nnorwitz): This test sucks. I don't know of a platform independent way
  9. # to verify that the messages were really logged.
  10. # The only purpose of this test is to verify the code doesn't crash or leak.
  11. class Test(unittest.TestCase):
  12. def tearDown(self):
  13. syslog.closelog()
  14. def test_openlog(self):
  15. syslog.openlog('python')
  16. # Issue #6697.
  17. self.assertRaises(UnicodeEncodeError, syslog.openlog, '\uD800')
  18. def test_syslog(self):
  19. syslog.openlog('python')
  20. syslog.syslog('test message from python test_syslog')
  21. syslog.syslog(syslog.LOG_ERR, 'test error from python test_syslog')
  22. def test_syslog_implicit_open(self):
  23. syslog.closelog() # Make sure log is closed
  24. syslog.syslog('test message from python test_syslog')
  25. syslog.syslog(syslog.LOG_ERR, 'test error from python test_syslog')
  26. def test_closelog(self):
  27. syslog.openlog('python')
  28. syslog.closelog()
  29. syslog.closelog() # idempotent operation
  30. def test_setlogmask(self):
  31. mask = syslog.LOG_UPTO(syslog.LOG_WARNING)
  32. oldmask = syslog.setlogmask(mask)
  33. self.assertEqual(syslog.setlogmask(0), mask)
  34. self.assertEqual(syslog.setlogmask(oldmask), mask)
  35. def test_log_mask(self):
  36. mask = syslog.LOG_UPTO(syslog.LOG_WARNING)
  37. self.assertTrue(mask & syslog.LOG_MASK(syslog.LOG_WARNING))
  38. self.assertTrue(mask & syslog.LOG_MASK(syslog.LOG_ERR))
  39. self.assertFalse(mask & syslog.LOG_MASK(syslog.LOG_INFO))
  40. def test_openlog_noargs(self):
  41. syslog.openlog()
  42. syslog.syslog('test message from python test_syslog')
  43. @threading_helper.requires_working_threading()
  44. def test_syslog_threaded(self):
  45. start = threading.Event()
  46. stop = False
  47. def opener():
  48. start.wait(10)
  49. i = 1
  50. while not stop:
  51. syslog.openlog(f'python-test-{i}') # new string object
  52. i += 1
  53. def logger():
  54. start.wait(10)
  55. while not stop:
  56. syslog.syslog('test message from python test_syslog')
  57. orig_si = sys.getswitchinterval()
  58. support.setswitchinterval(1e-9)
  59. try:
  60. threads = [threading.Thread(target=opener)]
  61. threads += [threading.Thread(target=logger) for k in range(10)]
  62. with threading_helper.start_threads(threads):
  63. start.set()
  64. time.sleep(0.1)
  65. stop = True
  66. finally:
  67. sys.setswitchinterval(orig_si)
  68. if __name__ == "__main__":
  69. unittest.main()