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

Create completely new TVars in LoggingSpec #2573

Merged
merged 2 commits into from
Mar 23, 2021
Merged
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
21 changes: 4 additions & 17 deletions lib/core/test/unit/Network/Wai/Middleware/LoggingSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ import Servant
import Servant.Server
( Handler )
import Test.Hspec
( Spec, after, afterAll, beforeAll, describe, it, shouldBe, shouldContain )
( Spec, after, before, describe, it, shouldBe, shouldContain )
import Test.QuickCheck
( Arbitrary (..), choose, counterexample, property, withMaxSuccess )
import Test.QuickCheck.Monadic
Expand All @@ -103,7 +103,7 @@ import UnliftIO.Concurrent
import UnliftIO.MVar
( newEmptyMVar, putMVar, readMVar )
import UnliftIO.STM
( TVar, atomically, newTVarIO, readTVarIO, writeTVar )
( TVar, newTVarIO, readTVarIO )

import qualified Data.Aeson as Aeson
import qualified Data.List as L
Expand All @@ -112,7 +112,7 @@ import qualified Network.Wai.Handler.Warp as Warp

spec :: Spec
spec = describe "Logging Middleware"
$ beforeAll setup $ after clearLogs $ afterAll tearDown $ do
$ before setup $ after tearDown $ do
it "GET, 200, no query" $ \ctx -> do
get ctx "/get"
expectLogs ctx
Expand Down Expand Up @@ -216,7 +216,7 @@ spec = describe "Logging Middleware"
property $ \(NumberOfRequests n) -> monadicIO $ do
entries <- liftIO $ do
replicateConcurrently_ n (get ctx "/get")
skipPrevLogs <$> takeLogs ctx
takeLogs ctx
let getReqId (ApiLog (RequestId rid) _) = rid
let uniqueReqIds = L.nubBy (\l1 l2 -> getReqId l1 == getReqId l2)
let numUniqueReqIds = length (uniqueReqIds entries)
Expand Down Expand Up @@ -282,24 +282,11 @@ data Context = Context
takeLogs :: Context -> IO [ApiLog]
takeLogs ctx = reverse <$> readTVarIO (logs ctx)

-- | Remove any partial logs which have come from the previous property test
-- run. These can occur because once the requests have completed, the property
-- finishes, but the response side may still be finishing writing its log
-- messages.
skipPrevLogs :: [ApiLog] -> [ApiLog]
skipPrevLogs = dropWhile (notLogRequestStart . logMsg)
where
notLogRequestStart LogRequestStart = False
notLogRequestStart _ = True

-- | Give server time to finish off its request handler - the intention is to
-- ensure that /all/ the response logs are captured before checking assertions.
waitForServerToComplete :: IO ()
waitForServerToComplete = threadDelay 500_000

clearLogs :: Context -> IO ()
clearLogs = atomically . flip writeTVar [] . logs

{-------------------------------------------------------------------------------
Test Helpers
-------------------------------------------------------------------------------}
Expand Down