Skip to content

Commit

Permalink
Merge #974
Browse files Browse the repository at this point in the history
974: tests: Fix Network.Wai.Middleware.LoggingSpec on Windows r=KtorZ a=rvl

Relates to #703.

# Overview

```
  test/unit/Network/Wai/Middleware/LoggingSpec.hs:109:5:
  9) Network.Wai.Middleware.Logging, Logging Middleware, GET, 200, no query
       uncaught exception: IOException of type UserError
       user error (Expected exactly 3 log entries but got 1: [LogObject {loName = "request-0", loMeta = LOMeta {tstamp = 2019-11-05 04:43:05.8652477 UTC, tid = "ThreadId 13863", severity = Info, privacy = Public}, loContent = LogMessage "[GET] /get"}])

  To rerun use: --match "/Network.Wai.Middleware.Logging/Logging Middleware/GET, 200, no query/"

  test/unit/Network/Wai/Middleware/LoggingSpec.hs:117:5:
  10) Network.Wai.Middleware.Logging, Logging Middleware, GET, 200, with query
       uncaught exception: IOException of type UserError
       user error (Expected exactly 3 log entries but got 5: [LogObject {loName = "request-0", loMeta = LOMeta {tstamp = 2019-11-05 04:43:05.8652477 UTC, tid = "ThreadId 13863", severity = Info, privacy = Public}, loContent = LogMessage "200 OK in 0s"},LogObject {loName = "request-0", loMeta = LOMeta {tstamp = 2019-11-05 04:43:05.8652477 UTC, tid = "ThreadId 13863", severity = Debug, privacy = Public}, loContent = LogMessage "14"},LogObject {loName = "request-1", loMeta = LOMeta {tstamp = 2019-11-05 04:43:05.8652477 UTC, tid = "ThreadId 13863", severity = Info, privacy = Public}, loContent = LogMessage "[GET] /get?query=patate"},LogObject {loName = "request-1", loMeta = LOMeta {tstamp = 2019-11-05 04:43:05.8652477 UTC, tid = "ThreadId 13863", severity = Info, privacy = Public}, loContent = LogMessage "200 OK in 0s"},LogObject {loName = "request-1", loMeta = LOMeta {tstamp = 2019-11-05 04:43:05.8652477 UTC, tid = "ThreadId 13863", severity = Debug, privacy = Public}, loContent = LogMessage "14"}])

  To rerun use: --match "/Network.Wai.Middleware.Logging/Logging Middleware/GET, 200, with query/"

  test/unit/Network/Wai/Middleware/LoggingSpec.hs:184:13:
  11) Network.Wai.Middleware.Logging, Logging Middleware, different request ids
       Falsifiable (after 4 tests and 6 shrinks):
         NumberOfRequests 1
       expected: 1
        but got: 48

  To rerun use: --match "/Network.Wai.Middleware.Logging/Logging Middleware/different request ids/"

  test/unit/Network/Wai/Middleware/LoggingSpec.hs:198:13:
  12) Network.Wai.Middleware.Logging, Logging Middleware, correct time measures
       Falsifiable (after 1 test):
         (NumberOfRequests 5,RandomIndex 1)
       expected: 1
        but got: 0

  To rerun use: --match "/Network.Wai.Middleware.Logging/Logging Middleware/correct time measures/"

Randomized with seed 534946986

Finished in 629.7711 seconds
915 examples, 12 failures, 1 pending

C:\Users\win\cw>
```

- This "fixes" the tests on windows.
- "different request ids" test still fails

# Comments

It seems like iohk-monitoring writes its logs asynchronously.
Or there is some other race condition with the TVar containing log messages.


Co-authored-by: Rodney Lorrimar <[email protected]>
  • Loading branch information
iohk-bors[bot] and rvl authored Nov 11, 2019
2 parents 535b2e9 + 6b73020 commit 078ab8b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/core/test/unit/Network/Wai/Middleware/LoggingSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ import Test.QuickCheck
( Arbitrary (..), choose, property )
import Test.QuickCheck.Monadic
( monadicIO )
import Test.Utils.Windows
( pendingOnWindows, whenWindows )

import qualified Data.Aeson as Aeson
import qualified Data.Map.Strict as Map
Expand Down Expand Up @@ -195,6 +197,7 @@ spec = describe "Logging Middleware"
entries <- readTVarIO (logs ctx)
let index = Map.fromList
$ catMaybes [ (loName l,) <$> captureTime l | l <- entries ]
pendingOnWindows "Disabled on windows due to race with log flushing"
Map.size (Map.filter (> (200*ms)) index) `shouldBe` 1
where
setup :: IO Context
Expand Down Expand Up @@ -267,6 +270,8 @@ postIlled ctx path body = do

expectLogs :: Context -> [(Severity, String)] -> IO ()
expectLogs ctx expectations = do
whenWindows $ threadDelay 1000000 -- let iohk-monitoring flush the logs

entries <- reverse <$> readTVarIO (logs ctx)

when (length entries /= length expectations) $
Expand Down
12 changes: 10 additions & 2 deletions lib/test-utils/src/Test/Utils/Windows.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

module Test.Utils.Windows
( skipOnWindows
, pendingOnWindows
, whenWindows
) where

import Prelude
Expand All @@ -19,9 +21,15 @@ import Control.Monad
import System.Info
( os )
import Test.Hspec.Core.Spec
( ResultStatus (..) )
( ResultStatus (..), pendingWith )
import Test.Hspec.Expectations
( Expectation, HasCallStack )

skipOnWindows :: HasCallStack => String -> Expectation
skipOnWindows _reason = when (os == "mingw32") $ throwIO Success
skipOnWindows _reason = whenWindows $ throwIO Success

pendingOnWindows :: HasCallStack => String -> Expectation
pendingOnWindows reason = whenWindows $ pendingWith reason

whenWindows :: IO () -> IO ()
whenWindows = when (os == "mingw32")

0 comments on commit 078ab8b

Please sign in to comment.