1+ """
2+ Test for __slots__ implementation in connection module classes.
3+ This test ensures that memory optimization via __slots__ is working correctly.
4+ """
5+ import unittest
6+ from cassandra .connection import (
7+ EndPoint , DefaultEndPoint , SniEndPoint , UnixSocketEndPoint ,
8+ _Frame , ContinuousPagingSession , ShardawarePortGenerator ,
9+ _ConnectionIOBuffer , ResponseWaiter , HeartbeatFuture , Timer , TimerManager
10+ )
11+
12+
13+ class SlotsImplementationTest (unittest .TestCase ):
14+ """Test that targeted classes have __slots__ and prevent dynamic attributes."""
15+
16+ def test_endpoint_classes_have_slots (self ):
17+ """Test EndPoint and its subclasses have __slots__ implemented."""
18+ # EndPoint base class should have empty slots
19+ self .assertEqual (EndPoint .__slots__ , ())
20+
21+ # Test DefaultEndPoint
22+ ep = DefaultEndPoint ('127.0.0.1' , 9042 )
23+ self .assertFalse (hasattr (ep , '__dict__' ))
24+ with self .assertRaises (AttributeError ):
25+ ep .dynamic_attr = 'test'
26+
27+ # Test SniEndPoint
28+ sni_ep = SniEndPoint ('proxy.example.com' , 'server.example.com' , 9042 )
29+ self .assertFalse (hasattr (sni_ep , '__dict__' ))
30+ with self .assertRaises (AttributeError ):
31+ sni_ep .dynamic_attr = 'test'
32+
33+ # Test UnixSocketEndPoint
34+ unix_ep = UnixSocketEndPoint ('/tmp/cassandra.sock' )
35+ self .assertFalse (hasattr (unix_ep , '__dict__' ))
36+ with self .assertRaises (AttributeError ):
37+ unix_ep .dynamic_attr = 'test'
38+
39+ def test_frame_class_has_slots (self ):
40+ """Test _Frame class has __slots__ implemented."""
41+ frame = _Frame (4 , 0 , 1 , 7 , 9 , 100 )
42+ self .assertFalse (hasattr (frame , '__dict__' ))
43+ with self .assertRaises (AttributeError ):
44+ frame .dynamic_attr = 'test'
45+
46+ # Test that all expected attributes are accessible
47+ self .assertEqual (frame .version , 4 )
48+ self .assertEqual (frame .flags , 0 )
49+ self .assertEqual (frame .stream , 1 )
50+ self .assertEqual (frame .opcode , 7 )
51+ self .assertEqual (frame .body_offset , 9 )
52+ self .assertEqual (frame .end_pos , 100 )
53+
54+ def test_timer_classes_have_slots (self ):
55+ """Test Timer and TimerManager classes have __slots__ implemented."""
56+ # Test Timer
57+ timer = Timer (5.0 , lambda : None )
58+ self .assertFalse (hasattr (timer , '__dict__' ))
59+ with self .assertRaises (AttributeError ):
60+ timer .dynamic_attr = 'test'
61+
62+ # Test Timer attributes
63+ self .assertEqual (timer .canceled , False )
64+ self .assertIsNotNone (timer .end )
65+ self .assertIsNotNone (timer .callback )
66+
67+ # Test TimerManager
68+ timer_mgr = TimerManager ()
69+ self .assertFalse (hasattr (timer_mgr , '__dict__' ))
70+ with self .assertRaises (AttributeError ):
71+ timer_mgr .dynamic_attr = 'test'
72+
73+ def test_utility_classes_have_slots (self ):
74+ """Test utility classes have __slots__ implemented."""
75+ # Test ShardawarePortGenerator
76+ self .assertEqual (ShardawarePortGenerator .__slots__ , ())
77+
78+ # Test _ConnectionIOBuffer
79+ class MockConnection :
80+ pass
81+
82+ io_buffer = _ConnectionIOBuffer (MockConnection ())
83+ self .assertFalse (hasattr (io_buffer , '__dict__' ))
84+ with self .assertRaises (AttributeError ):
85+ io_buffer .dynamic_attr = 'test'
86+
87+ # Test ResponseWaiter
88+ response_waiter = ResponseWaiter (MockConnection (), 2 , True )
89+ self .assertFalse (hasattr (response_waiter , '__dict__' ))
90+ with self .assertRaises (AttributeError ):
91+ response_waiter .dynamic_attr = 'test'
92+
93+ def test_slots_prevent_memory_overhead (self ):
94+ """Test that objects with __slots__ don't have __dict__ overhead."""
95+ instances = [
96+ DefaultEndPoint ('127.0.0.1' , 9042 ),
97+ SniEndPoint ('proxy.example.com' , 'server.example.com' , 9042 ),
98+ UnixSocketEndPoint ('/tmp/cassandra.sock' ),
99+ _Frame (4 , 0 , 1 , 7 , 9 , 100 ),
100+ Timer (5.0 , lambda : None ),
101+ TimerManager (),
102+ ]
103+
104+ for instance in instances :
105+ with self .subTest (instance = instance .__class__ .__name__ ):
106+ # Ensure no __dict__ is present (memory optimization)
107+ self .assertFalse (hasattr (instance , '__dict__' ))
108+ # Ensure __slots__ is defined
109+ self .assertTrue (hasattr (instance .__class__ , '__slots__' ))
110+
111+
112+ if __name__ == '__main__' :
113+ unittest .main ()
0 commit comments