11from hazelcast import HazelcastClient
22from hazelcast .core import Address
33from tests .base import HazelcastTestCase
4- from hazelcast .config import ClientConfig , PROPERTY_HEARTBEAT_INTERVAL , PROPERTY_HEARTBEAT_TIMEOUT
4+ from hazelcast .config import ClientConfig , ClientProperties
55from tests .util import configure_logging
66
77
@@ -20,8 +20,8 @@ def setUp(self):
2020 self .member = self .rc .startMember (self .cluster .id )
2121 self .config = ClientConfig ()
2222
23- self .config ._properties [ PROPERTY_HEARTBEAT_INTERVAL ] = 500
24- self .config ._properties [ PROPERTY_HEARTBEAT_TIMEOUT ] = 2000
23+ self .config .set_property ( ClientProperties . HEARTBEAT_INTERVAL . name , 500 )
24+ self .config .set_property ( ClientProperties . HEARTBEAT_TIMEOUT . name , 2000 )
2525
2626 self .client = HazelcastClient (self .config )
2727
@@ -31,7 +31,14 @@ def tearDown(self):
3131
3232 def test_heartbeat_stopped (self ):
3333
34- self .client .cluster .add_listener (member_added = lambda m : self .client .connection_manager .get_or_connect (m .address ))
34+ def member_added_func (m ):
35+ def connection_callback (f ):
36+ conn = f .result ()
37+ self .simulate_heartbeat_lost (self .client , Address (conn ._address [0 ], conn ._address [1 ]), 2 )
38+
39+ self .client .connection_manager .get_or_connect (m .address ).add_done_callback (connection_callback )
40+
41+ self .client .cluster .add_listener (member_added = member_added_func )
3542
3643 def heartbeat_stopped_collector ():
3744 connections = []
@@ -42,32 +49,6 @@ def connection_collector(c):
4249 connection_collector .connections = connections
4350 return connection_collector
4451
45- collector = heartbeat_stopped_collector ()
46-
47- self .client .heartbeat .add_listener (on_heartbeat_stopped = collector )
48-
49- member2 = self .rc .startMember (self .cluster .id )
50- self .simulate_heartbeat_lost (self .client , Address (member2 .host , member2 .port ), 2 )
51-
52- def assert_heartbeat_stopped ():
53- self .assertEqual (1 , len (collector .connections ))
54- connection = collector .connections [0 ]
55- self .assertEqual (connection ._address , (member2 .host , member2 .port ))
56-
57- self .assertTrueEventually (assert_heartbeat_stopped )
58-
59- def test_heartbeat_restored (self ):
60-
61- def member_added_func (m ):
62-
63- def connection_callback (f ):
64- conn = f .result ()
65- self .simulate_heartbeat_lost (self .client , Address (conn ._address [0 ], conn ._address [1 ]), 2 )
66-
67- self .client .connection_manager .get_or_connect (m .address ).add_done_callback (connection_callback )
68-
69- self .client .cluster .add_listener (member_added = member_added_func )
70-
7152 def heartbeat_restored_collector ():
7253 connections = []
7354
@@ -77,18 +58,23 @@ def connection_collector(c):
7758 connection_collector .connections = connections
7859 return connection_collector
7960
80- collector = heartbeat_restored_collector ()
61+ stopped_collector = heartbeat_stopped_collector ()
62+ restored_collector = heartbeat_restored_collector ()
8163
82- self .client .heartbeat .add_listener (on_heartbeat_restored = collector )
64+ self .client .heartbeat .add_listener (on_heartbeat_stopped = stopped_collector ,
65+ on_heartbeat_restored = restored_collector )
8366
8467 member2 = self .rc .startMember (self .cluster .id )
8568
86- def assert_heartbeat_restored ():
87- self .assertEqual (1 , len (collector .connections ))
88- connection = collector .connections [0 ]
89- self .assertEqual (connection ._address , (member2 .host , member2 .port ))
69+ def assert_heartbeat_stopped_and_restored ():
70+ self .assertEqual (1 , len (stopped_collector .connections ))
71+ self .assertEqual (1 , len (restored_collector .connections ))
72+ connection_stopped = stopped_collector .connections [0 ]
73+ connection_restored = restored_collector .connections [0 ]
74+ self .assertEqual (connection_stopped ._address , (member2 .host , member2 .port ))
75+ self .assertEqual (connection_restored ._address , (member2 .host , member2 .port ))
9076
91- self .assertTrueEventually (assert_heartbeat_restored )
77+ self .assertTrueEventually (assert_heartbeat_stopped_and_restored )
9278
9379 @staticmethod
9480 def simulate_heartbeat_lost (client , address , timeout ):
0 commit comments