@@ -134,10 +134,11 @@ def test_load_module_from_file(caplog):
134134 (10 , 200 ),
135135 ],
136136)
137- def test_load_module_from_file_reload (caplog , n1 , n2 ):
137+ def test_load_module_from_file_reload_sleep (caplog , n1 , n2 ):
138138 """'load_module_from_file' should be able to reload modules that were previously loaded,
139- allowing hot changes"""
140- module_name = f"load_module_from_file_reload_{ n1 } _{ n2 } "
139+ allowing hot changes. To be able reload a file the file timestamp or size must change, or the
140+ cache must be invalidated"""
141+ module_name = f"test_load_module_from_file_reload_sleep_{ n1 } _{ n2 } "
141142 module_code = "def get_value(): return {n}"
142143
143144 module_path = loader .create_module_files (module_name , module_code .format (n = n1 ))
@@ -147,8 +148,8 @@ def test_load_module_from_file_reload(caplog, n1, n2):
147148 assert module .get_value () == n1
148149 assert_message_in_log (caplog , f"Monitor '{ module_name } ' loaded" )
149150
150- # As python checks for the timestamp to change to reload a module, sleep for 1 second
151- time .sleep (1 )
151+ # Sleep until next second
152+ time .sleep (1 - time . time () % 1 + 0.01 )
152153
153154 module_path = loader .create_module_files (module_name , module_code .format (n = n2 ))
154155
@@ -158,6 +159,31 @@ def test_load_module_from_file_reload(caplog, n1, n2):
158159 assert_message_in_log (caplog , f"Monitor '{ module_name } ' loaded" , count = 2 )
159160
160161
162+ @pytest .mark .flaky (reruns = 1 )
163+ def test_load_module_from_file_reload_no_time_change (caplog ):
164+ """'load_module_from_file' should not reload the file if the file timestamp or size didn't
165+ change. Test is marked with 'flaky' because the second might change between the module loads"""
166+ module_name = "test_load_module_from_file_reload_no_time_change"
167+ module_code = "def get_value(): return {n}"
168+
169+ module_path = loader .create_module_files (module_name , module_code .format (n = 10 ))
170+
171+ module = loader .load_module_from_file (module_path )
172+
173+ assert module .get_value () == 10
174+ assert_message_in_log (caplog , f"Monitor '{ module_name } ' loaded" )
175+
176+ time .sleep (0.1 )
177+
178+ module_path = loader .create_module_files (module_name , module_code .format (n = 99 ))
179+
180+ module = loader .load_module_from_file (module_path )
181+
182+ # The value didn't change, even though the module code changed
183+ assert module .get_value () == 10
184+ assert_message_in_log (caplog , f"Monitor '{ module_name } ' loaded" , count = 2 )
185+
186+
161187def test_load_module_from_file_reload_replace_variables ():
162188 """'load_module_from_file' should be able to reload modules replacing the previous state for a
163189 new one"""
0 commit comments