summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/catapult/common/py_trace_event/py_trace_event/trace_event_impl/trace_test.py
blob: 37f7501d99d3b8994b2fe6ba40a2cd92baad2eae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from __future__ import absolute_import
import unittest

#from .log import *
#from .parsed_trace_events import *

from .log import *
from .parsed_trace_events import *
from py_utils import tempfile_ext

class TraceTest(unittest.TestCase):
  def __init__(self, *args):
    """
    Infrastructure for running tests of the tracing system.

    Does not actually run any tests. Look at subclasses for those.
    """
    unittest.TestCase.__init__(self, *args)
    self._file = None

  def go(self, cb):
    """
    Enables tracing, runs the provided callback, and if successful, returns a
    TraceEvents object with the results.
    """
    with tempfile_ext.TemporaryFileName() as filename:
      self._file = open(filename, 'a+')
      trace_enable(self._file)
      try:
        cb()
      finally:
        trace_disable()
      e = ParsedTraceEvents(trace_filename=self._file.name)
      self._file.close()
      self._file = None
    return e

  @property
  def trace_filename(self):
    return self._file.name

  def tearDown(self):
    if trace_is_enabled():
      trace_disable()
    if self._file:
      self._file.close()