@@ -29,14 +29,12 @@ module Servant.StreamSpec (spec) where
2929import Control.Monad (replicateM_, void)
3030import qualified Data.ByteString as BS
3131import Data.Proxy
32−import GHC.Stats (currentBytesUsed, getGCStats)
32+import GHC.Stats
3333import qualified Network.HTTP.Client as C
3434import Prelude ()
3535import Prelude.Compat
36−import System.IO (IOMode (ReadMode), withFile)
3736import System.IO.Unsafe (unsafePerformIO)
3837import Test.Hspec
39−import Test.QuickCheck
4038
4139import Servant.API ((:<|>) ((:<|>)), (:>), JSON,
4240 NetstringFraming, NewlineFraming,
@@ -46,6 +44,7 @@ import Servant.Client
4644import Servant.ClientSpec (Person (..))
4745import qualified Servant.ClientSpec as CS
4846import Servant.Server
47+import Data.ByteString.Random.MWC (random)
4948
5049
5150spec :: Spec
@@ -81,17 +80,17 @@ server = serve sapi
8180 :<|> return (StreamGenerator lotsGenerator)
8281 where
8382 lotsGenerator f r = do
84− f ""
85− withFile "/dev/urandom" ReadMode $
86− \handle -> streamFiveMBNTimes handle 1000 r
83+ _ <- f ""
84+ streamFiveMBNTimes 1000 r
8785 return ()
8886
89− streamFiveMBNTimes handle left sink
90− | left <= 0 = return ""
87+ streamFiveMBNTimes :: Int -> (BS.ByteString -> IO ()) -> IO ()
88+ streamFiveMBNTimes left sink
89+ | left <= 0 = return ()
9190 | otherwise = do
92− msg <- BS.hGet handle (megabytes 5)
91+ msg <- random (megabytes 5)
9392 sink msg
94− streamFiveMBNTimes handle (left - 1) sink
93+ streamFiveMBNTimes (left - 1) sink
9594
9695
9796
@@ -129,8 +128,12 @@ streamSpec = beforeAll (CS.startWaiApp server) $ afterAll CS.endWaiApp $ do
129128 Right (ResultStream res) <- runClient getGetALot baseUrl
130129 let consumeNChunks n = replicateM_ n (res void)
131130 consumeNChunks 900
131+#if MIN_VERSION_base(4,9,0)
132+ memUsed <- max_mem_in_use_bytes <$> getRTSStats
133+#else
132134 memUsed <- currentBytesUsed <$> getGCStats
135+#endif
133136 memUsed `shouldSatisfy` (< (megabytes 20))
134137
135138megabytes :: Num a => a -> a
136−megabytes n = n * (1000 ^ 2)
139+megabytes n = n * (1000 ^ (2 :: Int))