@@ -29,14 +29,12 @@ module Servant.StreamSpec (spec) where
29
29
import Control.Monad (replicateM_ , void )
30
30
import qualified Data.ByteString as BS
31
31
import Data.Proxy
32
- import GHC.Stats ( currentBytesUsed , getGCStats )
32
+ import GHC.Stats
33
33
import qualified Network.HTTP.Client as C
34
34
import Prelude ()
35
35
import Prelude.Compat
36
- import System.IO (IOMode (ReadMode ), withFile )
37
36
import System.IO.Unsafe (unsafePerformIO )
38
37
import Test.Hspec
39
- import Test.QuickCheck
40
38
41
39
import Servant.API ((:<|>) ((:<|>) ), (:>) , JSON ,
42
40
NetstringFraming , NewlineFraming ,
@@ -46,6 +44,7 @@ import Servant.Client
46
44
import Servant.ClientSpec (Person (.. ))
47
45
import qualified Servant.ClientSpec as CS
48
46
import Servant.Server
47
+ import Data.ByteString.Random.MWC (random )
49
48
50
49
51
50
spec :: Spec
@@ -81,17 +80,17 @@ server = serve sapi
81
80
:<|> return (StreamGenerator lotsGenerator)
82
81
where
83
82
lotsGenerator f r = do
84
- f " "
85
- withFile " /dev/urandom" ReadMode $
86
- \ handle -> streamFiveMBNTimes handle 1000 r
83
+ _ <- f " "
84
+ streamFiveMBNTimes 1000 r
87
85
return ()
88
86
89
- streamFiveMBNTimes handle left sink
90
- | left <= 0 = return " "
87
+ streamFiveMBNTimes :: Int -> (BS. ByteString -> IO () ) -> IO ()
88
+ streamFiveMBNTimes left sink
89
+ | left <= 0 = return ()
91
90
| otherwise = do
92
- msg <- BS. hGet handle (megabytes 5 )
91
+ msg <- random (megabytes 5 )
93
92
sink msg
94
- streamFiveMBNTimes handle (left - 1 ) sink
93
+ streamFiveMBNTimes (left - 1 ) sink
95
94
96
95
97
96
@@ -129,8 +128,12 @@ streamSpec = beforeAll (CS.startWaiApp server) $ afterAll CS.endWaiApp $ do
129
128
Right (ResultStream res) <- runClient getGetALot baseUrl
130
129
let consumeNChunks n = replicateM_ n (res void)
131
130
consumeNChunks 900
131
+ #if MIN_VERSION_base(4,9,0)
132
+ memUsed <- max_mem_in_use_bytes <$> getRTSStats
133
+ #else
132
134
memUsed <- currentBytesUsed <$> getGCStats
135
+ #endif
133
136
memUsed `shouldSatisfy` (< (megabytes 20 ))
134
137
135
138
megabytes :: Num a => a -> a
136
- megabytes n = n * (1000 ^ 2 )
139
+ megabytes n = n * (1000 ^ ( 2 :: Int ) )
0 commit comments