def testThreadLogContextExtendLabel(self): """Verify ThreadLogContext.ExtendLabel behavior.""" context = log_util.ThreadLogContext() self.assertEqual(context.label, '') with context.ExtendLabel('LABEL-A'): self.assertEqual(context.label, 'LABEL-A ') with context.ExtendLabel('LABEL-B'): self.assertEqual(context.label, 'LABEL-A LABEL-B ') self.assertEqual(context.label, 'LABEL-A ') self.assertEqual(context.label, '')
def testPkbLogFilter(self): """Verify that PkbLogFilter sets the pkb_label of LogRecords it processes. """ logger_name = 'log_util_test.LogUtilTestCase.testPkbLogFilter' context = log_util.ThreadLogContext() log_util.SetThreadLogContext(context) with context.ExtendLabel('LABEL-A'): log_record = logging.LogRecord( name=logger_name, level=logging.INFO, pathname=__file__, lineno=inspect.getframeinfo(inspect.currentframe()).lineno + 1, msg="Log message.", args=None, exc_info=None) log_util.PkbLogFilter().filter(log_record) self.assertEqual(log_record.pkb_label, 'LABEL-A ')
def testThreadLogContextCopyConstruct(self): """Verify ThreadLogContext init with a reference ThreadLogContext behavior. The label state of the first ThreadLogContext should be copied. """ original = log_util.ThreadLogContext() self.assertEqual(original.label, '') with original.ExtendLabel('LABEL-A'): self.assertEqual(original.label, 'LABEL-A ') copied = log_util.ThreadLogContext(original) self.assertEqual(original.label, 'LABEL-A ') self.assertEqual(copied.label, 'LABEL-A ') with original.ExtendLabel('LABEL-B'): self.assertEqual(original.label, 'LABEL-A LABEL-B ') self.assertEqual(copied.label, 'LABEL-A ') with copied.ExtendLabel('LABEL-C'): self.assertEqual(original.label, 'LABEL-A LABEL-B ') self.assertEqual(copied.label, 'LABEL-A LABEL-C ') self.assertEqual(original.label, 'LABEL-A LABEL-B ') self.assertEqual(copied.label, 'LABEL-A ') self.assertEqual(original.label, 'LABEL-A ') self.assertEqual(copied.label, 'LABEL-A ') self.assertEqual(original.label, '') self.assertEqual(copied.label, 'LABEL-A ')
def testRunThreadedContextCopy(self): """Verify that ThreadLogContext is copied to threads by vm_util.RunThreaded. """ original = log_util.ThreadLogContext() log_util.SetThreadLogContext(original) t1_list = ['T1'] t2_list = ['T2'] self.assertEqual(original.label, '') with original.ExtendLabel('T0'): self.assertEqual(original.label, 'T0 ') vm_util.RunThreaded( target=LogUtilTestCase.RunThreadedContextCopyHelper, thread_params=[t1_list, t2_list]) self.assertEqual(original.label, 'T0 ') self.assertEqual(t1_list, ['T1', 'T0 ', 'T0 T1 ', 'T0 ']) self.assertEqual(t2_list, ['T2', 'T0 ', 'T0 T2 ', 'T0 '])
def _ExecuteThreadCall(target_arg_tuple, call_id, queue, parent_log_context): """Function invoked in another thread by RunParallelThreads. Executes a specified function call and captures the traceback upon exception. Args: target_arg_tuple: (target, args, kwargs) tuple containing the function to call and the arguments to pass it. call_id: int. Index corresponding to the call in the thread_params argument of RunParallelThreads. queue: Queue. Receives a ThreadCallResult. parent_log_context: ThreadLogContext of the parent thread. """ target, args, kwargs = target_arg_tuple try: log_context = log_util.ThreadLogContext(parent_log_context) log_util.SetThreadLogContext(log_context) queue.put(ThreadCallResult(call_id, target(*args, **kwargs), None)) except: queue.put(ThreadCallResult(call_id, None, traceback.format_exc()))
def CopyToCurrentThread(self): """Sets the thread context of the current thread.""" log_util.SetThreadLogContext(log_util.ThreadLogContext(self.log_context)) context.SetThreadBenchmarkSpec(self.benchmark_spec)
def __init__(self, *args, **kwargs): super(ThreadWithExceptions, self).__init__(*args, **kwargs) self.exception = None self._log_context = log_util.ThreadLogContext( log_util.GetThreadLogContext())