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