Permalink
Browse files

Fix model's use of _mmap.init_mmap and add expand_filename tests

  • Loading branch information...
1 parent 146aa4d commit 8d676965620ed3cfb106cfaaa3154d88d71e44f4 @schmichael committed Nov 19, 2012
Showing with 38 additions and 20 deletions.
  1. +8 −4 mmstats/models.py
  2. +9 −16 tests/test_mmap.py
  3. +21 −0 tests/test_naming.py
View
@@ -54,12 +54,16 @@ class BaseMmStats(threading.local):
"""
def __init__(self, path=DEFAULT_PATH, filename=DEFAULT_FILENAME,
- label_prefix=None):
+ label_prefix=None):
self._removed = False
# Setup label prefix
self._label_prefix = '' if label_prefix is None else label_prefix
+ self._full_path = _expand_filename(path, filename)
+ self._filename = filename
+ self._path = path
+
self._offset = 1
# Store state for this instance's fields
@@ -73,8 +77,8 @@ def __init__(self, path=DEFAULT_PATH, filename=DEFAULT_FILENAME,
and isinstance(attrval, fields.Field)):
total_size += self._add_field(attrname, attrval)
- self._fd, self._filename, self._size, self._mm_ptr = _mmap.init_mmap(
- path=path, filename=filename, size=total_size)
+ self._fd, self._size, self._mm_ptr = _mmap.init_mmap(
+ self._full_path, size=total_size)
mmap_t = ctypes.c_char * self._size
self._mmap = mmap_t.from_address(self._mm_ptr)
ver = ctypes.c_byte.from_address(self._mm_ptr)
@@ -100,7 +104,7 @@ def _init_fields(self, total_size):
@property
def filename(self):
- return self._filename
+ return self._full_path
@property
def label_prefix(self):
View
@@ -13,42 +13,35 @@ def test_pagesize(self):
"""PAGESIZE > 0"""
self.assertTrue(_mmap.PAGESIZE > 0, _mmap.PAGESIZE)
- def test_init_alt_name(self):
+ def test_mmap_creation(self):
expected_fn = os.path.join(self.path, 'mmstats-test_init_alt_name')
self.assertFalse(os.path.exists(expected_fn))
- _, fn, sz, m = _mmap.init_mmap(
- path=self.path, filename='mmstats-test_init_alt_name')
- self.assertEqual(fn, expected_fn)
- self.assertTrue(os.path.exists(fn))
+ _, sz, m = _mmap.init_mmap(expected_fn)
+ self.assertTrue(os.path.exists(expected_fn))
def test_size_adjusting1(self):
"""mmapped files must be at least PAGESIZE in size"""
- _, _, sz, m = _mmap.init_mmap(path=self.path,
- filename='mmstats-test_size_adjusting-1', size=1)
+ fn = os.path.join(self.path, 'mmstats-test_size_adjusting-1')
+ _, sz, m = _mmap.init_mmap(fn, size=1)
self.assertEqual(sz, _mmap.PAGESIZE)
for i in range(sz):
self.assertEqual(ctypes.c_char.from_address(m+i).value, '\x00')
def test_size_adjusting2(self):
"""mmapped files must be multiples of PAGESIZE"""
- _, _, sz, m = _mmap.init_mmap(
- path=self.path,
- filename='mmstats-test_size_adjusting-2',
- size=(_mmap.PAGESIZE + 1)
- )
+ fn = os.path.join(self.path, 'mmstats-test_size_adjusting-2')
+ _, sz, m = _mmap.init_mmap(fn, size=(_mmap.PAGESIZE + 1))
self.assertEqual(sz, _mmap.PAGESIZE * 2)
for i in range(sz):
self.assertEqual(ctypes.c_char.from_address(m+i).value, '\x00')
def test_truncate(self):
"""mmapped files must be initialized with null bytes"""
- _, fn, sz, m = _mmap.init_mmap(
- path=self.path,
- filename='mmstats-test_truncate',
- )
+ fn = os.path.join(self.path, 'mmstats-test_truncate')
+ _, sz, m = _mmap.init_mmap(fn)
first_byte = ctypes.c_char.from_address(m)
first_byte.value = 'X'
View
@@ -0,0 +1,21 @@
+import os
+import sys
+
+from mmstats import models, libgettid
+
+
+def test_defaults():
+ """Threadsafe defaults in expanded filename"""
+ fn = models._expand_filename()
+ assert str(os.getpid()) in fn, 'missing PID'
+ assert str(libgettid.gettid()) in fn, 'missing TID'
+ assert fn.endswith('.mmstats'), 'missing .mmstats extension'
+
+
+def test_substitutions():
+ fn = models._expand_filename(filename=' {CMD} {CMD} {PID} {TID}').split()
+ cmd = os.path.basename(sys.argv[0])
+ assert fn[1] == cmd, fn[1] + ' != ' + cmd
+ assert fn[1] == fn[2], 'unable to repeat substitutions'
+ assert fn[3] == str(os.getpid()), 'PID wrong'
+ assert fn[4] == str(libgettid.gettid()), 'TID wrong'

0 comments on commit 8d67696

Please sign in to comment.