blobsets/tests/test_http.nim
2019-03-16 18:24:31 +01:00

84 lines
2.1 KiB
Nim

import std/asyncdispatch, std/net, std/random, std/strutils, std/unittest
import ../src/blobsets, ../src/blobsets/filestores, ../src/blobsets/httpstores, ../src/blobsets/httpservers
let
port = (Port)rand(1 shl 15)
store = newFileStore("/tmp/store")
server = newHttpStoreServer(store)
asyncCheck server.serve(port)
let
url = "http://127.0.0.1:$1/" % $port
client = newHttpStore url
suite "Http store":
randomize()
var
blob: BlobId
size: BiggestInt
test "ingest":
(blob, size) = waitFor client.ingestFile("tests/test_http.nim")
test "dump":
for chunk in store.dumpBlob(blob):
discard chunk
test "ingest":
(blob, size) = waitFor client.ingestFile("tests/test_http.nim")
suite "store":
var
setId: SetId
bs: BlobSet
const count = 64
test "commit":
bs = newBlobSet()
for i in 1..count:
let
name = $i
blob = waitFor client.ingest(newString(i))
echo "insert ", blob, " ", i
bs = insert(bs, name, blob, i)
setId = commit(client, bs).setId
test "load":
bs = load(client, setId)
for i in 1..count:
let
name = $i
blob = blobHash newString(i)
other = bs.search(name)
#doAssert(other == blob)
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":
for i in 1..count:
store.randomApply(bs, i) do (id: BlobId; size: BiggestInt):
echo "randomApply: ", id, " ", size
let stream = store.openBlobStream(id, size, dataBlob)
close stream
test "applyAll":
bs = load(client, setId)
for i in 1..count:
var found = false
store.applyAll(bs, i) do (id: BlobId; size: BiggestInt):
if i == size: found = true
if not found:
echo i, " not found"
apply(client, bs, $i) do (id: BlobId; size: BiggestInt):
echo "but ", i, " really is in the set"
raiseAssert($i)