|
| 1 | +"""Tests for transitioning Manager from 'idle' to 'connected' state.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from test_utils import setup_manager_to_world_ready |
| 5 | +from test_utils import setup_manager_to_application_running |
| 6 | + |
| 7 | + |
| 8 | +class DummyProc: |
| 9 | + """Dummy process class for testing suspend and resume methods.""" |
| 10 | + |
| 11 | + def suspend(self): |
| 12 | + """Simulate suspending the process.""" |
| 13 | + pass |
| 14 | + |
| 15 | + def resume(self): |
| 16 | + """Simulate resuming the process.""" |
| 17 | + pass |
| 18 | + |
| 19 | + |
| 20 | +def test_idle_to_connected(manager): |
| 21 | + """Test transitioning Manager from 'idle' to 'connected' state.""" |
| 22 | + # Initial state should be 'idle' |
| 23 | + assert manager.state == "idle" |
| 24 | + # Simulate the 'connect' event |
| 25 | + manager.trigger("connect", event=None) |
| 26 | + # State should now be 'connected' |
| 27 | + assert manager.state == "connected" |
| 28 | + # Check that the consumer received the expected message |
| 29 | + msgs = manager.consumer.messages |
| 30 | + on_connect_msg = msgs[0][0] |
| 31 | + state_change_msg = msgs[1] |
| 32 | + # print(msgs) |
| 33 | + # Verify the first message (on connect) |
| 34 | + assert on_connect_msg[0]["robotics_backend_version"] == b"test_image_tag" |
| 35 | + assert on_connect_msg[0]["ros_version"] == b"humble" |
| 36 | + assert on_connect_msg[0]["gpu_avaliable"] == "OFF" |
| 37 | + |
| 38 | + # Verify the state change message |
| 39 | + assert state_change_msg[0][0]["state"] == "connected" |
| 40 | + assert state_change_msg[1]["command"] == "state-changed" |
| 41 | + |
| 42 | + |
| 43 | +def test_idle_to_connected_with_exception(manager): |
| 44 | + """Test transitioning Manager from 'idle' to 'connected' state with an exception.""" |
| 45 | + # Simulate an exception during the connection process |
| 46 | + manager.consumer.send_message = lambda *args, **kwargs: ( |
| 47 | + 1 / 0 # This will raise a ZeroDivisionError |
| 48 | + ) |
| 49 | + |
| 50 | + with pytest.raises(ZeroDivisionError): |
| 51 | + manager.trigger("connect", event=None) |
| 52 | + |
| 53 | + # State should still be 'idle' after the exception |
| 54 | + assert manager.state == "idle" |
| 55 | + |
| 56 | + # Check that no messages were sent to the consumer |
| 57 | + assert not manager.consumer.messages |
| 58 | + |
| 59 | + |
| 60 | +def test_disconnect_valid(manager, monkeypatch): |
| 61 | + """Test the valid disconnect transition in the Manager.""" |
| 62 | + # Ensure the manager is in a state where it can disconnect |
| 63 | + setup_manager_to_world_ready(manager, monkeypatch) |
| 64 | + |
| 65 | + # Mock needed methods and attributes |
| 66 | + manager.on_disconnect = lambda event: None |
| 67 | + |
| 68 | + # Trigger the disconnect transition |
| 69 | + manager.trigger("disconnect") |
| 70 | + |
| 71 | + # Check that the state has changed to 'idle' |
| 72 | + assert manager.state == "idle" |
| 73 | + |
| 74 | + |
| 75 | +def test_disconnect_invalid_process(manager, monkeypatch): |
| 76 | + """ |
| 77 | + Test the invalid disconnect transition in the Manager. |
| 78 | +
|
| 79 | + Tests that the disconnect raises an Exception when the process |
| 80 | + cannot be disconnected properly. |
| 81 | + """ |
| 82 | + # Ensure the manager is in a state where it can disconnect |
| 83 | + setup_manager_to_application_running(manager, monkeypatch) |
| 84 | + # Mock needed methods and attributes |
| 85 | + monkeypatch.setattr("os.execl", lambda *args: None) |
| 86 | + monkeypatch.setattr( |
| 87 | + "manager.manager.manager.stop_process_and_children", |
| 88 | + lambda proc: (_ for _ in ()).throw(Exception("Process cannot be stopped")), |
| 89 | + ) |
| 90 | + manager.application_process = DummyProc() |
| 91 | + manager.terminate_harmonic_processes = lambda: None |
| 92 | + |
| 93 | + # Trigger the disconnect transition |
| 94 | + manager.trigger("disconnect") |
| 95 | + |
| 96 | + # Optional: can test that exception has been logged |
| 97 | + |
| 98 | + # Check that the state changed |
| 99 | + assert manager.state == "idle" |
0 commit comments