@@ -500,3 +500,107 @@ def test_can_connect_with_sslcontext_default_context(self):
500500 """
501501 ssl_context = ssl .create_default_context (cafile = CLIENT_CA_CERTS )
502502 validate_ssl_options (ssl_context = ssl_context )
503+
504+ @unittest .skipIf (USES_PYOPENSSL , "This test is for the built-in ssl.Context" )
505+ def test_tls_session_cache_enabled_by_default (self ):
506+ """
507+ Test that TLS session caching is enabled by default when SSL is configured.
508+
509+ @since 3.30.0
510+ @expected_result TLS session cache is created and configured
511+ @test_category connection:ssl
512+ """
513+ ssl_context = ssl .create_default_context (cafile = CLIENT_CA_CERTS )
514+ cluster = TestCluster (
515+ contact_points = [DefaultEndPoint ('127.0.0.1' )],
516+ ssl_context = ssl_context
517+ )
518+
519+ # Verify session cache was created
520+ self .assertIsNotNone (cluster ._tls_session_cache )
521+ self .assertEqual (cluster .tls_session_cache_enabled , True )
522+ self .assertEqual (cluster .tls_session_cache_size , 100 )
523+ self .assertEqual (cluster .tls_session_cache_ttl , 3600 )
524+
525+ cluster .shutdown ()
526+
527+ @unittest .skipIf (USES_PYOPENSSL , "This test is for the built-in ssl.Context" )
528+ def test_tls_session_cache_can_be_disabled (self ):
529+ """
530+ Test that TLS session caching can be disabled.
531+
532+ @since 3.30.0
533+ @expected_result TLS session cache is not created when disabled
534+ @test_category connection:ssl
535+ """
536+ ssl_context = ssl .create_default_context (cafile = CLIENT_CA_CERTS )
537+ cluster = TestCluster (
538+ contact_points = [DefaultEndPoint ('127.0.0.1' )],
539+ ssl_context = ssl_context ,
540+ tls_session_cache_enabled = False
541+ )
542+
543+ # Verify session cache was not created
544+ self .assertIsNone (cluster ._tls_session_cache )
545+ self .assertEqual (cluster .tls_session_cache_enabled , False )
546+
547+ cluster .shutdown ()
548+
549+ @unittest .skipIf (USES_PYOPENSSL , "This test is for the built-in ssl.Context" )
550+ def test_tls_session_reuse (self ):
551+ """
552+ Test that TLS sessions are reused across multiple connections to the same endpoint.
553+
554+ @since 3.30.0
555+ @expected_result Sessions are cached and reused, reducing handshake overhead
556+ @test_category connection:ssl
557+ """
558+ ssl_context = ssl .create_default_context (cafile = CLIENT_CA_CERTS )
559+ cluster = TestCluster (
560+ contact_points = [DefaultEndPoint ('127.0.0.1' )],
561+ ssl_context = ssl_context
562+ )
563+
564+ try :
565+ session = cluster .connect (wait_for_all_pools = True )
566+
567+ # Verify session cache was populated
568+ self .assertIsNotNone (cluster ._tls_session_cache )
569+ initial_cache_size = cluster ._tls_session_cache .size ()
570+ self .assertGreater (initial_cache_size , 0 , "Session cache should contain sessions after connection" )
571+
572+ # Execute a simple query
573+ result = session .execute ("SELECT * FROM system.local WHERE key='local'" )
574+ self .assertIsNotNone (result )
575+
576+ # Get a connection from the pool to check session_reused flag
577+ # Note: We can't easily check the exact connection that was reused,
578+ # but we can verify the cache has sessions
579+ cache_size = cluster ._tls_session_cache .size ()
580+ self .assertGreater (cache_size , 0 , "Session cache should contain sessions" )
581+
582+ finally :
583+ cluster .shutdown ()
584+
585+ @unittest .skipIf (USES_PYOPENSSL , "This test is for the built-in ssl.Context" )
586+ def test_tls_session_cache_configuration (self ):
587+ """
588+ Test that TLS session cache can be configured with custom parameters.
589+
590+ @since 3.30.0
591+ @expected_result Custom cache configuration is applied
592+ @test_category connection:ssl
593+ """
594+ ssl_context = ssl .create_default_context (cafile = CLIENT_CA_CERTS )
595+ cluster = TestCluster (
596+ contact_points = [DefaultEndPoint ('127.0.0.1' )],
597+ ssl_context = ssl_context ,
598+ tls_session_cache_size = 50 ,
599+ tls_session_cache_ttl = 1800
600+ )
601+
602+ self .assertIsNotNone (cluster ._tls_session_cache )
603+ self .assertEqual (cluster .tls_session_cache_size , 50 )
604+ self .assertEqual (cluster .tls_session_cache_ttl , 1800 )
605+
606+ cluster .shutdown ()
0 commit comments