| 1 |
import pytest |
| 2 |
from rpython.rlib import rvmprof |
| 3 |
|
| 4 |
class FakeVMProf(object): |
| 5 |
|
| 6 |
def __init__(self): |
| 7 |
self._enabled = False |
| 8 |
self._ignore_signals = 1 |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
def stop_sampling(self): |
| 14 |
self._ignore_signals += 1 |
| 15 |
|
| 16 |
def start_sampling(self): |
| 17 |
assert self._ignore_signals > 0, ('calling start_sampling() without ' |
| 18 |
'the corresponding stop_sampling()?') |
| 19 |
self._ignore_signals -= 1 |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
@property |
| 26 |
def is_sampling_enabled(self): |
| 27 |
return self._ignore_signals == 0 |
| 28 |
|
| 29 |
def check_status(self): |
| 30 |
""" |
| 31 |
To be called during test teardown |
| 32 |
""" |
| 33 |
if self._ignore_signals != 1: |
| 34 |
msg = ('Invalid value for fakevmprof._ignore_signals: expected 1, ' |
| 35 |
'got %d. This probably means that you called ' |
| 36 |
'{start,stop}_sampling() a wrong number of times') |
| 37 |
raise ValueError(msg % self._ignore_signals) |
| 38 |
|
| 39 |
|
| 40 |
@pytest.fixture |
| 41 |
def fakevmprof(request, monkeypatch): |
| 42 |
fake = FakeVMProf() |
| 43 |
monkeypatch.setattr(rvmprof.rvmprof, '_vmprof_instance', fake) |
| 44 |
request.addfinalizer(fake.check_status) |
| 45 |
return fake |