瀏覽代碼

Give dispatchers a human-readable repr.

Chris McDonough 18 年之前
父節點
當前提交
0405dd6914
共有 2 個文件被更改,包括 40 次插入1 次删除
  1. 6 0
      src/supervisor/dispatchers.py
  2. 34 1
      src/supervisor/tests/test_dispatchers.py

+ 6 - 0
src/supervisor/dispatchers.py

@@ -15,6 +15,12 @@ class PDispatcher:
     """ Asyncore dispatcher for mainloop, representing a process channel
     (stdin, stdout, or stderr).  This class is abstract. """
 
+    def __repr__(self):
+        return '<%s at %s for %s (%s)>' % (self.__class__.__name__,
+                                           id(self),
+                                           self.process,
+                                           self.channel)
+
     def readable(self):
         raise NotImplementedError
 

+ 34 - 1
src/supervisor/tests/test_dispatchers.py

@@ -312,7 +312,18 @@ class POutputDispatcherTests(unittest.TestCase):
         self.assertEqual(dispatcher.mainlog, None)
         self.assertEqual(dispatcher.childlog, None)
 
-
+    def test_repr(self):
+        options = DummyOptions()
+        config = DummyPConfig(options, 'process1', '/bin/process1')
+        process = DummyProcess(config)
+        dispatcher = self._makeOne(process)
+        drepr = repr(dispatcher)
+        self.assertTrue(drepr.startswith('<POutputDispatcher at'), drepr)
+        self.assertNotEqual(
+            drepr.find('<supervisor.tests.base.DummyProcess instance at'),
+            -1)
+        self.assertTrue(drepr.endswith('(stdout)>'), drepr)
+                        
 class PInputDispatcherTests(unittest.TestCase):
     def _getTargetClass(self):
         from supervisor.dispatchers import PInputDispatcher
@@ -402,6 +413,17 @@ class PInputDispatcherTests(unittest.TestCase):
         dispatcher = self._makeOne(process)
         self.assertRaises(NotImplementedError, dispatcher.handle_error)
 
+    def test_repr(self):
+        options = DummyOptions()
+        config = DummyPConfig(options, 'process1', '/bin/process1')
+        process = DummyProcess(config)
+        dispatcher = self._makeOne(process)
+        drepr = repr(dispatcher)
+        self.assertTrue(drepr.startswith('<PInputDispatcher at'), drepr)
+        self.assertNotEqual(
+            drepr.find('<supervisor.tests.base.DummyProcess instance at'),
+            -1)
+        self.assertTrue(drepr.endswith('(stdin)>'), drepr)
 
 
 class PEventListenerDispatcherTests(unittest.TestCase):
@@ -713,6 +735,17 @@ class PEventListenerDispatcherTests(unittest.TestCase):
         self.assertEqual(dispatcher.fd, 0)
         self.assertEqual(dispatcher.childlog.__class__, DummyLogger)
 
+    def test_repr(self):
+        options = DummyOptions()
+        config = DummyPConfig(options, 'process1', '/bin/process1')
+        process = DummyProcess(config)
+        dispatcher = self._makeOne(process)
+        drepr = repr(dispatcher)
+        self.assertTrue(drepr.startswith('<PEventListenerDispatcher at'), drepr)
+        self.assertNotEqual(
+            drepr.find('<supervisor.tests.base.DummyProcess instance at'),
+            -1)
+        self.assertTrue(drepr.endswith('(stdout)>'), drepr)