@@ -42,3 +42,110 @@ type ``Cmd tests``() =
4242 Assert.AreEqual( 2 , messageCount)
4343 Assert.AreEqual( Some( NewValue 5 ), actualValue)
4444 }
45+
46+ [<Test>]
47+ member _. ``Cmd.throttle issues message at specified intervals`` () =
48+ async {
49+ let mutable messageCount = 0
50+ let mutable actualValue = None
51+
52+ let dispatch msg =
53+ messageCount <- messageCount + 1
54+ actualValue <- Some msg
55+
56+ let throttleCmd = Cmd.throttle 100 NewValue
57+
58+ throttleCmd 1 |> CmdTestsHelper.execute dispatch
59+ do ! Async.Sleep 50
60+ throttleCmd 2 |> CmdTestsHelper.execute dispatch
61+ do ! Async.Sleep 75
62+ throttleCmd 3 |> CmdTestsHelper.execute dispatch
63+ do ! Async.Sleep 125
64+
65+ Assert.AreEqual( 2 , messageCount)
66+ Assert.AreEqual( Some( NewValue 3 ), actualValue)
67+
68+ throttleCmd 4 |> CmdTestsHelper.execute dispatch
69+ do ! Async.Sleep 75
70+ throttleCmd 5 |> CmdTestsHelper.execute dispatch
71+ do ! Async.Sleep 125
72+
73+ Assert.AreEqual( 3 , messageCount)
74+ Assert.AreEqual( Some( NewValue 4 ), actualValue)
75+ }
76+
77+ [<Test>]
78+ member _. ``Cmd.throttle issues only one message per interval`` () =
79+ async {
80+ let mutable messageCount = 0
81+ let mutable actualValue = None
82+
83+ let dispatch msg =
84+ messageCount <- messageCount + 1
85+ actualValue <- Some msg
86+
87+ let throttleCmd = Cmd.throttle 100 NewValue
88+
89+ throttleCmd 1 |> CmdTestsHelper.execute dispatch
90+ do ! Async.Sleep 20
91+ throttleCmd 2 |> CmdTestsHelper.execute dispatch
92+ do ! Async.Sleep 35
93+ throttleCmd 3 |> CmdTestsHelper.execute dispatch
94+ do ! Async.Sleep 125
95+
96+ // Only the first message should have been dispatched
97+ Assert.AreEqual( 1 , messageCount)
98+ Assert.AreEqual( Some( NewValue 1 ), actualValue)
99+ }
100+
101+ [<Test>]
102+ member _. ``Cmd.bufferedThrottle dispatches the first and most recent message within the specified interval`` () =
103+ async {
104+ let mutable messageCount = 0
105+ let mutable actualValue = None
106+
107+ let dispatch msg =
108+ messageCount <- messageCount + 1
109+ actualValue <- Some msg
110+
111+ let throttleCmd = Cmd.bufferedThrottle 100 NewValue
112+
113+ throttleCmd 1 |> CmdTestsHelper.execute dispatch
114+ do ! Async.Sleep 20
115+ throttleCmd 2 |> CmdTestsHelper.execute dispatch
116+ do ! Async.Sleep 10
117+ throttleCmd 3 |> CmdTestsHelper.execute dispatch
118+ do ! Async.Sleep 20
119+ throttleCmd 4 |> CmdTestsHelper.execute dispatch
120+ do ! Async.Sleep 125
121+
122+ // Only the first and most recent message should be dispatched
123+ Assert.AreEqual( 2 , messageCount)
124+ Assert.AreEqual( Some( NewValue 4 ), actualValue)
125+ }
126+
127+ [<Test>]
128+ member _. ``Cmd.bufferedThrottle dispatches the most recent message even if delayed`` () =
129+ async {
130+ let mutable actualValue = None
131+ let mutable messageCount = 0
132+
133+ let dispatch msg =
134+ messageCount <- messageCount + 1
135+ actualValue <- Some msg
136+
137+ let throttleCmd = Cmd.bufferedThrottle 100 NewValue
138+
139+ throttleCmd 1 |> CmdTestsHelper.execute dispatch
140+ throttleCmd 2 |> CmdTestsHelper.execute dispatch
141+
142+ // Only the first message should have been dispatched
143+ Assert.AreEqual( 1 , messageCount)
144+ Assert.AreEqual( Some( NewValue 1 ), actualValue)
145+
146+ do ! Async.Sleep 200 // Wait longer than the throttle interval
147+
148+ // the second message should have been dispatched delayed
149+ Assert.AreEqual( 2 , messageCount)
150+ Assert.AreEqual( Some( NewValue 2 ), actualValue)
151+ }
0 commit comments