Home | History | Annotate | Download | only in trace_event_impl
      1 # Copyright 2016 The Chromium Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 import tempfile
      5 import unittest
      6 
      7 #from .log import *
      8 #from .parsed_trace_events import *
      9 
     10 from log import *
     11 from parsed_trace_events import *
     12 
     13 class TraceTest(unittest.TestCase):
     14   def __init__(self, *args):
     15     """
     16     Infrastructure for running tests of the tracing system.
     17 
     18     Does not actually run any tests. Look at subclasses for those.
     19     """
     20     unittest.TestCase.__init__(self, *args)
     21     self._file = None
     22 
     23   def go(self, cb):
     24     """
     25     Enables tracing, runs the provided callback, and if successful, returns a
     26     TraceEvents object with the results.
     27     """
     28     self._file = tempfile.NamedTemporaryFile()
     29     trace_enable(open(self._file.name, 'a+'))
     30 
     31     try:
     32       cb()
     33     finally:
     34       trace_disable()
     35     e = ParsedTraceEvents(trace_filename = self._file.name)
     36     self._file.close()
     37     self._file = None
     38     return e
     39 
     40   @property
     41   def trace_filename(self):
     42     return self._file.name
     43 
     44   def tearDown(self):
     45     if trace_is_enabled():
     46       trace_disable()
     47     if self._file:
     48       self._file.close()
     49