@@ -29,12 +29,14 @@ module Servant.StreamSpec (spec) where
2929import Control.Monad (replicateM_, void)
3030import qualified Data.ByteString as BS
3131import Data.Proxy
32−import GHC.Stats
32+import GHC.Stats (currentBytesUsed, getGCStats)
3333import qualified Network.HTTP.Client as C
3434import Prelude ()
3535import Prelude.Compat
36+import System.IO (IOMode (ReadMode), withFile)
3637import System.IO.Unsafe (unsafePerformIO)
3738import Test.Hspec
39+import Test.QuickCheck
3840
3941import Servant.API ((:<|>) ((:<|>)), (:>), JSON,
4042 NetstringFraming, NewlineFraming,
@@ -44,7 +46,6 @@ import Servant.Client
4446import Servant.ClientSpec (Person (..))
4547import qualified Servant.ClientSpec as CS
4648import Servant.Server
47−import Data.ByteString.Random.MWC (random)
4849
4950
5051spec :: Spec
@@ -80,17 +81,17 @@ server = serve sapi
8081 :<|> return (StreamGenerator lotsGenerator)
8182 where
8283 lotsGenerator f r = do
83− _ <- f ""
84− streamFiveMBNTimes 1000 r
84+ f ""
85+ withFile "/dev/urandom" ReadMode $
86+ \handle -> streamFiveMBNTimes handle 1000 r
8587 return ()
8688
87− streamFiveMBNTimes :: Int -> (BS.ByteString -> IO ()) -> IO ()
88− streamFiveMBNTimes left sink
89− | left <= 0 = return ()
89+ streamFiveMBNTimes handle left sink
90+ | left <= 0 = return ""
9091 | otherwise = do
91− msg <- random (megabytes 5)
92+ msg <- BS.hGet handle (megabytes 5)
9293 sink msg
93− streamFiveMBNTimes (left - 1) sink
94+ streamFiveMBNTimes handle (left - 1) sink
9495
9596
9697
@@ -128,12 +129,8 @@ streamSpec = beforeAll (CS.startWaiApp server) $ afterAll CS.endWaiApp $ do
128129 Right (ResultStream res) <- runClient getGetALot baseUrl
129130 let consumeNChunks n = replicateM_ n (res void)
130131 consumeNChunks 900
131−#if MIN_VERSION_base(4,9,0)
132− memUsed <- max_mem_in_use_bytes <$> getRTSStats
133−#else
134132 memUsed <- currentBytesUsed <$> getGCStats
135−#endif
136133 memUsed `shouldSatisfy` (< (megabytes 20))
137134
138135megabytes :: Num a => a -> a
139−megabytes n = n * (1000 ^ (2 :: Int))
136+megabytes n = n * (1000 ^ 2)