Private instance vars, public get properties
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import unittest
|
||||
from datetime import datetime
|
||||
import os
|
||||
import time
|
||||
import unittest
|
||||
|
||||
from sia.working_memory import WorkingMemory
|
||||
from sia.stop_command import StopCommand
|
||||
from sia.reasoning_entry import ReasoningEntry
|
||||
from sia.background_entry import BackgroundEntry
|
||||
|
||||
class StopCommandTest(unittest.TestCase):
|
||||
@@ -12,34 +12,38 @@ class StopCommandTest(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_stop_command_cleanup(self):
|
||||
"""Test stop command cleans up and removes all entries"""
|
||||
# Add a mix of entry types including a background process
|
||||
entries = [
|
||||
ReasoningEntry("test content", "id-1", self.test_timestamp),
|
||||
BackgroundEntry("sleep 10", "id-2", self.test_timestamp)
|
||||
]
|
||||
# Add background process
|
||||
entry = BackgroundEntry("sleep 10", "test-id", self.test_timestamp)
|
||||
self.memory.add_entry(entry)
|
||||
|
||||
for entry in entries:
|
||||
self.memory.add_entry(entry)
|
||||
|
||||
# Start the background process
|
||||
background_entry = self.memory.get_entry("id-2")
|
||||
background_entry.update()
|
||||
self.assertIsNotNone(background_entry.process)
|
||||
process_pid = background_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"))
|
||||
|
||||
# Execute stop command
|
||||
command = StopCommand()
|
||||
result = command.execute(self.memory)
|
||||
|
||||
# Verify all entries removed
|
||||
# Verify entries removed and process terminated
|
||||
self.assertTrue(result.should_stop)
|
||||
self.assertEqual(self.memory.get_entries_count(), 0)
|
||||
|
||||
# Verify background process was terminated
|
||||
# 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)
|
||||
Reference in New Issue
Block a user