| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import argparse |
| 6 |
import os |
| 7 |
import sys |
| 8 |
from unittest.mock import patch |
| 9 |
import pytest |
| 10 |
import scalene.scalene_preload |
| 11 |
|
| 12 |
|
| 13 |
@pytest.fixture |
| 14 |
def args(): |
| 15 |
parser = argparse.ArgumentParser() |
| 16 |
parser.add_argument("--allocation_sampling_window", type=int, default=1) |
| 17 |
parser.add_argument("--memory", action="store_true") |
| 18 |
return parser.parse_args([]) |
| 19 |
|
| 20 |
|
| 21 |
@pytest.fixture |
| 22 |
def clean_environ(): |
| 23 |
original_environ = os.environ.copy() |
| 24 |
yield |
| 25 |
os.environ = original_environ |
| 26 |
|
| 27 |
|
| 28 |
def test_get_preload_environ_darwin_memory(args, clean_environ): |
| 29 |
|
| 30 |
args.memory = True |
| 31 |
with patch.object(sys, "platform", "darwin"): |
| 32 |
env = scalene.scalene_preload.ScalenePreload.get_preload_environ(args) |
| 33 |
assert "DYLD_INSERT_LIBRARIES" in env |
| 34 |
assert "libscalene.dylib" in env["DYLD_INSERT_LIBRARIES"] |
| 35 |
assert env["OBJC_DISABLE_INITIALIZE_FORK_SAFETY"] == "YES" |
| 36 |
|
| 37 |
|
| 38 |
def test_get_preload_environ_linux_memory(args, clean_environ): |
| 39 |
|
| 40 |
args.memory = True |
| 41 |
with patch.object(sys, "platform", "linux"): |
| 42 |
with patch.dict("os.environ", {"PYTHONMALLOC": "malloc"}): |
| 43 |
env = scalene.scalene_preload.ScalenePreload.get_preload_environ(args) |
| 44 |
assert "LD_PRELOAD" in env |
| 45 |
assert "libscalene.so" in env["LD_PRELOAD"] |
| 46 |
assert env["PYTHONMALLOC"] == "default" |
| 47 |
|
| 48 |
|
| 49 |
def test_get_preload_environ_linux_no_memory(args, clean_environ): |
| 50 |
|
| 51 |
args.memory = False |
| 52 |
with patch.object(sys, "platform", "linux"): |
| 53 |
env = scalene.scalene_preload.ScalenePreload.get_preload_environ(args) |
| 54 |
assert "LD_PRELOAD" not in env |
| 55 |
|
| 56 |
|
| 57 |
def test_get_preload_environ_win32(args, clean_environ): |
| 58 |
|
| 59 |
with patch.object(sys, "platform", "win32"): |
| 60 |
env = scalene.scalene_preload.ScalenePreload.get_preload_environ(args) |
| 61 |
assert args.memory is False |