@@ -262,3 +262,50 @@ async def test_with_posargs(self, mock_connect, mock_connect_model, _):
262262 macaroons = 'macaroons' ,
263263 loop = 'loop' ,
264264 max_frame_size = 'max_frame_size' )
265+
266+
267+ class TestModelWaitForIdle (asynctest .TestCase ):
268+ async def test_no_args (self ):
269+ m = Model ()
270+ with self .assertWarns (DeprecationWarning ):
271+ # no apps so should return right away
272+ await m .wait_for_idle (wait_for_active = True )
273+
274+ async def test_timeout (self ):
275+ m = Model ()
276+ import asyncio
277+ with self .assertRaises (asyncio .TimeoutError ) as cm :
278+ # no apps so should timeout after timeout period
279+ await m .wait_for_idle (apps = ["nonexisting_app" ], timeout = 1 , idle_period = 0 )
280+ self .assertEqual (str (cm .exception ), "Timed out waiting for model:\n nonexisting_app (missing)" )
281+
282+ async def test_wait_for_active_status (self ):
283+ # create a custom apps mock
284+ from types import SimpleNamespace
285+ apps = {"dummy_app" : SimpleNamespace (
286+ status = "active" ,
287+ units = [SimpleNamespace (
288+ name = "mockunit/0" ,
289+ workload_status = "active" ,
290+ workload_status_message = "workload_status_message" ,
291+ machine = None ,
292+ agent_status = "idle" ,
293+ )],
294+ )}
295+
296+ from unittest .mock import patch , PropertyMock
297+ with patch .object (Model , 'applications' , new_callable = PropertyMock ) as mock_apps :
298+ mock_apps .return_value = apps
299+ m = Model ()
300+
301+ # pass "active" via `wait_for_status` (str)
302+ await m .wait_for_idle (apps = ["dummy_app" ], wait_for_status = "active" , timeout = 5 , idle_period = 0 )
303+
304+ # pass "active" via `wait_for_active` (bool; deprecated)
305+ await m .wait_for_idle (apps = ["dummy_app" ], wait_for_active = True , timeout = 5 , idle_period = 0 )
306+
307+ # use both `wait_for_status` and `wait_for_active` - `wait_for_active` takes precedence
308+ await m .wait_for_idle (apps = ["dummy_app" ], wait_for_active = True , wait_for_status = "doesn't matter" , timeout = 5 ,
309+ idle_period = 0 )
310+
311+ mock_apps .assert_called ()
0 commit comments