This is an automated email from the ASF dual-hosted git repository.

HyukjinKwon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark-connect-rust.git


The following commit(s) were added to refs/heads/master by this push:
     new e44f7026 [SPARK-61219][CONNECT][PYTHON][TESTS][FOLLOWUP] Test that 
stop() delivers the listener events sent while it releases the session with the 
mock server, not a live query
e44f7026 is described below

commit e44f7026a3c85918f703c20e7b1939b7855736dc
Author: Hyukjin Kwon <[email protected]>
AuthorDate: Mon Nov 9 20:17:54 2026 +0900

    [SPARK-61219][CONNECT][PYTHON][TESTS][FOLLOWUP] Test that stop() delivers 
the listener events sent while it releases the session with the mock server, 
not a live query
    
    ### What changes were proposed in this pull request?
    
    A follow-up of 
[SPARK-61219](https://issues.apache.org/jira/browse/SPARK-61219) (#402), which 
made `spark.stop()` release the session before it cancels the listener bus, so 
that the terminated events of the queries still running reach the listeners, as 
PySpark's do.
    
    Its live test, 
`test_a_query_running_at_stop_sends_its_terminated_event_to_the_listeners_as_pyspark`,
 waited for the terminated event of a `rate` query that was running at 
`stop()`. Whether the server sends that event is not under the client's 
control: the server posts it through its asynchronous listener bus while it 
releases the session, and the release can end the listener stream first. The 
test failed in the CI of unrelated PRs (#411, #413: no event within 120 s).
    
    This replaces it with a deterministic test of the client's part, in 
`streaming_listener_cases.py`: the mock server (`_mock_connect_server.py`, new 
opt-in `release_session_events` and `release_session_blocked`) sends a 
terminated event on the session's listener stream while it handles 
`ReleaseSession`, and holds the release's response until the listener got the 
event.
    
    ### Why are the changes needed?
    
    The live test was flaky in CI and failed the `Connect parity` job of 
unrelated PRs.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No, tests only.
    
    ### How was this patch tested?
    
    - The new test passes in 5 of 5 runs with master's build, and fails with a 
build of master whose `stop()` cancels the listener bus before the release (the 
order before SPARK-61219).
    - The listener tests of the offline suite (`-k "listener or Listener or 
stop"`, 67) pass, also under pytest 9.2 for the stop tests.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Opus 5.5
    
    This pull request and its description were written by Isaac.
    
    Closes #423 from HyukjinKwon/SPARK-61219-fu-test.
    
    Authored-by: Hyukjin Kwon <[email protected]>
    Signed-off-by: Hyukjin Kwon <[email protected]>
---
 python/tests/_mock_connect_server.py     | 26 +++++++++++++++++++-
 python/tests/streaming_listener_cases.py | 35 +++++++++++++++++++++++++++
 python/tests/test_dropin_e2e.py          | 41 --------------------------------
 3 files changed, 60 insertions(+), 42 deletions(-)

diff --git a/python/tests/_mock_connect_server.py 
b/python/tests/_mock_connect_server.py
index c2f67b90..4742b15b 100644
--- a/python/tests/_mock_connect_server.py
+++ b/python/tests/_mock_connect_server.py
@@ -315,7 +315,7 @@ def serve(connection):
                 self.listener_removals = 0
                 self.listener_streams_finished = 0
                 self.listener_condition.notify_all()
-            for name in ("listener_register_gate", "listener_remove_gate"):
+            for name in ("listener_register_gate", "listener_remove_gate", 
"release_session_gate"):
                 gate = getattr(self, name, None)
                 if gate is not None:
                     gate.set()
@@ -328,6 +328,11 @@ def serve(connection):
             self.listener_remove_gate = Event()
             self.release_session_failure = False
             self.release_session_calls = 0
+            # The listener events that a release sends, as a server that stops 
the running
+            # queries of the session, and a gate that holds the release's 
response.
+            self.release_session_events = []
+            self.release_session_blocked = False
+            self.release_session_gate = Event()
 
         def analyze(self, request, context):
             # AnalyzePlanRequest.session_id=1, echoed as 
AnalyzePlanResponse.session_id=1, as the
@@ -431,6 +436,17 @@ def serve(connection):
                 # retain independent response and session-release behavior.
                 if self.release_session_unblocks_execute:
                     self.response_gate.set()
+                # ReleaseSessionRequest.session_id = 1.
+                session_id = parse_message(request).get(1, [b""])[0].decode()
+                queue = self.listener_queues.get(session_id)
+                events = list(self.release_session_events)
+                blocked = self.release_session_blocked
+                gate = self.release_session_gate
+            if queue is not None:
+                for event in events:
+                    queue.put(event)
+            if blocked and not gate.wait(15):
+                context.abort(grpc.StatusCode.INVALID_ARGUMENT, "release gate 
was not released")
             if self.release_session_failure:
                 context.abort(grpc.StatusCode.INVALID_ARGUMENT, "session 
release failed")
             return b""
@@ -773,6 +789,9 @@ def serve(connection):
                 elif command[0] == "release_listener_removal":
                     service.listener_remove_gate.set()
                     connection.send(None)
+                elif command[0] == "release_session_release":
+                    service.release_session_gate.set()
+                    connection.send(None)
         finally:
             server.stop(0).wait()
 
@@ -852,6 +871,11 @@ class RemoteService:
         assert self.connection.poll(10), "Mock server did not respond"
         self.connection.recv()
 
+    def release_session_release(self):
+        self.connection.send(("release_session_release",))
+        assert self.connection.poll(10), "Mock server did not respond"
+        self.connection.recv()
+
 
 @contextmanager
 def mock_connect_session():
diff --git a/python/tests/streaming_listener_cases.py 
b/python/tests/streaming_listener_cases.py
index 19569ddd..f7ed976f 100644
--- a/python/tests/streaming_listener_cases.py
+++ b/python/tests/streaming_listener_cases.py
@@ -618,6 +618,41 @@ class TestStreamingListenerLifecycle:
             service.release_session_failure = False
             spark.streams.close()
 
+    def 
test_streaming_listener_stop_delivers_the_events_sent_while_releasing(self, 
mock_connect):
+        # As PySpark's stop(): the session is released before the listener bus 
is cancelled, so
+        # the terminated events that the server sends while it releases the 
session, of the
+        # queries still running, reach the listeners. The mock holds the 
release's response
+        # until the listener has the event; a bus cancelled before the release 
drops it.
+        spark, service = mock_connect
+        service.listener_bus_enabled = True
+        terminated = threading.Event()
+
+        class TerminatedListener(IdleListener):
+            def onQueryTerminated(self, event):
+                terminated.set()
+
+        spark.streams.addListener(TerminatedListener())
+        assert service.wait_listener_registration(1)
+        service.release_session_events = [(2, json.dumps({
+            "id": "00000000-0000-4000-8000-000000000001",
+            "runId": "00000000-0000-4000-8000-000000000002",
+            "exception": None,
+            "errorClassOnException": None,
+        }))]
+        service.release_session_blocked = True
+        stopper = threading.Thread(target=spark.stop, daemon=True)
+        stopper.start()
+        try:
+            assert terminated.wait(timeout=15)
+        finally:
+            service.release_session_release()
+            stopper.join(timeout=15)
+            service.release_session_blocked = False
+            service.release_session_events = []
+        assert not stopper.is_alive()
+        assert spark.is_stopped
+        assert service.release_session_calls == 1
+
     def test_streaming_listener_client_close_cancels_without_releasing(self, 
mock_connect):
         spark, service = mock_connect
         service.listener_bus_enabled = True
diff --git a/python/tests/test_dropin_e2e.py b/python/tests/test_dropin_e2e.py
index bb26e818..e00e91f9 100644
--- a/python/tests/test_dropin_e2e.py
+++ b/python/tests/test_dropin_e2e.py
@@ -60,47 +60,6 @@ def session():
         session.stop()
 
 
-def 
test_a_query_running_at_stop_sends_its_terminated_event_to_the_listeners_as_pyspark(session):
-    # As PySpark's stop(): the session is released before its client closes, 
so the terminated
-    # event that the server sends for a query still running at stop reaches 
the listeners.
-    import threading
-
-    from pyspark.sql.streaming import StreamingQueryListener
-
-    started, terminated = threading.Event(), threading.Event()
-    terminated_ids = []
-
-    class Listener(StreamingQueryListener):
-        def onQueryStarted(self, event):
-            started.set()
-
-        def onQueryProgress(self, event):
-            pass
-
-        def onQueryIdle(self, event):
-            pass
-
-        def onQueryTerminated(self, event):
-            terminated_ids.append(str(event.id))
-            terminated.set()
-
-    other = session.newSession()
-    other.streams.addListener(Listener())
-    query = 
other.readStream.format("rate").load().writeStream.format("noop").start()
-    try:
-        assert started.wait(timeout=120)
-        other.stop()
-        assert terminated.wait(timeout=120)
-        assert terminated_ids == [query.id]
-    finally:
-        try:
-            query.stop()
-        except Exception:
-            # The session is stopped, so the query is no longer reachable.
-            pass
-        other.stop()
-
-
 @pytest.mark.parametrize("prefetch", [False, True])
 def 
test_to_local_iterator_raises_the_error_of_the_execution_as_pyspark(session, 
prefetch):
     # As official PySpark 4.3.0: the error of a query that fails while it is 
iterated is


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to