blobsets/tests/test_http.nim

92 lines
2.3 KiB
Nim
Raw Normal View History

2019-01-20 17:14:32 +01:00
import std/asyncdispatch, std/net, std/random, std/strutils, std/unittest
import ../src/blobsets, ../src/blobsets/filestores, ../src/blobsets/httpstores, ../src/blobsets/httpservers
2019-02-15 21:56:21 +01:00
let
port = (Port)rand(1 shl 15)
store = newFileStore("/tmp/store")
server = newHttpStoreServer(store)
asyncCheck server.serve(port)
2019-01-20 17:14:32 +01:00
2019-02-15 21:56:21 +01:00
let
url = "http://127.0.0.1:$1/" % $port
client = newHttpStore url
2019-01-20 17:14:32 +01:00
2019-02-15 21:56:21 +01:00
suite "Http store":
randomize()
2019-01-20 17:14:32 +01:00
var
blob: BlobId
size: BiggestInt
test "ingest":
2019-02-15 21:56:21 +01:00
(blob, size) = waitFor client.ingestFile("tests/test_http.nim")
2019-01-20 17:14:32 +01:00
test "dump":
for chunk in store.dumpBlob(blob):
2019-02-15 21:56:21 +01:00
discard chunk
test "ingest":
(blob, size) = waitFor client.ingestFile("tests/test_http.nim")
const count = 64
var
setId: SetId
bs: BlobSet
2019-02-15 21:56:21 +01:00
suite "store":
var rng = initRand(rand(int.high))
2019-02-15 21:56:21 +01:00
test "commit":
bs = newBlobSet()
2019-02-15 21:56:21 +01:00
for i in 1..count:
let
name = $i
blob = waitFor client.ingest(newString(i))
2019-03-18 22:50:00 +01:00
bs = waitFor insert(client, bs, name, blob, i)
setId = (waitFor commit(client, bs)).setId
2019-02-15 21:56:21 +01:00
test "load":
bs = waitFor client.load(setId)
2019-02-15 21:56:21 +01:00
for i in 1..count:
let
name = $i
blob = blobHash newString(i)
2019-03-18 22:50:00 +01:00
var found = false
apply(client, bs, name) do (id: BlobId; size: BiggestInt):
2019-04-03 21:29:27 +02:00
check(id == blob)
2019-03-18 22:50:00 +01:00
found = true
2019-04-03 21:29:27 +02:00
check(found)
2019-02-15 21:56:21 +01:00
for i in 1..count:
let
i = i and 0x8000
name = $i
apply(client, bs, name) do (id: BlobId; size: BiggestInt):
echo "inserted ", name, " - ", name.toKey
echo "applied ", name, " - ", ($(i xor 0x8000)).toKey
raiseAssert("apply succedded for a key not inserted")
test "random":
2019-02-16 12:48:08 +01:00
for i in 1..count:
2019-03-17 22:02:39 +01:00
store.randomApply(bs, rng) do (id: BlobId; size: BiggestInt):
let stream = store.openBlobStream(id, size, dataBlob)
close stream
2019-03-16 11:52:24 +01:00
test "stream":
proc findAll() {.async.} =
for i in 1..count:
let stream = newMemberStream()
asyncCheck stream.streamMembers(store, bs)
var
found = false
count = 0
while not found:
let (valid, val) = await stream.read()
if not valid: break
if i == val.size:
complete stream
found = true
inc count
2019-04-03 21:29:27 +02:00
check(count > 0)
check(found)
waitFor findAll()