Пример #1
0
    def testFullLogQueueFilter(self):
        """ACSHandler drops low priority messages when message queue is full"""
        def mockShouldFlush(self, record):
            return 0

        holdShouldFlush = ACSHandler.ACSHandler.shouldFlush
        ACSHandler.ACSHandler.shouldFlush = mockShouldFlush
        h = ACSHandler.ACSHandler(capacity=ACSHandler.DEFAULT_RECORD_CAPACITY)
        lra = ACSHandler.ACSLogRecord("Nested.Name", logging.NOTSET + 1,
                                      "/path/to/file.py", 100, "Test text",
                                      None, None)
        lrb = ACSHandler.ACSLogRecord("Name", logging.DEBUG,
                                      "/path/to/file.py", 100, "Test text",
                                      None, None)
        lrc = ACSHandler.ACSLogRecord("Name", logging.INFO, "/path/to/file.py",
                                      100, "Test text", None, None)
        lrd = ACSHandler.ACSLogRecord("Name", logging.ERROR,
                                      "/path/to/file.py", 100, "Test text",
                                      None, None)
        h.buffer += [lrd, lra, lrc, lrb, lrd, lrb, lra, lrc, lra, lrd]
        self.assertEqual([lrd, lra, lrc, lrb, lrd, lrb, lra, lrc, lra, lrd],
                         h.buffer)
        h.handle(lrd)
        self.assertEqual([lrd, lrc, lrd, lrc, lrd, lrd], h.buffer)
        ACSHandler.ACSHandler.shouldFlush = holdShouldFlush
Пример #2
0
    def testSendLog(self):
        """ACSHandler sends Log messages at the appropriate levels"""
        import Acspy.Common.Log
        h = ACSHandler.ACSHandler()

        # Make logging calls for all known logging levels except OFF and NOTSET
        expected = []
        keys = Acspy.Common.Log.LEVELS.keys()
        keys.sort()
        for l in keys:
            if l == 0 or l == 99:
                continue
            else:
                if l in [7, 8, ACSLog.ACS_LOG_ERROR]:
                    expected.append("logWithAudience")
                else:
                    expected.append(
                        "log" + Acspy.Common.Log.getLevelName(l).capitalize())
            lr = ACSHandler.ACSLogRecord("Sample", Acspy.Common.Log.LEVELS[l],
                                         "/path/to/file.py", 100, expected[-1],
                                         None, None)
            h.sendLog(lr)

        # Append an unknown level
        expected.append("logCritical")
        lr = ACSHandler.ACSLogRecord("Sample", 75, "/path/to/file.py", 100,
                                     "Unknown", None, None)
        h.sendLog(lr)

        self.assertEquals(
            expected, [n[0] for n in mockLogSvc.method_calls[-len(keys) + 1:]])
Пример #3
0
 def testFlushLevel(self):
     """ACSHandler flushes buffer when message priority greater than threshold arrives"""
     h = ACSHandler.ACSHandler()
     lr = ACSHandler.ACSLogRecord("Nested.Name", logging.INFO, "/path/to/file.py", 100, "Test text", [], None)
     self.assertEqual(False, h.shouldFlush(lr))
     lr = ACSHandler.ACSLogRecord("Nested.Name", logging.CRITICAL+1, "/path/to/file.py", 100, "Test text", [], None)
     self.assertEqual(True, h.shouldFlush(lr))
Пример #4
0
 def testFullLogQueue(self):
     """ACSHandler drops new messages when pending message queue is full"""
     def mockFlush(self): return 0
     holdFlush = ACSHandler.ACSHandler.flush
     ACSHandler.ACSHandler.flush = mockFlush
     h = ACSHandler.ACSHandler(capacity=ACSHandler.DEFAULT_RECORD_CAPACITY)
     lr = ACSHandler.ACSLogRecord("Nested.Name", logging.INFO, "/path/to/file.py", 100, "Test text", None, None)
     lrn = ACSHandler.ACSLogRecord("Name", logging.INFO, "/path/to/file.py", 100, "Test text", None, None)
     h.buffer += [ lr, lr, lr, lr, lr, lr, lr, lr, lr, lr ]
     self.assertEqual([ lr, lr, lr, lr, lr, lr, lr, lr, lr, lr ], h.buffer)
     h.handle(lrn)
     self.assertEqual([ lr, lr, lr, lr, lr, lr, lr, lr, lr, lr ], h.buffer)
     ACSHandler.ACSHandler.flush = holdFlush
Пример #5
0
 def testShouldFlushPriority(self):
     """ACSHandler flushes when high priority message is received"""
     h = ACSHandler.ACSHandler()
     hr = ACSHandler.ACSLogRecord("High.Name", logging.CRITICAL + 2,
                                  "/path/to/file.py", 100, "High Test text",
                                  [], None)
     self.assertEqual(True, h.shouldFlush(hr))
Пример #6
0
 def testNestedConstructor(self):
     """ACSLogRecord initialized with nested name"""
     lr = ACSHandler.ACSLogRecord("Nested.Name", "TRACE",
                                  "/path/to/file.py", 100, "Test text", [],
                                  None)
     self.assertEqual("Name", lr.name)
     self.assertEqual("Nested", lr.source)
Пример #7
0
 def testFormatNoData(self):
     """ACSFormatter formats a log record that has no data attribute correctly"""
     lr = ACSHandler.ACSLogRecord("Simple", "TRACE", "/path/to/file.py", 100, "Test text", [], None)
     lr.created = 0
     lr.msecs = 0
     s = self.f.format(lr)
     self.assertEqual("1970-01-01T00:00:00.000 Simple Test text",s)
Пример #8
0
 def testFlushToFile(self):
     """ACSHandler writes log messages to a file"""
     h = ACSHandler.ACSHandler()
     h.file_handler = mock.Mock(spec=logging.Handler)
     lr = ACSHandler.ACSLogRecord("Nested.Name", "TRACE", "/path/to/file.py", 100, "Test text", [], None)
     h.flushToFile(lr)
     self.assertEqual('handle',h.file_handler.method_calls[0][0])
Пример #9
0
 def testShouldFlushCapacity(self):
     """ACSHandler flushes when capacity is reached"""
     h = ACSHandler.ACSHandler()
     lr = ACSHandler.ACSLogRecord("Nested.Name", logging.NOTSET+1, "/path/to/file.py", 100, "Test text", [], None)
     self.assertEqual(False, h.shouldFlush(lr))
     h.buffer += [ lr, lr, lr, lr, lr, lr, lr, lr, lr, lr ]
     self.assertEqual(True, h.shouldFlush(lr))
Пример #10
0
 def testFormatEmptyDataList(self):
     """ACSFormatter formats a log record with an empty data list attribute correctly"""
     lr = ACSHandler.ACSLogRecord("Simple", "TRACE", "/path/to/file.py", 100, "Test text", [], None)
     lr.created = 0
     lr.msecs = 0
     lr.data = []
     s = self.f.format(lr)
     self.assertEqual("1970-01-01T00:00:00.000 Simple Test text [ ]",s)
Пример #11
0
 def testFlush(self):
     """ACSHandler flushes buffer correctly"""
     h = ACSHandler.ACSHandler()
     lr = ACSHandler.ACSLogRecord("Nested.Name", logging.INFO, "/path/to/file.py", 100, "Test text", None, None)
     h.buffer = [ lr, lr ]
     expected = [ 'logInfo' , 'logInfo' ]
     h.flush()
     self.assertEquals(expected, [ n[0] for n in mockLogSvc.method_calls[-2:]])
Пример #12
0
 def testFormatDataList(self):
     """ACSFormatter formats a log record with a data list attribute correctly"""
     lr = ACSHandler.ACSLogRecord("Simple", "TRACE", "/path/to/file.py", 100, "Test text", [], None)
     lr.created = 0
     lr.msecs = 0
     lr.data = [ ACSLog.NVPair('a', 'A'), ACSLog.NVPair('5', '5'), ACSLog.NVPair('B', '9') ]
     s = self.f.format(lr)
     self.assertEqual("1970-01-01T00:00:00.000 Simple Test text [ a=A 5=5 B=9 ]",s)
Пример #13
0
 def testFormatDataDict(self):
     """ACSFormatter formats a log record with a data dictionary attribute correctly"""
     lr = ACSHandler.ACSLogRecord("Simple", "TRACE", "/path/to/file.py", 100, "Test text", [], None)
     lr.created = 0
     lr.msecs = 0
     lr.data = { 'a' : 'A', 5 : '5', 'B' : 9 }
     s = self.f.format(lr)
     self.assertEqual("1970-01-01T00:00:00.000 Simple Test text [ a=A B=9 5=5 ]",s)
Пример #14
0
 def testFlushToFileNoHandler(self):
     """ACSHandler creates singleton file handler when necessary"""
     def mockInitFileHandler(self):
         self.file_handler = mock.Mock(spec=logging.FileHandler)
     holdmethod = ACSHandler.ACSHandler.initFileHandler
     ACSHandler.ACSHandler.initFileHandler = mockInitFileHandler 
     h = ACSHandler.ACSHandler()
     lr = ACSHandler.ACSLogRecord("Nested.Name", "TRACE", "/path/to/file.py", 100, "Test text", [], None)
     self.assertEqual(True, h.file_handler is None)
     h.flushToFile(lr)
     self.assertEqual(False, h.file_handler is None)
     ACSHandler.ACSHandler.initFileHandler = holdmethod
Пример #15
0
 def testSimpleConstructor(self):
     """ACSLogRecord initialized with simple name"""
     lr = ACSHandler.ACSLogRecord("Simple", "TRACE", "/path/to/file.py", 100, "Test text", [], None)
     self.assertEqual("Simple", lr.name)
     self.assertEqual(lr.name, lr.source)
     self.assertEqual("/path/to/file.py", lr.pathname)
     self.assertEqual("file.py", lr.filename)
     self.assertEqual("file", lr.module)
     self.assertEqual("Test text", lr.msg)
     self.assertEqual([], lr.args)
     self.assertEqual(None, lr.exc_info)
     self.assertEqual(None, lr.funcName)
Пример #16
0
 def testFlushException(self):
     """ACSHandler handles exceptions correctly when flushing buffer"""
     def mockSendLog(self, record): raise Exception()
     holdSendLog = ACSHandler.ACSHandler.sendLog
     ACSHandler.ACSHandler.sendLog = mockSendLog
     h = ACSHandler.ACSHandler()
     h.file_handler = mock.Mock(spec=logging.Handler)
     lr = ACSHandler.ACSLogRecord("Nested.Name", logging.INFO, "/path/to/file.py", 100, "Test text", None, None)
     h.buffer = [ lr, lr ]
     expected = [ 'handle' , 'handle' ]
     h.flush()
     self.assertEquals(expected, [ n[0] for n in h.file_handler.method_calls[-2:]])
     ACSHandler.ACSHandler.sendLog = holdSendLog