From 7c901dcb7d3f568eef86448116d50904fb32a9a8 Mon Sep 17 00:00:00 2001 From: "Julian K. Arni" Date: Mon, 19 Mar 2018 18:22:49 +0100 Subject: [PATCH] Re-review changes. Using random packages mysteriously fail on CI, and also uses a lot more CPU. --- servant-client/test/Servant/StreamSpec.hs | 25 ++++++++++------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/servant-client/test/Servant/StreamSpec.hs b/servant-client/test/Servant/StreamSpec.hs index 2b688ed9..ad4a2664 100644 --- a/servant-client/test/Servant/StreamSpec.hs +++ b/servant-client/test/Servant/StreamSpec.hs @@ -29,12 +29,14 @@ module Servant.StreamSpec (spec) where import Control.Monad (replicateM_, void) import qualified Data.ByteString as BS import Data.Proxy -import GHC.Stats +import GHC.Stats (currentBytesUsed, getGCStats) import qualified Network.HTTP.Client as C import Prelude () import Prelude.Compat +import System.IO (IOMode (ReadMode), withFile) import System.IO.Unsafe (unsafePerformIO) import Test.Hspec +import Test.QuickCheck import Servant.API ((:<|>) ((:<|>)), (:>), JSON, NetstringFraming, NewlineFraming, @@ -44,7 +46,6 @@ import Servant.Client import Servant.ClientSpec (Person (..)) import qualified Servant.ClientSpec as CS import Servant.Server -import Data.ByteString.Random.MWC (random) spec :: Spec @@ -80,17 +81,17 @@ server = serve sapi :<|> return (StreamGenerator lotsGenerator) where lotsGenerator f r = do - _ <- f "" - streamFiveMBNTimes 1000 r + f "" + withFile "/dev/urandom" ReadMode $ + \handle -> streamFiveMBNTimes handle 1000 r return () - streamFiveMBNTimes :: Int -> (BS.ByteString -> IO ()) -> IO () - streamFiveMBNTimes left sink - | left <= 0 = return () + streamFiveMBNTimes handle left sink + | left <= 0 = return "" | otherwise = do - msg <- random (megabytes 5) + msg <- BS.hGet handle (megabytes 5) sink msg - streamFiveMBNTimes (left - 1) sink + streamFiveMBNTimes handle (left - 1) sink @@ -128,12 +129,8 @@ streamSpec = beforeAll (CS.startWaiApp server) $ afterAll CS.endWaiApp $ do Right (ResultStream res) <- runClient getGetALot baseUrl let consumeNChunks n = replicateM_ n (res void) consumeNChunks 900 -#if MIN_VERSION_base(4,9,0) - memUsed <- max_mem_in_use_bytes <$> getRTSStats -#else memUsed <- currentBytesUsed <$> getGCStats -#endif memUsed `shouldSatisfy` (< (megabytes 20)) megabytes :: Num a => a -> a -megabytes n = n * (1000 ^ (2 :: Int)) +megabytes n = n * (1000 ^ 2)