22# Licensed under the Apache V2, see LICENCE file for details.
33from __future__ import annotations
44
5- from freezegun import freeze_time
6-
7- from juju .model ._idle import CheckStatus , loop
5+ from typing import AbstractSet , Iterable
86
7+ from freezegun import freeze_time
98
10- async def alist (agen ):
11- return [v async for v in agen ]
9+ from juju .model ._idle import CheckStatus , Loop
10+
11+
12+ def unroll (
13+ statuses : Iterable [CheckStatus | None ],
14+ * ,
15+ apps : AbstractSet [str ],
16+ wait_for_exact_units : int | None = None ,
17+ wait_for_units : int ,
18+ idle_period : float ,
19+ ) -> list [bool ]:
20+ loop = Loop (
21+ apps = apps ,
22+ wait_for_exact_units = wait_for_exact_units ,
23+ wait_for_units = wait_for_units ,
24+ idle_period = idle_period ,
25+ )
26+ return [loop .next (s ) for s in statuses ]
1227
1328
14- async def test_wait_for_apps ():
15- async def checks ():
29+ def test_wait_for_apps ():
30+ def checks ():
1631 yield None
1732 yield None
1833
19- assert await alist (
20- loop (
21- checks (),
22- apps = {"a" },
23- wait_for_units = 0 ,
24- idle_period = 0 ,
25- )
34+ assert unroll (
35+ checks (),
36+ apps = {"a" },
37+ wait_for_units = 0 ,
38+ idle_period = 0 ,
2639 ) == [False , False ]
2740
2841
29- async def test_at_least_units ():
30- async def checks ():
42+ def test_at_least_units ():
43+ def checks ():
3144 yield CheckStatus ({"u/0" , "u/1" , "u/2" }, {"u/0" }, {"u/0" , "u/1" , "u/2" })
3245 yield CheckStatus ({"u/0" , "u/1" , "u/2" }, {"u/0" , "u/1" }, {"u/0" , "u/1" , "u/2" })
3346 yield CheckStatus (
3447 {"u/0" , "u/1" , "u/2" }, {"u/0" , "u/1" , "u/2" }, {"u/0" , "u/1" , "u/2" }
3548 )
3649
3750 with freeze_time ():
38- assert await alist (
39- loop (
40- checks (),
41- apps = {"u" },
42- wait_for_units = 2 ,
43- idle_period = 0 ,
44- )
51+ assert unroll (
52+ checks (),
53+ apps = {"u" },
54+ wait_for_units = 2 ,
55+ idle_period = 0 ,
4556 ) == [False , True , True ]
4657
4758
48- async def test_for_exact_units ():
59+ def test_for_exact_units ():
4960 good = CheckStatus (
5061 {"u/0" , "u/1" , "u/2" },
5162 {"u/1" , "u/2" },
@@ -62,55 +73,49 @@ async def test_for_exact_units():
6273 {"u/0" , "u/1" , "u/2" },
6374 )
6475
65- async def checks ():
76+ def checks ():
6677 yield too_few
6778 yield good
6879 yield too_many
6980 yield good
7081
71- assert await alist (
72- loop (
73- checks (),
74- apps = {"u" },
75- wait_for_units = 1 ,
76- wait_for_exact_units = 2 ,
77- idle_period = 0 ,
78- )
82+ assert unroll (
83+ checks (),
84+ apps = {"u" },
85+ wait_for_units = 1 ,
86+ wait_for_exact_units = 2 ,
87+ idle_period = 0 ,
7988 ) == [False , True , False , True ]
8089
8190
82- async def test_idle_ping_pong ():
91+ def test_idle_ping_pong ():
8392 good = CheckStatus ({"hexanator/0" }, {"hexanator/0" }, {"hexanator/0" })
8493 bad = CheckStatus ({"hexanator/0" }, {"hexanator/0" }, set ())
8594
86- async def checks ():
95+ def checks ():
8796 with freeze_time () as clock :
8897 for status in [good , bad , good , bad ]:
8998 yield status
9099 clock .tick (10 )
91100
92- assert await alist (
93- loop (
94- checks (),
95- apps = {"hexanator" },
96- wait_for_units = 1 ,
97- idle_period = 15 ,
98- )
101+ assert unroll (
102+ checks (),
103+ apps = {"hexanator" },
104+ wait_for_units = 1 ,
105+ idle_period = 15 ,
99106 ) == [False , False , False , False ]
100107
101108
102- async def test_idle_period ():
103- async def checks ():
109+ def test_idle_period ():
110+ def checks ():
104111 with freeze_time () as clock :
105112 for _ in range (4 ):
106113 yield CheckStatus ({"hexanator/0" }, {"hexanator/0" }, {"hexanator/0" })
107114 clock .tick (10 )
108115
109- assert await alist (
110- loop (
111- checks (),
112- apps = {"hexanator" },
113- wait_for_units = 1 ,
114- idle_period = 15 ,
115- )
116+ assert unroll (
117+ checks (),
118+ apps = {"hexanator" },
119+ wait_for_units = 1 ,
120+ idle_period = 15 ,
116121 ) == [False , False , True , True ]
0 commit comments