@@ -2393,6 +2393,77 @@ def test_execution_progress_handler(
23932393 )
23942394 self .stopSession (mock_session_controller_client_instance , session )
23952395
2396+ @mock .patch ("time.sleep" , return_value = None )
2397+ @mock .patch ("google.cloud.dataproc_v1.SessionControllerClient" )
2398+ def test_wait_for_session_available_success (
2399+ self , mock_session_controller_client , mock_sleep
2400+ ):
2401+ """Test that the method waits and returns the session when the endpoint appears."""
2402+ mock_client = mock_session_controller_client .return_value
2403+ session_name = (
2404+ "projects/test-project/locations/test-region/sessions/test-session"
2405+ )
2406+
2407+ # Session without the endpoint
2408+ session_pending = Session ()
2409+ session_pending .name = session_name
2410+
2411+ # Session with the endpoint
2412+ session_ready = Session ()
2413+ session_ready .name = session_name
2414+ session_ready .runtime_info .endpoints ["Spark Connect Server" ] = (
2415+ "sc://example.com:443"
2416+ )
2417+
2418+ # Mock get_session to return pending, then ready
2419+ mock_client .get_session .side_effect = [
2420+ session_pending ,
2421+ session_pending ,
2422+ session_ready ,
2423+ ]
2424+
2425+ builder = DataprocSparkSession .Builder ()
2426+ builder ._session_controller_client = (
2427+ mock_client # Inject the mock client
2428+ )
2429+
2430+ result = builder ._wait_for_session_available (session_name , timeout = 10 )
2431+
2432+ self .assertEqual (result , session_ready )
2433+ self .assertEqual (mock_client .get_session .call_count , 3 )
2434+ self .assertEqual (mock_sleep .call_count , 2 )
2435+
2436+ @mock .patch ("time.sleep" , return_value = None )
2437+ @mock .patch ("google.cloud.dataproc_v1.SessionControllerClient" )
2438+ def test_wait_for_session_available_timeout (
2439+ self , mock_session_controller_client , mock_sleep
2440+ ):
2441+ """Test that the method raises RuntimeError on timeout."""
2442+ mock_client = mock_session_controller_client .return_value
2443+ session_name = (
2444+ "projects/test-project/locations/test-region/sessions/test-session"
2445+ )
2446+
2447+ # Session that never gets the endpoint
2448+ session_pending = Session ()
2449+ session_pending .name = session_name
2450+
2451+ mock_client .get_session .return_value = session_pending
2452+
2453+ builder = DataprocSparkSession .Builder ()
2454+ builder ._session_controller_client = (
2455+ mock_client # Inject the mock client
2456+ )
2457+
2458+ with self .assertRaises (RuntimeError ) as context :
2459+ # Use a short timeout for the test
2460+ builder ._wait_for_session_available (session_name , timeout = 1 )
2461+
2462+ self .assertIn (
2463+ f"Spark Connect endpoint not available for session { session_name } " ,
2464+ str (context .exception ),
2465+ )
2466+
23962467
23972468class SessionIdValidationTests (unittest .TestCase ):
23982469 """Test cases for session ID validation and custom session ID functionality."""
0 commit comments