Skip to content

Commit 063b197

Browse files
committed
sync local changes
1 parent 3daa402 commit 063b197

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
lines changed

hololinked/core/thing.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,10 +359,9 @@ def exit(self) -> None:
359359
self.logger.debug("exit() called on a object that is not exposed yet.")
360360
return
361361
if self._owners:
362-
raise BreakInnerLoop # stops the inner loop of the object
363-
else:
364362
raise NotImplementedError("call exit on the top-level object, composed objects cannot exit the loop. "+
365363
f"This object belongs to {self._owners.__class__.__name__} with ID {self._owners.id}.")
364+
self.rpc_server.stop()
366365

367366

368367
@action()

hololinked/server/http/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from ...param import Parameterized
1414
from ...param.parameters import Integer, IPAddress, ClassSelector, Selector, TypedList, String
1515
from ...constants import HTTP_METHODS, ZMQ_TRANSPORTS, HTTPServerTypes, Operations
16-
from ...utils import complete_pending_tasks_in_current_loop, complete_pending_tasks_in_current_loop_async, forkable, get_IP_from_interface, get_current_async_loop, issubklass, pep8_to_dashed_name, get_default_logger, run_callable_somehow
16+
from ...utils import complete_pending_tasks_in_current_loop, complete_pending_tasks_in_current_loop_async, forkable, get_IP_from_interface, get_current_async_loop, issubklass, pep8_to_dashed_name, get_default_logger, print_pending_tasks_in_current_loop, run_callable_somehow
1717
from ...serializers.serializers import JSONSerializer
1818
from ...schema_validators import BaseSchemaValidator, JSONSchemaValidator
1919
from ...core.property import Property
@@ -217,7 +217,8 @@ def listen(self, forked: bool = False) -> None:
217217
self.tornado_instance.listen(port=self.port, address=self.address)
218218
self.logger.info(f'started webserver at {self._IP}, ready to receive requests.')
219219
self.tornado_event_loop.start()
220-
complete_pending_tasks_in_current_loop() # will reach here only when the server is stopped, so complete pending tasks
220+
if forked:
221+
complete_pending_tasks_in_current_loop() # will reach here only when the server is stopped, so complete pending tasks
221222

222223

223224
def stop(self, attempt_async_stop: bool = True) -> None:
@@ -236,7 +237,8 @@ def stop(self, attempt_async_stop: bool = True) -> None:
236237
run_callable_somehow(self.tornado_instance.close_all_connections())
237238
if self.tornado_event_loop is not None:
238239
self.tornado_event_loop.stop()
239-
# complete_pending_tasks_in_current_loop()
240+
complete_pending_tasks_in_current_loop()
241+
print_pending_tasks_in_current_loop()
240242

241243

242244
async def async_stop(self) -> None:
@@ -251,6 +253,7 @@ async def async_stop(self) -> None:
251253
await self.tornado_instance.close_all_connections()
252254
if self.tornado_event_loop is not None:
253255
self.tornado_event_loop.stop()
256+
print_pending_tasks_in_current_loop()
254257
# await complete_pending_tasks_in_current_loop_async()
255258

256259

hololinked/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,17 @@ async def complete_pending_tasks_in_current_loop_async():
155155
"""
156156
await asyncio.gather(*asyncio.all_tasks(get_current_async_loop()))
157157

158+
def print_pending_tasks_in_current_loop():
159+
"""
160+
Print all pending tasks in the current asyncio event loop.
161+
"""
162+
tasks = asyncio.all_tasks(get_current_async_loop())
163+
if not tasks:
164+
print("No pending tasks in the current event loop.")
165+
return
166+
for task in tasks:
167+
print(f"Task: {task}, Status: {task._state}")
168+
158169

159170
def get_signature(callable : typing.Callable) -> typing.Tuple[typing.List[str], typing.List[type]]:
160171
"""

tests/test_13_protocols_http.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from hololinked.core.meta import ThingMeta
55
from hololinked.server.http import HTTPServer
66
from hololinked.core.zmq.rpc_server import RPCServer # sets loop policy, TODO: move somewhere else
7-
from hololinked.utils import get_current_async_loop, issubklass
7+
from hololinked.utils import get_current_async_loop, issubklass, print_pending_tasks_in_current_loop
88

99
try:
1010
from .things import OceanOpticsSpectrometer, TestThing
@@ -18,26 +18,28 @@
1818
class TestHTTPServer(TestCase):
1919

2020

21-
def notest_1_init_run_and_stop(self):
21+
def test_1_init_run_and_stop(self):
2222
"""Test basic init, run and stop of the HTTP server."""
2323

2424
# init, run and stop synchronously
2525
server = HTTPServer(log_level=logging.ERROR+10)
26-
# self.assertTrue(server.all_ok)
27-
# server.listen(forked=True)
28-
# time.sleep(3)
29-
# server.stop()
26+
self.assertTrue(server.all_ok)
27+
server.listen(forked=True)
28+
time.sleep(3)
29+
server.stop()
30+
time.sleep(2)
3031

3132
# init, run and stop asynchronously
3233
server.listen(forked=True)
3334
time.sleep(3)
3435
get_current_async_loop().run_until_complete(server.async_stop())
36+
time.sleep(2)
3537

3638
server.listen(forked=True)
3739
time.sleep(3)
3840
response = requests.post('http://localhost:8080/stop')
3941
self.assertIn(response.status_code, [200, 201, 202, 204])
40-
42+
time.sleep(2)
4143

4244
def notest_2_add_interaction_affordance(self):
4345
"""Test adding an interaction affordance to the HTTP server."""
@@ -168,8 +170,9 @@ def test_5_http_handler_functionalities(self):
168170
8085
169171
]):
170172
thing.run_with_http_server(forked=True, port=port)
171-
time.sleep(3)
173+
time.sleep(3) # TODO: add a way to check if the server is running
172174

175+
session = requests.Session()
173176
for (method, path, body) in [
174177
('get', '/test-spectrometer/max-intensity', 16384),
175178
('get', '/test-spectrometer/serial-number', 'simulation'),
@@ -178,12 +181,22 @@ def test_5_http_handler_functionalities(self):
178181
('post', '/test-spectrometer/connect', None)
179182
]:
180183
if method == 'get':
181-
response = requests.get(f'http://localhost:{port}{path}')
184+
response = session.get(f'http://localhost:{port}{path}')
182185
elif method == 'post':
183-
response = requests.post(f'http://localhost:{port}{path}')
186+
response = session.post(f'http://localhost:{port}{path}')
184187
self.assertTrue(response.status_code in [200, 201, 202, 204])
185188
if body:
186189
self.assertTrue(response.json() == body)
190+
191+
192+
for (method, path, body) in [
193+
('post', '/test-spectrometer/exit', None),
194+
('post', '/stop', None)
195+
]:
196+
if method == 'get':
197+
response = session.get(f'http://localhost:{port}{path}')
198+
elif method == 'post':
199+
response = session.post(f'http://localhost:{port}{path}')
187200

188201

189202
def notest_5_run_thing_with_http_server(self):

0 commit comments

Comments
 (0)