File: DebounceSpec.hs

package info (click to toggle)
haskell-auto-update 0.2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 144 kB
  • sloc: haskell: 677; makefile: 2
file content (226 lines) | stat: -rw-r--r-- 6,754 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
{-# LANGUAGE NumericUnderscores #-}
module Control.DebounceSpec (main, spec) where

import Control.Concurrent (
    MVar,
    newEmptyMVar,
    takeMVar,
    putMVar,
    newMVar,
    threadDelay,
    tryReadMVar,
 )
import Control.Debounce (
    DebounceSettings(..),
    leadingEdge,
    leadingMuteEdge,
    trailingEdge,
    trailingDelayEdge,
    defaultDebounceSettings,
 )
import qualified Control.Debounce.Internal as DI
import Control.Monad (void)
import Control.Monad.Catch
import Control.Retry (recovering, constantDelay, limitRetries)
import Data.IORef (IORef, readIORef, newIORef, modifyIORef)
import Data.Word (Word64)
import GHC.Clock (getMonotonicTime)
import Test.Hspec (Spec, describe, it, shouldReturn, hspec)
import Test.HUnit (assertBool)
import Test.HUnit.Lang (HUnitFailure (HUnitFailure))

spec :: Spec
spec = describe "mkDebounce" $ do
    describe "Leading edge" $ do
        it "works for a single event" $ do
            (ref, debounced, _baton, returnFromWait) <- getDebounce leadingEdge

            debounced
            waitUntil 5 $ readIORef ref `shouldReturn` 1

            returnFromWait
            pause
            readIORef ref `shouldReturn` 1

            -- Try another round
            debounced
            waitUntil 5 $ readIORef ref `shouldReturn` 2

            returnFromWait
            pause
            readIORef ref `shouldReturn` 2

        it "works for multiple events" $ do
            (ref, debounced, baton, returnFromWait) <- getDebounce leadingEdge

            debounced
            waitForBatonToBeTaken baton
            debounced
            pause
            waitUntil 5 $ readIORef ref `shouldReturn` 1

            returnFromWait
            pause
            readIORef ref `shouldReturn` 2
    describe "LeadingMute edge" $ do
        it "works for a single event" $ do
            (ref, debounced, _baton, returnFromWait) <- getDebounce leadingMuteEdge

            debounced
            waitUntil 5 $ readIORef ref `shouldReturn` 1

            returnFromWait
            pause
            readIORef ref `shouldReturn` 1

            -- Try another round
            debounced
            waitUntil 5 $ readIORef ref `shouldReturn` 2

            returnFromWait
            pause
            readIORef ref `shouldReturn` 2

        it "works for multiple events" $ do
            (ref, debounced, baton, returnFromWait) <- getDebounce leadingMuteEdge

            debounced
            waitForBatonToBeTaken baton
            debounced
            pause
            debounced
            waitUntil 5 $ readIORef ref `shouldReturn` 1
            debounced

            returnFromWait
            pause
            readIORef ref `shouldReturn` 1

    describe "Trailing edge" $ do
        it "works for a single event" $ do
            (ref, debounced, _baton, returnFromWait) <- getDebounce trailingEdge

            debounced
            pause
            readIORef ref `shouldReturn` 0

            returnFromWait
            waitUntil 5 $ readIORef ref `shouldReturn` 1

            -- Try another round
            debounced
            pause
            waitUntil 5 $ readIORef ref `shouldReturn` 1

            returnFromWait
            waitUntil 5 $ readIORef ref `shouldReturn` 2

        it "works for multiple events" $ do
            (ref, debounced, baton, returnFromWait) <- getDebounce trailingEdge

            debounced
            waitForBatonToBeTaken baton
            debounced
            pause
            readIORef ref `shouldReturn` 0

            returnFromWait
            waitUntil 5 $ readIORef ref `shouldReturn` 1

    describe "TrailingDelay edge" $ do
        it "works for a single event" $ do
            (ref, debounced, _baton, _returnFromWait) <- getDebounce' True trailingDelayEdge

            debounced
            readIORef ref `shouldReturn` 0

            waitUntil 1 $ readIORef ref `shouldReturn` 1

            -- Try another round
            debounced
            readIORef ref `shouldReturn` 1

            waitUntil 1 $ readIORef ref `shouldReturn` 2

        it "works for multiple events" $ do
            (ref, debounced, _baton, _returnFromWait) <- getDebounce' True trailingDelayEdge

            start <- getMonotonicTime

            debounced
            readIORef ref `shouldReturn` 0
            -- Asserts at end check that this timing gets added to the cooldown time
            threadDelay 500_000

            readIORef ref `shouldReturn` 0
            before2nd <- getMonotonicTime
            debounced
            readIORef ref `shouldReturn` 0
            threadDelay 500_000

            readIORef ref `shouldReturn` 0
            threadDelay 250_000

            readIORef ref `shouldReturn` 0

            waitUntil 1 $ readIORef ref `shouldReturn` 1
            end <- getMonotonicTime
            assertBool "Took less than 1 sec after retrigger" $
                end - before2nd > 1
            assertBool "Took less than 1.5 sec total" $
                end - start > 1.5

-- | Make a controllable delay function
getWaitAction :: IO (p -> IO (), IO ())
getWaitAction = do
    waitVar <- newEmptyMVar
    let waitAction _ = takeMVar waitVar
    let returnFromWait = putMVar waitVar ()
    return (waitAction, returnFromWait)

getDebounce :: DI.DebounceEdge -> IO (IORef Int, IO (), MVar (), IO ())
getDebounce = getDebounce' False

-- | Get a debounce system with access to the internals for testing
getDebounce' :: Bool -> DI.DebounceEdge -> IO (IORef Int, IO (), MVar (), IO ())
getDebounce' useThreadDelay edge = do
    ref <- newIORef 0
    let action = modifyIORef ref (+ 1)

    (waitAction, returnFromWait) <-
        if useThreadDelay
            then pure (threadDelay, pure ())
            else getWaitAction

    baton <- newMVar ()

    debounced <-
        DI.mkDebounceInternal
            baton
            waitAction
            defaultDebounceSettings
                { debounceFreq = 1_000_000 -- !!! used in 'TrailingDelay' test
                , debounceAction = action
                , debounceEdge = edge
                }

    return (ref, debounced, baton, returnFromWait)

-- | Pause briefly (100ms)
pause :: IO ()
pause = threadDelay 100_000

waitForBatonToBeTaken :: MVar () -> IO ()
waitForBatonToBeTaken baton =
    waitUntil 5 $ tryReadMVar baton `shouldReturn` Nothing

-- | Wait up to n seconds for an action to complete without throwing an HUnitFailure
waitUntil :: Int -> IO a -> IO ()
waitUntil n action =
    recovering policy [handler] (\_status -> void action)
  where
    policy = constantDelay 1000 `mappend` limitRetries (n * 1000) -- 1ms * n * 1000 tries = n seconds
    handler _status = Handler (\HUnitFailure{} -> return True)

main :: IO ()
main = hspec spec