Skip to content

Commit fe43f88

Browse files
committed
Improve large asset test
1 parent 6558491 commit fe43f88

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

services/cargohold/test/integration/API/Federation.hs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import Bilge.Assert
66
import CargoHold.API.V3 (randToken)
77
import Conduit
88
import Control.Lens
9+
import Crypto.Random
10+
import qualified Data.ByteString as BS
911
import Data.Id
1012
import Data.Qualified
1113
import Data.UUID.V4
@@ -118,14 +120,17 @@ testGetAssetWrongToken c = do
118120
testLargeAsset :: TestSignature ()
119121
testLargeAsset c = do
120122
-- Initial upload
121-
let size = 1024 * 1024
122-
settings =
123+
let settings =
123124
defAssetSettings
124125
& set setAssetRetention (Just AssetVolatile)
125126
uid <- liftIO $ Id <$> nextRandom
127+
-- generate random bytes
128+
let size = 1024 * 1024
129+
bs <- liftIO $ getRandomBytes size
130+
126131
ast :: Asset <-
127132
responseJsonError
128-
=<< uploadRandom (c . path "/assets/v3") uid settings applicationOctetStream size
133+
=<< uploadSimple (c . path "/assets/v3") uid settings (applicationOctetStream, bs)
129134
<!! const 201 === statusCode
130135

131136
-- Call get-asset federation API
@@ -137,11 +142,20 @@ testLargeAsset c = do
137142
gaToken = tok,
138143
gaKey = qUnqualified key
139144
}
145+
let getAllChunks getChunk = fmap reverse . ($ []) . fix $ \go acc -> do
146+
chunk <- getChunk
147+
if BS.null chunk
148+
then pure acc
149+
else go (chunk : acc)
150+
140151
http empty (method HTTP.POST . c . path "/federation/stream-asset" . json ga) $ \resp -> do
141152
statusCode resp @?= 200
142-
-- check that the first chunk is received
143-
chunk <- responseBody resp
144-
print chunk
153+
chunks <- getAllChunks (responseBody resp)
154+
let minNumChunks = 8
155+
assertBool
156+
("Expected at least " <> show minNumChunks <> " chunks, got " <> show (length chunks))
157+
(length chunks > minNumChunks)
158+
mconcat chunks @?= bs
145159

146160
testStreamAsset :: TestSignature ()
147161
testStreamAsset c = do
@@ -165,9 +179,9 @@ testStreamAsset c = do
165179
gaToken = tok,
166180
gaKey = qUnqualified key
167181
}
168-
respBody <- fmap responseBody $
169-
post (c . path "/federation/stream-asset" . json ga)
170-
<!! const 200 === statusCode
171-
182+
respBody <-
183+
fmap responseBody $
184+
post (c . path "/federation/stream-asset" . json ga)
185+
<!! const 200 === statusCode
172186

173187
liftIO $ respBody @?= Just "Hello World"

services/cargohold/test/integration/API/Util.hs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ module API.Util where
2020
import Bilge hiding (body)
2121
import qualified Codec.MIME.Parse as MIME
2222
import qualified Codec.MIME.Type as MIME
23-
import Crypto.Random
2423
import Data.ByteString.Builder
2524
import Data.ByteString.Conversion
2625
import qualified Data.ByteString.Lazy as Lazy
@@ -44,17 +43,6 @@ uploadSimple c usr sets (ct, bs) =
4443
let mp = buildMultipartBody sets ct (Lazy.fromStrict bs)
4544
in uploadRaw c usr (toLazyByteString mp)
4645

47-
uploadRandom ::
48-
CargoHold ->
49-
UserId ->
50-
AssetSettings ->
51-
MIME.Type ->
52-
Int ->
53-
Http (Response (Maybe LByteString))
54-
uploadRandom c usr settings ct size = do
55-
bs <- liftIO $ getRandomBytes size
56-
uploadSimple c usr settings (ct, bs)
57-
5846
decodeHeader :: FromByteString a => HeaderName -> Response b -> a
5947
decodeHeader h =
6048
fromMaybe (error $ "decodeHeader: missing or invalid header: " ++ show h)

0 commit comments

Comments
 (0)