Private instance vars, public get properties
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from datetime import datetime
|
||||
import os
|
||||
import time
|
||||
import unittest
|
||||
import xml.etree.ElementTree as ET
|
||||
@@ -12,6 +13,7 @@ from sia.web_io_buffer import WebIOBuffer
|
||||
from sia.working_memory import WorkingMemory
|
||||
from sia.write_entry import WriteEntry
|
||||
|
||||
|
||||
class MockEntry(Entry):
|
||||
"""Mock entry class for testing"""
|
||||
def __init__(self, id: str, timestamp: datetime):
|
||||
@@ -31,6 +33,17 @@ class WorkingMemoryTest(unittest.TestCase):
|
||||
"""Set up test cases with a fresh working memory"""
|
||||
self.memory = WorkingMemory()
|
||||
self.test_timestamp = datetime(2024, 10, 31, 12, 0, 0)
|
||||
|
||||
def wait_for_process_start(self, entry: BackgroundEntry, timeout=1.0):
|
||||
"""Wait for process to start, checking context pid"""
|
||||
start_time = time.time()
|
||||
while time.time() - start_time < timeout:
|
||||
entry.update()
|
||||
context = entry.generate_context()
|
||||
if context.get("pid") is not None:
|
||||
return True
|
||||
time.sleep(0.1)
|
||||
return False
|
||||
|
||||
def test_initialization(self):
|
||||
"""Test initial state of working memory"""
|
||||
@@ -147,46 +160,40 @@ class WorkingMemoryTest(unittest.TestCase):
|
||||
|
||||
def test_remove_entry_cleanup(self):
|
||||
"""Test that removing an entry triggers cleanup"""
|
||||
# Create a background process entry
|
||||
entry = BackgroundEntry("sleep 10", "test-id", datetime(2024, 10, 31, 12, 0, 0))
|
||||
# Create and start background process
|
||||
entry = BackgroundEntry("sleep 10", "test-id", self.test_timestamp)
|
||||
self.memory.add_entry(entry)
|
||||
|
||||
# Start the process
|
||||
entry.update()
|
||||
self.assertIsNotNone(entry.process)
|
||||
process_pid = entry.process.pid
|
||||
# Wait for process to start and get PID
|
||||
self.assertTrue(self.wait_for_process_start(entry))
|
||||
context = entry.generate_context()
|
||||
pid = int(context.get("pid"))
|
||||
|
||||
# Remove entry should trigger cleanup
|
||||
self.memory.remove_entry("test-id")
|
||||
# Remove entry and verify process terminated
|
||||
self.memory.remove_entry(entry.id)
|
||||
|
||||
# Verify process was terminated
|
||||
time.sleep(0.1) # Give process time to terminate
|
||||
with self.assertRaises(ProcessLookupError):
|
||||
import os
|
||||
os.kill(process_pid, 0)
|
||||
|
||||
os.kill(pid, 0)
|
||||
|
||||
def test_cleanup_on_memory_clear(self):
|
||||
"""Test that clearing memory properly cleans up all entries"""
|
||||
# Add multiple background processes
|
||||
processes = []
|
||||
pids = []
|
||||
for i in range(3):
|
||||
entry = BackgroundEntry(
|
||||
"sleep 10",
|
||||
f"id-{i}",
|
||||
datetime(2024, 10, 31, 12, 0, 0)
|
||||
)
|
||||
entry = BackgroundEntry("sleep 10", f"id-{i}", self.test_timestamp)
|
||||
self.memory.add_entry(entry)
|
||||
entry.update()
|
||||
processes.append(entry.process.pid)
|
||||
self.assertTrue(self.wait_for_process_start(entry))
|
||||
context = entry.generate_context()
|
||||
pids.append(int(context.get("pid")))
|
||||
|
||||
# Clear memory
|
||||
self.memory.clear()
|
||||
|
||||
# Verify all processes were terminated
|
||||
time.sleep(0.1) # Give processes time to terminate
|
||||
for pid in processes:
|
||||
for pid in pids:
|
||||
with self.assertRaises(ProcessLookupError):
|
||||
import os
|
||||
os.kill(pid, 0)
|
||||
|
||||
self.assertEqual(self.memory.get_entries_count(), 0)
|
||||
@@ -194,14 +201,13 @@ class WorkingMemoryTest(unittest.TestCase):
|
||||
def test_cleanup_on_memory_deletion(self):
|
||||
"""Test that deleting memory properly cleans up all entries"""
|
||||
# Add a background process
|
||||
entry = BackgroundEntry(
|
||||
"sleep 10",
|
||||
"test-id",
|
||||
datetime(2024, 10, 31, 12, 0, 0)
|
||||
)
|
||||
entry = BackgroundEntry("sleep 10", "test-id", self.test_timestamp)
|
||||
self.memory.add_entry(entry)
|
||||
entry.update()
|
||||
process_pid = entry.process.pid
|
||||
|
||||
# Wait for process to start and get PID
|
||||
self.assertTrue(self.wait_for_process_start(entry))
|
||||
context = entry.generate_context()
|
||||
pid = int(context.get("pid"))
|
||||
|
||||
# Delete memory
|
||||
del self.memory
|
||||
@@ -209,8 +215,7 @@ class WorkingMemoryTest(unittest.TestCase):
|
||||
# Verify process was terminated
|
||||
time.sleep(0.1) # Give process time to terminate
|
||||
with self.assertRaises(ProcessLookupError):
|
||||
import os
|
||||
os.kill(process_pid, 0)
|
||||
os.kill(pid, 0)
|
||||
|
||||
def test_get_entries(self):
|
||||
"""Test getting all entries"""
|
||||
|
||||
Reference in New Issue
Block a user