Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 import functools | |
| 7 import logging | |
| 8 import os | |
| 9 import sys | |
| 10 import unittest | |
| 11 | |
| 12 sys.path.insert(0, os.path.dirname(__file__)) | |
|
bulach
2014/02/20 17:35:58
nit: would this work, without the sys.path?
from
Primiano Tucci (use gerrit)
2014/02/20 18:59:14
Hmm I think not (I'll give a try) because somethin
| |
| 13 import elf_symbolizer | |
| 14 import mock_addr2line | |
| 15 | |
| 16 | |
| 17 _MOCK_A2L_PATH = os.path.join(os.path.dirname(mock_addr2line.__file__), | |
| 18 'mock_addr2line') | |
| 19 | |
| 20 | |
| 21 class ELFSymbolizerTest(unittest.TestCase): | |
| 22 def setUp(self): | |
| 23 self._callback = functools.partial( | |
| 24 ELFSymbolizerTest._SymbolizeCallback, self) | |
| 25 self._resolved_addresses = set() | |
| 26 # Mute warnings, we expect them due to the crash/hang tests. | |
| 27 logging.getLogger().setLevel(logging.ERROR) | |
| 28 | |
| 29 def test_parallelism_1(self): | |
|
bulach
2014/02/20 17:00:13
I suppose we always use camelCaseTest?
https://co
Primiano Tucci (use gerrit)
2014/02/20 18:59:14
Ah, right. I use that casing just by looking at py
| |
| 30 self._RunTest(max_concurrent_jobs=1, num_symbols=100) | |
| 31 | |
| 32 def test_parallelism_4(self): | |
| 33 self._RunTest(max_concurrent_jobs=4, num_symbols=100) | |
| 34 | |
| 35 def test_parallelism_8(self): | |
| 36 self._RunTest(max_concurrent_jobs=8, num_symbols=100) | |
| 37 | |
| 38 def test_crash(self): | |
| 39 os.environ['MOCK_A2L_CRASH_EVERY'] = '99' | |
| 40 self._RunTest(max_concurrent_jobs=1, num_symbols=100) | |
| 41 os.environ['MOCK_A2L_CRASH_EVERY'] = '0' | |
| 42 | |
| 43 def test_hang(self): | |
| 44 os.environ['MOCK_A2L_HANG_EVERY'] = '99' | |
| 45 self._RunTest(max_concurrent_jobs=1, num_symbols=100) | |
| 46 os.environ['MOCK_A2L_HANG_EVERY'] = '0' | |
| 47 | |
| 48 def test_incomplete_syminfo(self): | |
| 49 """Stimulate the symbol-not-resolved logic.""" | |
| 50 symbolizer = elf_symbolizer.ELFSymbolizer( | |
| 51 elf_file_path='/path/doesnt/matter/mock_lib1.so', | |
| 52 addr2line_path=_MOCK_A2L_PATH, | |
| 53 callback=self._callback, | |
| 54 max_concurrent_jobs=1) | |
| 55 | |
| 56 # Test symbols with valid name but incomplete path. | |
| 57 addr = 1024 * 1024 # mock_addr2line treats this addr range specially. | |
| 58 exp_sym_name = 'mock_sym_for_addr_%d' % addr | |
| 59 exp_source_path = '??' | |
| 60 exp_source_line = 0 | |
| 61 cb_args = (addr, exp_sym_name, exp_source_path, exp_source_line) | |
| 62 symbolizer.SymbolizeAsync(addr, cb_args) | |
| 63 | |
| 64 # Test symbols with no name or sym info. | |
| 65 addr = 2 * 1024 * 1024 # mock_addr2line treats this addr range specially. | |
| 66 exp_sym_name = '??' | |
| 67 exp_source_path = '??/mock_lib1.so+0x200000' | |
| 68 exp_source_line = 0 | |
| 69 cb_args = (addr, exp_sym_name, exp_source_path, exp_source_line) | |
| 70 symbolizer.SymbolizeAsync(addr, cb_args) | |
| 71 | |
| 72 symbolizer.Join() | |
| 73 | |
| 74 def _RunTest(self, max_concurrent_jobs, num_symbols): | |
| 75 symbolizer = elf_symbolizer.ELFSymbolizer( | |
| 76 elf_file_path='/path/doesnt/matter/mock_lib1.so', | |
| 77 addr2line_path=_MOCK_A2L_PATH, | |
| 78 callback=self._callback, | |
| 79 max_concurrent_jobs=max_concurrent_jobs, | |
| 80 addr2line_timeout=0.5) | |
| 81 | |
| 82 for addr in xrange(num_symbols): | |
| 83 exp_sym_name = 'mock_sym_for_addr_%d' % addr | |
| 84 exp_source_path = 'mock_src/mock_lib1.so.c' | |
| 85 exp_source_line = addr | |
| 86 cb_args = (addr, exp_sym_name, exp_source_path, exp_source_line) | |
| 87 symbolizer.SymbolizeAsync(addr, cb_args) | |
| 88 | |
| 89 symbolizer.Join() | |
| 90 | |
| 91 # Check that all the expected callbacks have been received. | |
| 92 for addr in xrange(num_symbols): | |
| 93 self.assertIn(addr, self._resolved_addresses) | |
| 94 self._resolved_addresses.remove(addr) | |
| 95 | |
| 96 # Check for unexpected callbacks. | |
| 97 self.assertEqual(len(self._resolved_addresses), 0) | |
| 98 | |
| 99 def _SymbolizeCallback(self, sym_info, cb_args): | |
| 100 self.assertTrue(isinstance(sym_info, elf_symbolizer.ELFSymbolInfo)) | |
| 101 self.assertTrue(isinstance(cb_args, tuple)) | |
| 102 self.assertEqual(len(cb_args), 4) | |
| 103 | |
| 104 # Unpack expectations from the callback extra argument. | |
| 105 (addr, exp_sym_name, exp_source_path, exp_source_line) = cb_args | |
| 106 self.assertEqual(sym_info.name, exp_sym_name) | |
| 107 self.assertEqual(sym_info.source_path, exp_source_path) | |
| 108 self.assertEqual(sym_info.source_line, exp_source_line) | |
| 109 | |
| 110 # Check against duplicate callbacks. | |
| 111 self.assertNotIn(addr, self._resolved_addresses) | |
| 112 self._resolved_addresses.add(addr) | |
| 113 | |
|
bulach
2014/02/20 17:35:58
nit: add \n
Primiano Tucci (use gerrit)
2014/02/20 18:59:14
Done.
| |
| 114 if __name__ == '__main__': | |
| 115 unittest.main() | |
| OLD | NEW |