From e4bcc59790b4c1650a891c6a4c25e528689c44ac Mon Sep 17 00:00:00 2001 From: Daniel Schüssler <933504+DanielSchuessler@users.noreply.github.com> Date: Fri, 31 Aug 2018 22:38:03 +0200 Subject: Alsa plugin: Cancel reader thread (to terminate alsactl) when plugin main thread exits. --- test/Plugins/Monitors/AlsaSpec.hs | 116 +++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 58 deletions(-) (limited to 'test/Plugins') diff --git a/test/Plugins/Monitors/AlsaSpec.hs b/test/Plugins/Monitors/AlsaSpec.hs index a813c1d..77845e0 100644 --- a/test/Plugins/Monitors/AlsaSpec.hs +++ b/test/Plugins/Monitors/AlsaSpec.hs @@ -57,83 +57,83 @@ runFakeAlsactlTest = waiterTaskIsRunning <- newEmptyMVar :: IO (MVar ()) waiterTaskIsWaiting <- newEmptyMVar :: IO (MVar ()) - waitFunc <- getMonitorWaiter fifoPath (Just fakeAlsactlPath) + withMonitorWaiter fifoPath (Just fakeAlsactlPath) $ \waitFunc -> do - let addToTimeline e = modifyMVar_ timeline (pure . (e :)) + let addToTimeline e = modifyMVar_ timeline (pure . (e :)) - emitEvent = do - addToTimeline EventEmitted - hPutStrLn fifo "#17 (2,0,0,Master Playback Volume,0) VALUE" - hFlush fifo + emitEvent = do + addToTimeline EventEmitted + hPutStrLn fifo "#17 (2,0,0,Master Playback Volume,0) VALUE" + hFlush fifo - putNow mv val = do - ok <- tryPutMVar mv val - unless ok $ expectationFailure "Expected the MVar to be empty" + putNow mv val = do + ok <- tryPutMVar mv val + unless ok $ expectationFailure "Expected the MVar to be empty" - simulateRunVolumeCompleted = putNow runVolumeCompleted False + simulateRunVolumeCompleted = putNow runVolumeCompleted False - quitWaiter = putNow runVolumeCompleted True + quitWaiter = putNow runVolumeCompleted True - waiterTaskMain = do - addToTimeline RunVolume - putNow waiterTaskIsRunning () - q <- takeMVar runVolumeCompleted - unless q $ do - addToTimeline Waiting - putNow waiterTaskIsWaiting () - waitFunc + waiterTaskMain = do + addToTimeline RunVolume + putNow waiterTaskIsRunning () + q <- takeMVar runVolumeCompleted + unless q $ do + addToTimeline Waiting + putNow waiterTaskIsWaiting () + waitFunc - waiterTaskMain + waiterTaskMain - delay_ms = threadDelay . (* 1000) + delay_ms = threadDelay . (* 1000) - withAsync waiterTaskMain $ \waiterTask -> do + withAsync waiterTaskMain $ \waiterTask -> do - takeMVar waiterTaskIsRunning - simulateRunVolumeCompleted - takeMVar waiterTaskIsWaiting - takeMVar waiterTaskIsRunning -- Waiter returns instantly once - simulateRunVolumeCompleted - takeMVar waiterTaskIsWaiting - - emitEvent - takeMVar waiterTaskIsRunning - simulateRunVolumeCompleted - takeMVar waiterTaskIsWaiting - - let iters = 3 - burstSize = 5 - - replicateM_ iters $ do - emitEvent takeMVar waiterTaskIsRunning - -- Now more events start to accumulate during runVolume - replicateM_ burstSize emitEvent - delay_ms 250 -- Give the events time to go through the pipe simulateRunVolumeCompleted - -- runVolume completed and should run once more due to - -- accumulated events takeMVar waiterTaskIsWaiting + takeMVar waiterTaskIsRunning -- Waiter returns instantly once + simulateRunVolumeCompleted + takeMVar waiterTaskIsWaiting + + emitEvent takeMVar waiterTaskIsRunning simulateRunVolumeCompleted takeMVar waiterTaskIsWaiting - emitEvent - takeMVar waiterTaskIsRunning - quitWaiter + let iters = 3 + burstSize = 5 + + replicateM_ iters $ do + emitEvent + takeMVar waiterTaskIsRunning + -- Now more events start to accumulate during runVolume + replicateM_ burstSize emitEvent + delay_ms 250 -- Give the events time to go through the pipe + simulateRunVolumeCompleted + -- runVolume completed and should run once more due to + -- accumulated events + takeMVar waiterTaskIsWaiting + takeMVar waiterTaskIsRunning + simulateRunVolumeCompleted + takeMVar waiterTaskIsWaiting + + emitEvent + takeMVar waiterTaskIsRunning + quitWaiter - wait waiterTask + wait waiterTask - timelineValue <- reverse <$> readMVar timeline + timelineValue <- reverse <$> readMVar timeline - timelineValue `shouldBe` - [RunVolume, Waiting, RunVolume, Waiting, EventEmitted, RunVolume, Waiting] - ++ concat - (replicate iters $ - [EventEmitted, RunVolume] - ++ replicate burstSize EventEmitted - ++ [Waiting, RunVolume, Waiting]) - ++ [EventEmitted, RunVolume] + timelineValue `shouldBe` + [RunVolume, Waiting, RunVolume, Waiting, EventEmitted, RunVolume, Waiting] + ++ concat + (replicate iters $ + [EventEmitted, RunVolume] + ++ replicate burstSize EventEmitted + ++ [Waiting, RunVolume, Waiting]) + ++ [EventEmitted, RunVolume] data TimelineEntry = EventEmitted | Waiting | RunVolume deriving(Eq) @@ -154,4 +154,4 @@ withFifoWriteHandle fifoPath body = do (proc "bash" ["-c", "cat >> \"$0\"", fifoPath]) {std_in = CreatePipe} $ \(Just h) _ _ _ -> do hSetBuffering h LineBuffering - body h \ No newline at end of file + body h -- cgit v1.2.3