-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract tracers used in tests into Test.Util.Tracer
- Loading branch information
Showing
4 changed files
with
34 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Test.Util.Tracer | ||
( recordingTracerIORef | ||
, recordingTracerTVar | ||
) where | ||
|
||
import Data.IORef | ||
|
||
import Control.Monad.Class.MonadSTM | ||
|
||
import Control.Tracer | ||
|
||
|
||
-- | Create a 'Tracer' that stores all events in an 'IORef' that is atomically | ||
-- updated. The second return value lets you obtain the events recorded so far | ||
-- (from oldest to newest). Obtaining the events does not erase them. | ||
recordingTracerIORef :: IO (Tracer IO ev, IO [ev]) | ||
recordingTracerIORef = newIORef [] >>= \ref -> return | ||
( Tracer $ \ev -> atomicModifyIORef' ref $ \evs -> (ev:evs, ()) | ||
, reverse <$> readIORef ref | ||
) | ||
|
||
-- | Create a 'Tracer' that stores all events in a 'TVar' that is atomically | ||
-- updated. The second return value lets you obtain the events recorded so far | ||
-- (from oldest to newest). Obtaining the events does not erase them. | ||
recordingTracerTVar :: MonadSTM m => m (Tracer m ev, m [ev]) | ||
recordingTracerTVar = newTVarM [] >>= \ref -> return | ||
( Tracer $ \ev -> atomically $ modifyTVar' ref (ev:) | ||
, atomically $ reverse <$> readTVar ref | ||
) |