@@ -109,20 +109,21 @@ def test_load_incluster_config(self):
109109
110110 def test_refresh_token (self ):
111111 with patch ('os.environ' , {SERVICE_HOST_ENV_NAME : 'a' , SERVICE_PORT_ENV_NAME : '1' }), \
112+ patch ('patroni.dcs.kubernetes.datetime' ) as mock_datetime , \
112113 patch ('os.path.isfile' , Mock (side_effect = [True , True , False , True , True , True ])), \
113114 patch ('builtins.open' , Mock (side_effect = [
114115 mock_open (read_data = 'cert' )(), mock_open (read_data = 'a' )(),
115116 mock_open ()(), mock_open (read_data = 'b' )(), mock_open (read_data = 'c' )()])):
116- k8s_config .load_incluster_config (token_refresh_interval = datetime .timedelta (milliseconds = 1 ))
117+ mock_datetime .datetime .now .side_effect = [datetime .datetime (1 , 1 , 1 , 0 , 0 , 0 )] * 2 + \
118+ [datetime .datetime (1 , 1 , 1 , 0 , 0 , 1 )] * 4 + [datetime .datetime (1 , 1 , 1 , 0 , 0 , 2 )] * 3
119+ k8s_config .load_incluster_config (token_refresh_interval = datetime .timedelta (milliseconds = 100 ))
117120 self .assertEqual (k8s_config .headers .get ('authorization' ), 'Bearer a' )
118- time .sleep (0.001 )
119121 # token file doesn't exist
120122 self .assertEqual (k8s_config .headers .get ('authorization' ), 'Bearer a' )
121123 # token file is empty
122124 self .assertEqual (k8s_config .headers .get ('authorization' ), 'Bearer a' )
123125 # token refreshed
124126 self .assertEqual (k8s_config .headers .get ('authorization' ), 'Bearer b' )
125- time .sleep (0.001 )
126127 # token refreshed
127128 self .assertEqual (k8s_config .headers .get ('authorization' ), 'Bearer c' )
128129 # no need to refresh token
0 commit comments