OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-cogserver commits / 6ae3e95

Remove the is-server-running function
6ae3e9534fbdceb82ea34ecd14309de60a6bfffb
by Linas Vepstas <linasvepstas@gmail.com> · 2025-12-14T23:28:58.000Z

Changed files (3) +61 -39

opencog/cogserver/python/__init__.py +0 -2
……@@ -6,12 +6,10 @@ try:
66 from .cogserver import (
77 start_cogserver,
88 stop_cogserver,
9 is_cogserver_running,
109 )
1110 __all__ = [
1211 'start_cogserver',
1312 'stop_cogserver',
14 'is_cogserver_running',
1513 ]
1614except ImportError:
1715 # Module not fully installed yet
opencog/cogserver/python/cogserver.py +18 -9
……@@ -6,6 +6,24 @@
66# the CogServer network server.
77#
88
9import ctypes
10import os
11
12# Load the servernode library to register CogServerNode type
13# This must happen before we can use types.CogServerNode
14_lib_paths = [
15 "/usr/local/lib/opencog/libservernode.so",
16 "/usr/lib/opencog/libservernode.so",
17]
18_servernode_lib = None
19for _path in _lib_paths:
20 if os.path.exists(_path):
21 _servernode_lib = ctypes.CDLL(_path, mode=ctypes.RTLD_GLOBAL)
22 break
23
24if _servernode_lib is None:
25 raise ImportError("Could not find libservernode.so - is cogserver installed?")
26
927from opencog.type_constructors import get_thread_atomspace, FloatValue, VoidValue, Predicate
1028from opencog.atomspace import types
1129
……@@ -90,12 +108,3 @@ def stop_cogserver():
90108 _server_running = False
91109
92110 return True
93
94def is_cogserver_running():
95 """Check if the CogServer is currently running.
96
97 Returns:
98 bool: True if server is running, False otherwise.
99 """
100 global _server_running
101 return _server_running
tests/cython/test_cogserver.py +43 -28
……@@ -15,10 +15,10 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../../build/opencog/
1515
1616try:
1717 from opencog.atomspace import AtomSpace
18 from opencog.type_constructors import get_thread_atomspace, Predicate
1819 from opencog.cogserver import (
1920 start_cogserver,
2021 stop_cogserver,
21 is_cogserver_running
2222 )
2323except ImportError as e:
2424 print(f"Error importing modules: {e}")
……@@ -26,35 +26,52 @@ except ImportError as e:
2626 sys.exit(1)
2727
2828
29# Global to track the current server node for cleanup
30_current_server = None
31
32
33def is_server_running(server_node):
34 """Check if a CogServerNode is running by querying its value."""
35 if server_node is None:
36 return False
37 val = server_node.get_value(Predicate("*-is-running?-*"))
38 if val is None:
39 return False
40 # val is a BoolValue; check if it's true
41 return val.to_list()[0] == 1
42
43
2944class TestCogServer(unittest.TestCase):
3045 """Test cases for CogServer Python bindings."""
3146
3247 def setUp(self):
3348 """Ensure server is stopped before each test."""
34 if is_cogserver_running():
49 global _current_server
50 if _current_server is not None and is_server_running(_current_server):
3551 stop_cogserver()
52 _current_server = None
3653 # Small delay to ensure clean state
3754 time.sleep(0.1)
3855
3956 def tearDown(self):
4057 """Ensure server is stopped after each test."""
41 if is_cogserver_running():
58 global _current_server
59 if _current_server is not None and is_server_running(_current_server):
4260 stop_cogserver()
61 _current_server = None
4362 # Small delay to ensure clean shutdown
4463 time.sleep(0.1)
4564
4665 def test_basic_start_stop(self):
4766 """Test basic server start/stop functionality."""
48 # Check server is not running initially
49 self.assertFalse(is_cogserver_running(),
50 "Server should not be running initially")
67 global _current_server
5168
5269 # Start server with custom port to avoid conflicts
53 result = start_cogserver(console_port=17550, enable_web=False, enable_mcp=False)
54 self.assertTrue(result, "start_cogserver should return True")
70 _current_server = start_cogserver(console_port=17550, enable_web=False, enable_mcp=False)
71 self.assertIsNotNone(_current_server, "start_cogserver should return CogServerNode")
5572
5673 # Check server is running
57 self.assertTrue(is_cogserver_running(),
74 self.assertTrue(is_server_running(_current_server),
5875 "Server should be running after start")
5976
6077 # Stop the server
……@@ -62,13 +79,15 @@ class TestCogServer(unittest.TestCase):
6279 self.assertTrue(result, "stop_cogserver should return True")
6380
6481 # Check server is stopped
65 self.assertFalse(is_cogserver_running(),
82 self.assertFalse(is_server_running(_current_server),
6683 "Server should not be running after stop")
6784
6885 def test_custom_ports(self):
6986 """Test server with custom port configuration."""
87 global _current_server
88
7089 # Start server with custom ports
71 result = start_cogserver(
90 _current_server = start_cogserver(
7291 console_port=17552,
7392 web_port=18581,
7493 mcp_port=18889,
……@@ -76,24 +95,26 @@ class TestCogServer(unittest.TestCase):
7695 enable_web=False, # Disable web to avoid SSL issues in tests
7796 enable_mcp=False
7897 )
79 self.assertTrue(result, "Server should start with custom ports")
98 self.assertIsNotNone(_current_server, "Server should start with custom ports")
8099
81100 # Check server is running
82 self.assertTrue(is_cogserver_running(), "Server should be running")
101 self.assertTrue(is_server_running(_current_server), "Server should be running")
83102
84103 # Let it run briefly
85104 time.sleep(0.5)
86105
87106 # Stop the server
88107 stop_cogserver()
89 self.assertFalse(is_cogserver_running(),
108 self.assertFalse(is_server_running(_current_server),
90109 "Server should be stopped")
91110
92111 def test_double_start_error(self):
93112 """Test that starting server twice raises error."""
113 global _current_server
114
94115 # Start server
95 start_cogserver(console_port=17553, enable_web=False, enable_mcp=False)
96 self.assertTrue(is_cogserver_running(), "Server should be running")
116 _current_server = start_cogserver(console_port=17553, enable_web=False, enable_mcp=False)
117 self.assertTrue(is_server_running(_current_server), "Server should be running")
97118
98119 # Try to start again - should raise RuntimeError
99120 with self.assertRaises(RuntimeError) as context:
……@@ -113,16 +134,8 @@ class TestCogServer(unittest.TestCase):
113134 self.assertIn("Invalid", str(context.exception))
114135 self.assertIn("port", str(context.exception))
115136
116 # Ensure server is not running
117 self.assertFalse(is_cogserver_running(),
118 "Server should not be running after error")
119
120137 def test_stop_when_not_running(self):
121138 """Test stopping server when not running."""
122 # Ensure server is not running
123 self.assertFalse(is_cogserver_running(),
124 "Server should not be running")
125
126139 # Try to stop - should return False
127140 result = stop_cogserver()
128141 self.assertFalse(result,
……@@ -131,18 +144,20 @@ class TestCogServer(unittest.TestCase):
131144
132145 def test_multiple_start_stop_cycles(self):
133146 """Test multiple start/stop cycles."""
147 global _current_server
148
134149 for i in range(3):
135150 # Start server
136 result = start_cogserver(console_port=17560 + i,
151 _current_server = start_cogserver(console_port=17560 + i,
137152 enable_web=False, enable_mcp=False)
138 self.assertTrue(result, f"Server should start on cycle {i}")
139 self.assertTrue(is_cogserver_running(),
153 self.assertIsNotNone(_current_server, f"Server should start on cycle {i}")
154 self.assertTrue(is_server_running(_current_server),
140155 f"Server should be running on cycle {i}")
141156
142157 # Stop server
143158 result = stop_cogserver()
144159 self.assertTrue(result, f"Server should stop on cycle {i}")
145 self.assertFalse(is_cogserver_running(),
160 self.assertFalse(is_server_running(_current_server),
146161 f"Server should be stopped on cycle {i}")
147162
148163 # Small delay between cycles