|
| 1 | +# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file. |
| 2 | + |
| 3 | +""" |
| 4 | +Simple Remote client test |
| 5 | +------------------------- |
| 6 | +
|
| 7 | +This test covers the simple remote client functionalities as provided by the |
| 8 | +`sigima.client` module. |
| 9 | +
|
| 10 | +In Sigima, the `sigima.tests.common.client_unit_test` module provides a set of unit |
| 11 | +tests for the client functionalities. |
| 12 | +
|
| 13 | +The purpose of these tests is to ensure that the client can correctly interact with |
| 14 | +the real server, handling various scenarios and edge cases. |
| 15 | +
|
| 16 | +The tests include: |
| 17 | +
|
| 18 | +- A function comparing the list of methods implemented by the real server |
| 19 | + (DataLab's `RemoteServer` class) to those implemented by the stub server |
| 20 | + (Sigima's `DataLabStubServer` class). |
| 21 | +- A function comparing the list of methods implemented by the full client (DataLab's |
| 22 | + `RemoteProxy` class) to those implemented by the simple client (Sigima's |
| 23 | + `SimpleRemoteProxy` class). |
| 24 | +- A function that simply runs the `sigima.tests.common.client_unit_test` suite after |
| 25 | + having launched a real server instance (DataLab application, using the |
| 26 | + `datalab.tests.run_datalab_in_background` function), ensuring that this test passes |
| 27 | + with the stub server as well as with the real server. |
| 28 | +""" |
| 29 | + |
| 30 | +# pylint: disable=invalid-name # Allows short reference names like x, y, ... |
| 31 | +# guitest: skip |
| 32 | + |
| 33 | +import inspect |
| 34 | + |
| 35 | +from guidata.env import execenv |
| 36 | +from sigima.client.remote import SimpleRemoteProxy |
| 37 | +from sigima.client.stub import DataLabStubServer |
| 38 | +from sigima.tests.common.client_unit_test import RemoteClientTester |
| 39 | + |
| 40 | +from datalab.control.baseproxy import AbstractDLControl |
| 41 | +from datalab.control.proxy import RemoteProxy |
| 42 | +from datalab.tests import run_datalab_in_background |
| 43 | + |
| 44 | + |
| 45 | +def test_compare_server_methods() -> None: |
| 46 | + """Compare methods implemented by RemoteServer vs DataLabStubServer. |
| 47 | +
|
| 48 | + This function compares the list of methods implemented by the real server |
| 49 | + (DataLab's RemoteServer class) to those implemented by the stub server |
| 50 | + (Sigima's DataLabStubServer class). |
| 51 | + """ |
| 52 | + execenv.print("\n=== Comparing Server Methods ===") |
| 53 | + |
| 54 | + # Get methods from RemoteServer (the real DataLab server) |
| 55 | + # RemoteServer implements AbstractDLControl methods |
| 56 | + remote_server_methods = set(AbstractDLControl.get_public_methods()) |
| 57 | + |
| 58 | + # Get methods from DataLabStubServer (the stub server) |
| 59 | + stub_server_methods = { |
| 60 | + name |
| 61 | + for name, method in inspect.getmembers(DataLabStubServer, inspect.isfunction) |
| 62 | + if not name.startswith("_") |
| 63 | + } |
| 64 | + |
| 65 | + execenv.print(f"RemoteServer methods: {len(remote_server_methods)}") |
| 66 | + execenv.print(f"DataLabStubServer methods: {len(stub_server_methods)}") |
| 67 | + |
| 68 | + # Methods in RemoteServer but not in DataLabStubServer |
| 69 | + missing_in_stub = remote_server_methods - stub_server_methods |
| 70 | + if missing_in_stub: |
| 71 | + execenv.print("\n⚠️ Methods in RemoteServer but missing in DataLabStubServer:") |
| 72 | + for method in sorted(missing_in_stub): |
| 73 | + execenv.print(f" - {method}") |
| 74 | + |
| 75 | + # Methods in DataLabStubServer but not in RemoteServer |
| 76 | + extra_in_stub = stub_server_methods - remote_server_methods |
| 77 | + if extra_in_stub: |
| 78 | + execenv.print("\n🔍 Methods in DataLabStubServer but not in RemoteServer:") |
| 79 | + for method in sorted(extra_in_stub): |
| 80 | + execenv.print(f" - {method}") |
| 81 | + |
| 82 | + # Common methods |
| 83 | + common_methods = remote_server_methods & stub_server_methods |
| 84 | + execenv.print(f"\n✅ Common methods: {len(common_methods)}") |
| 85 | + |
| 86 | + # Assert that stub server implements all required methods |
| 87 | + assert not missing_in_stub, ( |
| 88 | + f"DataLabStubServer is missing {len(missing_in_stub)} methods " |
| 89 | + f"implemented by RemoteServer: {sorted(missing_in_stub)}" |
| 90 | + ) |
| 91 | + |
| 92 | + execenv.print("✨ Server method comparison completed successfully!") |
| 93 | + |
| 94 | + |
| 95 | +def test_compare_client_methods() -> None: |
| 96 | + """Compare methods implemented by RemoteProxy vs SimpleRemoteProxy. |
| 97 | +
|
| 98 | + This function compares the list of methods implemented by the full client |
| 99 | + (DataLab's RemoteProxy class) to those implemented by the simple client |
| 100 | + (Sigima's SimpleRemoteProxy class). |
| 101 | + """ |
| 102 | + execenv.print("\n=== Comparing Client Methods ===") |
| 103 | + |
| 104 | + # Get public methods from RemoteProxy (the full client) |
| 105 | + remote_proxy_methods = { |
| 106 | + name |
| 107 | + for name, method in inspect.getmembers(RemoteProxy, inspect.ismethod) |
| 108 | + if not name.startswith("_") and callable(method) |
| 109 | + } |
| 110 | + |
| 111 | + # Get public methods from SimpleRemoteProxy (the simple client) |
| 112 | + simple_proxy_methods = { |
| 113 | + name |
| 114 | + for name, method in inspect.getmembers(SimpleRemoteProxy, inspect.ismethod) |
| 115 | + if not name.startswith("_") and callable(method) |
| 116 | + } |
| 117 | + |
| 118 | + execenv.print(f"RemoteProxy methods: {len(remote_proxy_methods)}") |
| 119 | + execenv.print(f"SimpleRemoteProxy methods: {len(simple_proxy_methods)}") |
| 120 | + |
| 121 | + # Methods in SimpleRemoteProxy but not in RemoteProxy |
| 122 | + extra_in_simple = simple_proxy_methods - remote_proxy_methods |
| 123 | + if extra_in_simple: |
| 124 | + execenv.print( |
| 125 | + "\n🔍 Methods in SimpleRemoteProxy but not in RemoteProxy " |
| 126 | + "(expected, as SimpleRemoteProxy is a subset):" |
| 127 | + ) |
| 128 | + for method in sorted(extra_in_simple): |
| 129 | + execenv.print(f" - {method}") |
| 130 | + |
| 131 | + # Methods in RemoteProxy but not in SimpleRemoteProxy |
| 132 | + missing_in_simple = remote_proxy_methods - simple_proxy_methods |
| 133 | + if missing_in_simple: |
| 134 | + execenv.print( |
| 135 | + "\n📝 Methods in RemoteProxy but not in SimpleRemoteProxy " |
| 136 | + "(expected, as SimpleRemoteProxy is simpler):" |
| 137 | + ) |
| 138 | + for method in sorted(missing_in_simple): |
| 139 | + execenv.print(f" - {method}") |
| 140 | + |
| 141 | + # Common methods |
| 142 | + common_methods = remote_proxy_methods & simple_proxy_methods |
| 143 | + execenv.print(f"\n✅ Common methods: {len(common_methods)}") |
| 144 | + |
| 145 | + execenv.print("✨ Client method comparison completed successfully!") |
| 146 | + |
| 147 | + |
| 148 | +def test_with_real_server() -> None: |
| 149 | + """Run sigima.tests.common.client_unit_test suite with real DataLab server. |
| 150 | +
|
| 151 | + This function runs the comprehensive client unit test suite after launching |
| 152 | + a real DataLab instance in the background, ensuring that the tests pass |
| 153 | + with the real server. |
| 154 | + """ |
| 155 | + execenv.print("\n=== Testing with Real DataLab Server ===") |
| 156 | + |
| 157 | + # Launch DataLab application in the background |
| 158 | + execenv.print("Launching DataLab in background...") |
| 159 | + run_datalab_in_background(wait_until_ready=True) |
| 160 | + |
| 161 | + # Import and run the comprehensive test from sigima |
| 162 | + execenv.print("Running comprehensive client tests with real server...") |
| 163 | + tester = RemoteClientTester() |
| 164 | + |
| 165 | + # Initialize connection to real DataLab server using the existing port |
| 166 | + if not tester.init_cdl(): |
| 167 | + raise ConnectionRefusedError( |
| 168 | + "Failed to connect to DataLab server. " |
| 169 | + "Make sure DataLab is running and accessible." |
| 170 | + ) |
| 171 | + |
| 172 | + try: |
| 173 | + # Run all tests |
| 174 | + tester.run_comprehensive_test() |
| 175 | + execenv.print("✨ All tests passed with real DataLab server!") |
| 176 | + finally: |
| 177 | + # Clean up |
| 178 | + tester.close_datalab() |
| 179 | + |
| 180 | + |
| 181 | +if __name__ == "__main__": |
| 182 | + test_compare_server_methods() |
| 183 | + test_compare_client_methods() |
| 184 | + test_with_real_server() |
0 commit comments