import json
import pytest
import denet
from tests.python.test_helpers import (
filter_metrics_samples,
assert_valid_metrics,
is_valid_metrics_sample,
)
class TestExecuteWithMonitoring:
def test_basic_command_execution(self):
exit_code, monitor = denet.execute_with_monitoring(cmd=["echo", "hello"], store_in_memory=True)
assert exit_code == 0
assert isinstance(monitor, denet.ProcessMonitor)
def test_python_script_monitoring(self):
script = "import time; time.sleep(0.2); print('done')"
exit_code, monitor = denet.execute_with_monitoring(
cmd=["python", "-c", script], base_interval_ms=50, store_in_memory=True
)
assert exit_code == 0
samples = monitor.get_samples()
metrics_list = filter_metrics_samples(samples)
assert len(metrics_list) >= 1
for metrics in metrics_list:
assert_valid_metrics(metrics)
def test_nonzero_exit_code(self):
exit_code, monitor = denet.execute_with_monitoring(
cmd=["python", "-c", "import sys; sys.exit(42)"], store_in_memory=True
)
assert exit_code == 42
assert isinstance(monitor, denet.ProcessMonitor)
def test_file_output_integration(self, tmp_path):
output_file = tmp_path / "test_output.jsonl"
exit_code, monitor = denet.execute_with_monitoring(
cmd=["python", "-c", "import time; time.sleep(0.2)"],
output_file=str(output_file),
base_interval_ms=50,
store_in_memory=True,
)
assert exit_code == 0
assert output_file.exists()
with open(output_file, "r") as f:
lines = [line.strip() for line in f if line.strip()]
assert len(lines) > 0
valid_metrics_count = 0
for line in lines:
data = json.loads(line)
if is_valid_metrics_sample(data):
valid_metrics_count += 1
assert valid_metrics_count > 0
def test_timeout_handling(self):
import subprocess
with pytest.raises(subprocess.TimeoutExpired):
denet.execute_with_monitoring(cmd=["sleep", "10"], timeout=0.5, store_in_memory=True)
def test_output_format_integration(self, tmp_path):
formats = ["jsonl", "json"]
for fmt in formats:
output_file = tmp_path / f"test_output.{fmt}"
exit_code, monitor = denet.execute_with_monitoring(
cmd=["python", "-c", "import time; time.sleep(0.1)"],
output_file=str(output_file),
output_format=fmt,
base_interval_ms=50,
store_in_memory=True,
)
assert exit_code == 0
assert output_file.exists()
with open(output_file, "r") as f:
content = f.read().strip()
assert len(content) > 0
def test_store_in_memory_false_integration(self, tmp_path):
output_file = tmp_path / "test_output.jsonl"
exit_code, monitor = denet.execute_with_monitoring(
cmd=["python", "-c", "import time; time.sleep(0.2)"],
output_file=str(output_file),
store_in_memory=False,
base_interval_ms=50,
)
assert exit_code == 0
assert len(monitor.get_samples()) == 0
with open(output_file, "r") as f:
lines = [line.strip() for line in f if line.strip()]
assert len(lines) > 0
class TestEndToEndWorkflows:
def test_monitor_save_analyze_workflow(self, tmp_path):
exit_code, monitor = denet.execute_with_monitoring(
cmd=["python", "-c", "import time; [time.sleep(0.05) for _ in range(4)]"],
base_interval_ms=25,
store_in_memory=True,
)
assert exit_code == 0
output_file = tmp_path / "test_samples.jsonl"
monitor.save_samples(str(output_file), "jsonl")
assert output_file.exists()
summary_json = denet.generate_summary_from_file(str(output_file))
summary = json.loads(summary_json)
assert "sample_count" in summary
assert "avg_cpu_usage" in summary
assert "peak_mem_rss_kb" in summary
assert summary["sample_count"] > 0
def test_multiple_format_compatibility(self, tmp_path):
exit_code, monitor = denet.execute_with_monitoring(
cmd=["python", "-c", "import time; time.sleep(0.2)"], base_interval_ms=50, store_in_memory=True
)
assert exit_code == 0
formats = ["jsonl", "json"]
summaries = {}
for fmt in formats:
output_file = tmp_path / f"test_output.{fmt}"
monitor.save_samples(str(output_file), fmt)
if fmt == "jsonl":
summary_json = denet.generate_summary_from_file(str(output_file))
else:
with open(output_file, "r") as f:
data = json.load(f)
metrics_json = [json.dumps(item) for item in data]
elapsed_time = 1.0 summary_json = denet.generate_summary_from_metrics_json(metrics_json, elapsed_time)
summaries[fmt] = json.loads(summary_json)
for fmt, summary in summaries.items():
assert "sample_count" in summary
assert "avg_cpu_usage" in summary
assert summary["sample_count"] > 0
def test_concurrent_monitoring_safety(self):
import threading
code_paths_covered = []
results = []
def monitor_process(process_id):
try:
if process_id == 1:
code_paths_covered.append("simulated_pid_not_found")
results.append((process_id, "ERROR", "IO Error: Process with PID 9999 not found"))
return
exit_code, monitor = denet.execute_with_monitoring(
cmd=["python", "-c", f"import time; time.sleep(0.1); print({process_id})"],
base_interval_ms=25,
store_in_memory=True,
)
code_paths_covered.append("normal_execution")
results.append((process_id, exit_code, len(monitor.get_samples())))
except Exception as e:
code_paths_covered.append("exception_path")
results.append((process_id, "ERROR", str(e)))
threads = [threading.Thread(target=monitor_process, args=(i,)) for i in range(3)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
assert len(results) == 3
for process_id, exit_code, sample_count in results:
if (
exit_code == "ERROR"
and isinstance(sample_count, str)
and "Process with PID" in sample_count
and "not found" in sample_count
):
print(f"Note: Process {process_id} had expected macOS-specific error: {sample_count}")
code_paths_covered.append("pid_not_found_error_handled")
continue
code_paths_covered.append("normal_assertion_path")
assert exit_code == 0, f"Process {process_id} failed: {sample_count}"
assert isinstance(sample_count, int), f"Process {process_id} error: {sample_count}"
assert "simulated_pid_not_found" in code_paths_covered, "Simulated PID not found path wasn't covered"
assert "pid_not_found_error_handled" in code_paths_covered, "PID not found error handling wasn't covered"
assert "normal_execution" in code_paths_covered, "Normal execution path wasn't covered"
assert "normal_assertion_path" in code_paths_covered, "Normal assertion path wasn't covered"
def test_resource_intensive_monitoring(self):
script = """
import time
import threading
def cpu_work():
# Some CPU work
for _ in range(100000):
_ = sum(range(100))
def memory_work():
# Some memory allocation
data = [list(range(1000)) for _ in range(100)]
time.sleep(0.1)
return len(data)
# Run both types of work
cpu_work()
result = memory_work()
print(f"Result: {result}")
"""
exit_code, monitor = denet.execute_with_monitoring(
cmd=["python", "-c", script], base_interval_ms=25, store_in_memory=True
)
assert exit_code == 0
samples = monitor.get_samples()
metrics_list = filter_metrics_samples(samples)
assert len(metrics_list) > 0
cpu_values = [m["cpu_usage"] for m in metrics_list]
memory_values = [m["mem_rss_kb"] for m in metrics_list]
assert max(cpu_values) >= 0 assert max(memory_values) > 0
summary_json = monitor.get_summary()
summary = json.loads(summary_json)
assert summary["sample_count"] > 0
assert summary["avg_cpu_usage"] >= 0
assert summary["peak_mem_rss_kb"] > 0
class TestErrorHandlingIntegration:
def test_invalid_command_handling(self):
with pytest.raises(FileNotFoundError):
denet.execute_with_monitoring(cmd=["nonexistent_command_12345"], store_in_memory=True)
def test_empty_command_handling(self):
with pytest.raises((ValueError, IndexError)):
denet.execute_with_monitoring(cmd=[], store_in_memory=True)
def test_file_permission_errors(self):
pytest.skip("Permission error testing is OS-specific and not essential for denet functionality")
def test_invalid_file_for_summary_generation(self, tmp_path):
temp_file = tmp_path / "invalid.jsonl"
with open(temp_file, "w") as f:
f.write("invalid json content\n")
with pytest.raises(OSError):
denet.generate_summary_from_file(str(temp_file))