77from slack_sdk .signature import SignatureVerifier
88from slack_sdk .web import WebClient
99
10+ import slack_bolt .listener .thread_runner as runner_module
1011from slack_bolt .app import App
1112from slack_bolt .request import BoltRequest
1213from tests .mock_web_api_server import (
@@ -53,9 +54,9 @@ def build_request_from_body(self, message_body: dict) -> BoltRequest:
5354 timestamp , body = str (int (time .time ())), json .dumps (message_body )
5455 return BoltRequest (body = body , headers = self .build_headers (timestamp , body ))
5556
56- def setup_time_mocks (self , * , monkeypatch : pytest .MonkeyPatch , time_mock : Mock , sleep_mock : Mock ):
57- monkeypatch .setattr (time , "time" , time_mock )
58- monkeypatch .setattr (time , "sleep" , sleep_mock )
57+ def setup_time_mocks (self , * , monkeypatch : pytest .MonkeyPatch , time_mock , sleep_mock ):
58+ monkeypatch .setattr (runner_module . time , "time" , time_mock )
59+ monkeypatch .setattr (runner_module . time , "sleep" , sleep_mock )
5960
6061 def test_valid_callback_id_success (self ):
6162 app = App (
@@ -138,10 +139,20 @@ def test_auto_acknowledge_false_without_acknowledging(self, caplog, monkeypatch)
138139 app .function ("reverse" , auto_acknowledge = False )(just_no_ack )
139140
140141 request = self .build_request_from_body (function_body )
142+
143+ elapsed_seconds = 0
144+
145+ def fake_time ():
146+ return float (elapsed_seconds )
147+
148+ def fake_sleep (duration ):
149+ nonlocal elapsed_seconds
150+ elapsed_seconds += 1
151+
141152 self .setup_time_mocks (
142153 monkeypatch = monkeypatch ,
143- time_mock = Mock ( side_effect = [ current_time for current_time in range ( 100 )]) ,
144- sleep_mock = Mock (),
154+ time_mock = fake_time ,
155+ sleep_mock = Mock (side_effect = fake_sleep ),
145156 )
146157 response = app .dispatch (request )
147158
@@ -158,20 +169,29 @@ def test_function_handler_timeout(self, monkeypatch):
158169 app .function ("reverse" , auto_acknowledge = False , ack_timeout = timeout )(just_no_ack )
159170 request = self .build_request_from_body (function_body )
160171
161- sleep_mock = Mock ()
172+ elapsed_seconds = 0
173+
174+ def fake_time ():
175+ return float (elapsed_seconds )
176+
177+ def fake_sleep (duration ):
178+ nonlocal elapsed_seconds
179+ elapsed_seconds += 1
180+
162181 self .setup_time_mocks (
163182 monkeypatch = monkeypatch ,
164- time_mock = Mock ( side_effect = [ current_time for current_time in range ( 100 )]) ,
165- sleep_mock = sleep_mock ,
183+ time_mock = fake_time ,
184+ sleep_mock = Mock ( side_effect = fake_sleep ) ,
166185 )
167186
168187 response = app .dispatch (request )
169188
170189 assert response .status == 404
171190 assert_auth_test_count (self , 1 )
172- assert (
173- sleep_mock .call_count == timeout
174- ), f"Expected handler to time out after calling time.sleep 5 times, but it was called { sleep_mock .call_count } times"
191+ assert elapsed_seconds == timeout + 1 , (
192+ f"Expected handler to time out after { timeout + 1 } time.sleep calls, "
193+ f"but it was called { elapsed_seconds } times"
194+ )
175195
176196 def test_warning_when_timeout_improperly_set (self , caplog ):
177197 app = App (
0 commit comments