Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use MVars to implement recording of mocked requests for nri-http #128

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions nri-http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.5.0.3

- Use an `MVar` instead of `IORef` for mocked requests to hopefully reduce flake in cases where concurrent (mocked) requests are being made

# 0.5.0.2

- Loosen types for `Http.Mock` helpers to allow for arbitrary error types
Expand Down
2 changes: 1 addition & 1 deletion nri-http/nri-http.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cabal-version: 1.12
-- see: https://github.com/sol/hpack

name: nri-http
version: 0.5.0.2
version: 0.5.0.3
synopsis: Make Elm style HTTP requests
description: Please see the README at <https://github.com/NoRedInk/haskell-libraries/tree/trunk/nri-http#readme>.
category: Web
Expand Down
2 changes: 1 addition & 1 deletion nri-http/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: nri-http
synopsis: Make Elm style HTTP requests
description: Please see the README at <https://github.com/NoRedInk/haskell-libraries/tree/trunk/nri-http#readme>.
author: NoRedInk
version: 0.5.0.2
version: 0.5.0.3
maintainer: [email protected]
copyright: 2024 NoRedInk Corp.
github: NoRedInk/haskell-libraries/nri-http
Expand Down
21 changes: 14 additions & 7 deletions nri-http/src/Http/Mock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ module Http.Mock
)
where

import qualified Control.Concurrent.MVar as MVar
import qualified Data.Aeson as Aeson
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy
import qualified Data.Dynamic as Dynamic
import qualified Data.IORef
import Data.String (fromString)
import qualified Data.Text.Encoding
import qualified Debug
Expand Down Expand Up @@ -76,21 +76,28 @@ stub ::
(Internal.Handler -> Expect.Expectation) ->
Expect.Expectation' (List a)
stub responders stubbedTestBody = do
logRef <- Expect.fromIO (Data.IORef.newIORef [])
logRef <- Expect.fromIO (MVar.newMVar [])
doAnything <- Expect.fromIO Platform.doAnythingHandler
let mockHandler =
Internal.Handler
( \req -> do
(log, res) <- tryRespond responders req
Data.IORef.modifyIORef' logRef (\prev -> log : prev)
|> map Ok
|> Platform.doAnything doAnything
Prelude.pure res
-- `modifyMVar_` and `withMVar` aren't completely atomic so those
-- could introduce a source of flake if used. that said, `takeMVar` +
-- `putMVar` aren't exception-safe. this is fine for the simple
-- usecase of just prepending to the contained list but if this logic
-- ever needs to include potentially-exception-causing code we'll need
-- to think a bit harder
-- (ref: https://hackage.haskell.org/package/base-4.21.0.0/docs/Control-Concurrent-MVar.html#v:withMVar)
Platform.doAnything doAnything <| do
prev <- MVar.takeMVar logRef
MVar.putMVar logRef (log : prev)
Prelude.pure (Ok res)
)
(\_ -> Debug.todo "We don't mock third party HTTP calls yet")
(\_ -> Debug.todo "We don't mock third party HTTP calls yet")
Expect.around (\f -> f mockHandler) (Stack.withFrozenCallStack stubbedTestBody)
Expect.fromIO (Data.IORef.readIORef logRef)
Expect.fromIO (MVar.readMVar logRef)
|> map List.reverse

-- | Read the body of the request as text. Useful to check what data got
Expand Down
Loading