use tryGet, which is more informative than get (#45)
This commit is contained in:
parent
b315c759bb
commit
68a45d8f57
|
@ -41,14 +41,14 @@ suite "NetworkStore engine - 2 nodes":
|
||||||
if chunk.len <= 0:
|
if chunk.len <= 0:
|
||||||
break
|
break
|
||||||
|
|
||||||
blocks1.add(bt.Block.init(chunk).get())
|
blocks1.add(bt.Block.init(chunk).tryGet())
|
||||||
|
|
||||||
while true:
|
while true:
|
||||||
let chunk = await chunker2.getBytes()
|
let chunk = await chunker2.getBytes()
|
||||||
if chunk.len <= 0:
|
if chunk.len <= 0:
|
||||||
break
|
break
|
||||||
|
|
||||||
blocks2.add(bt.Block.init(chunk).get())
|
blocks2.add(bt.Block.init(chunk).tryGet())
|
||||||
|
|
||||||
switch1 = newStandardSwitch()
|
switch1 = newStandardSwitch()
|
||||||
switch2 = newStandardSwitch()
|
switch2 = newStandardSwitch()
|
||||||
|
@ -119,7 +119,7 @@ suite "NetworkStore engine - 2 nodes":
|
||||||
check peerCtx2.account.?address == pricing2.address.some
|
check peerCtx2.account.?address == pricing2.address.some
|
||||||
|
|
||||||
test "should send want-have for block":
|
test "should send want-have for block":
|
||||||
let blk = bt.Block.init("Block 1".toBytes).get()
|
let blk = bt.Block.init("Block 1".toBytes).tryGet()
|
||||||
check await blockexc2.engine.localStore.putBlock(blk)
|
check await blockexc2.engine.localStore.putBlock(blk)
|
||||||
|
|
||||||
let entry = Entry(
|
let entry = Entry(
|
||||||
|
@ -144,7 +144,7 @@ suite "NetworkStore engine - 2 nodes":
|
||||||
check blocks.mapIt( !it.read ) == blocks2
|
check blocks.mapIt( !it.read ) == blocks2
|
||||||
|
|
||||||
test "remote should send blocks when available":
|
test "remote should send blocks when available":
|
||||||
let blk = bt.Block.init("Block 1".toBytes).get()
|
let blk = bt.Block.init("Block 1".toBytes).tryGet()
|
||||||
|
|
||||||
# should fail retrieving block from remote
|
# should fail retrieving block from remote
|
||||||
check not await blockexc1.getBlock(blk.cid)
|
check not await blockexc1.getBlock(blk.cid)
|
||||||
|
@ -183,7 +183,7 @@ suite "NetworkStore - multiple nodes":
|
||||||
if chunk.len <= 0:
|
if chunk.len <= 0:
|
||||||
break
|
break
|
||||||
|
|
||||||
blocks.add(bt.Block.init(chunk).get())
|
blocks.add(bt.Block.init(chunk).tryGet())
|
||||||
|
|
||||||
for e in generateNodes(5):
|
for e in generateNodes(5):
|
||||||
switch.add(e.switch)
|
switch.add(e.switch)
|
||||||
|
|
|
@ -19,8 +19,8 @@ import ../examples
|
||||||
suite "NetworkStore engine basic":
|
suite "NetworkStore engine basic":
|
||||||
let
|
let
|
||||||
rng = Rng.instance()
|
rng = Rng.instance()
|
||||||
seckey = PrivateKey.random(rng[]).get()
|
seckey = PrivateKey.random(rng[]).tryGet()
|
||||||
peerId = PeerID.init(seckey.getPublicKey().get()).get()
|
peerId = PeerID.init(seckey.getPublicKey().tryGet()).tryGet()
|
||||||
chunker = RandomChunker.new(Rng.instance(), size = 1024, chunkSize = 256)
|
chunker = RandomChunker.new(Rng.instance(), size = 1024, chunkSize = 256)
|
||||||
wallet = WalletRef.example
|
wallet = WalletRef.example
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ suite "NetworkStore engine basic":
|
||||||
if chunk.len <= 0:
|
if chunk.len <= 0:
|
||||||
break
|
break
|
||||||
|
|
||||||
blocks.add(bt.Block.init(chunk).get())
|
blocks.add(bt.Block.init(chunk).tryGet())
|
||||||
|
|
||||||
done = newFuture[void]()
|
done = newFuture[void]()
|
||||||
|
|
||||||
|
@ -86,8 +86,8 @@ suite "NetworkStore engine basic":
|
||||||
suite "NetworkStore engine handlers":
|
suite "NetworkStore engine handlers":
|
||||||
let
|
let
|
||||||
rng = Rng.instance()
|
rng = Rng.instance()
|
||||||
seckey = PrivateKey.random(rng[]).get()
|
seckey = PrivateKey.random(rng[]).tryGet()
|
||||||
peerId = PeerID.init(seckey.getPublicKey().get()).get()
|
peerId = PeerID.init(seckey.getPublicKey().tryGet()).tryGet()
|
||||||
chunker = RandomChunker.new(Rng.instance(), size = 1024, chunkSize = 256)
|
chunker = RandomChunker.new(Rng.instance(), size = 1024, chunkSize = 256)
|
||||||
wallet = WalletRef.example
|
wallet = WalletRef.example
|
||||||
|
|
||||||
|
@ -103,7 +103,7 @@ suite "NetworkStore engine handlers":
|
||||||
if chunk.len <= 0:
|
if chunk.len <= 0:
|
||||||
break
|
break
|
||||||
|
|
||||||
blocks.add(bt.Block.init(chunk).get())
|
blocks.add(bt.Block.init(chunk).tryGet())
|
||||||
|
|
||||||
done = newFuture[void]()
|
done = newFuture[void]()
|
||||||
engine = BlockExcEngine.new(MemoryStore.new(), wallet, BlockExcNetwork())
|
engine = BlockExcEngine.new(MemoryStore.new(), wallet, BlockExcNetwork())
|
||||||
|
@ -227,15 +227,15 @@ suite "Task Handler":
|
||||||
if chunk.len <= 0:
|
if chunk.len <= 0:
|
||||||
break
|
break
|
||||||
|
|
||||||
blocks.add(bt.Block.init(chunk).get())
|
blocks.add(bt.Block.init(chunk).tryGet())
|
||||||
|
|
||||||
done = newFuture[void]()
|
done = newFuture[void]()
|
||||||
engine = BlockExcEngine.new(MemoryStore.new(), wallet, BlockExcNetwork())
|
engine = BlockExcEngine.new(MemoryStore.new(), wallet, BlockExcNetwork())
|
||||||
peersCtx = @[]
|
peersCtx = @[]
|
||||||
|
|
||||||
for i in 0..3:
|
for i in 0..3:
|
||||||
let seckey = PrivateKey.random(rng[]).get()
|
let seckey = PrivateKey.random(rng[]).tryGet()
|
||||||
peers.add(PeerID.init(seckey.getPublicKey().get()).get())
|
peers.add(PeerID.init(seckey.getPublicKey().tryGet()).tryGet())
|
||||||
|
|
||||||
peersCtx.add(BlockExcPeerCtx(
|
peersCtx.add(BlockExcPeerCtx(
|
||||||
id: peers[i]
|
id: peers[i]
|
||||||
|
@ -281,7 +281,7 @@ suite "Task Handler":
|
||||||
|
|
||||||
test "Should send presence":
|
test "Should send presence":
|
||||||
let present = blocks
|
let present = blocks
|
||||||
let missing = @[bt.Block.init("missing".toBytes).get()]
|
let missing = @[bt.Block.init("missing".toBytes).tryGet()]
|
||||||
let price = (!engine.pricing).price
|
let price = (!engine.pricing).price
|
||||||
|
|
||||||
proc sendPresence(id: PeerID, presence: seq[BlockPresence]) =
|
proc sendPresence(id: PeerID, presence: seq[BlockPresence]) =
|
||||||
|
|
|
@ -18,8 +18,8 @@ import ../examples
|
||||||
suite "NetworkStore network":
|
suite "NetworkStore network":
|
||||||
let
|
let
|
||||||
rng = Rng.instance()
|
rng = Rng.instance()
|
||||||
seckey = PrivateKey.random(rng[]).get()
|
seckey = PrivateKey.random(rng[]).tryGet()
|
||||||
peerId = PeerID.init(seckey.getPublicKey().get()).get()
|
peerId = PeerID.init(seckey.getPublicKey().tryGet()).tryGet()
|
||||||
chunker = RandomChunker.new(Rng.instance(), size = 1024, chunkSize = 256)
|
chunker = RandomChunker.new(Rng.instance(), size = 1024, chunkSize = 256)
|
||||||
|
|
||||||
var
|
var
|
||||||
|
@ -38,7 +38,7 @@ suite "NetworkStore network":
|
||||||
if chunk.len <= 0:
|
if chunk.len <= 0:
|
||||||
break
|
break
|
||||||
|
|
||||||
blocks.add(bt.Block.init(chunk).get())
|
blocks.add(bt.Block.init(chunk).tryGet())
|
||||||
|
|
||||||
done = newFuture[void]()
|
done = newFuture[void]()
|
||||||
buffer = BufferStream.new()
|
buffer = BufferStream.new()
|
||||||
|
@ -154,7 +154,7 @@ suite "NetworkStore Network - e2e":
|
||||||
if chunk.len <= 0:
|
if chunk.len <= 0:
|
||||||
break
|
break
|
||||||
|
|
||||||
blocks.add(bt.Block.init(chunk).get())
|
blocks.add(bt.Block.init(chunk).tryGet())
|
||||||
|
|
||||||
done = newFuture[void]()
|
done = newFuture[void]()
|
||||||
switch1 = newStandardSwitch()
|
switch1 = newStandardSwitch()
|
||||||
|
|
|
@ -42,7 +42,7 @@ proc example*(_: type Pricing): Pricing =
|
||||||
proc example*(_: type Block): Block =
|
proc example*(_: type Block): Block =
|
||||||
let length = rand(4096)
|
let length = rand(4096)
|
||||||
let bytes = newSeqWith(length, rand(uint8))
|
let bytes = newSeqWith(length, rand(uint8))
|
||||||
Block.init(bytes).get()
|
Block.init(bytes).tryGet()
|
||||||
|
|
||||||
proc example*(_: type PeerId): PeerID =
|
proc example*(_: type PeerId): PeerID =
|
||||||
let key = PrivateKey.random(Rng.instance[]).get
|
let key = PrivateKey.random(Rng.instance[]).get
|
||||||
|
|
|
@ -21,7 +21,7 @@ suite "FS Store":
|
||||||
var
|
var
|
||||||
store: FSStore
|
store: FSStore
|
||||||
repoDir: string
|
repoDir: string
|
||||||
newBlock = Block.init("New Block".toBytes()).get()
|
newBlock = Block.init("New Block".toBytes()).tryGet()
|
||||||
|
|
||||||
setup:
|
setup:
|
||||||
repoDir = path.parentDir / "repo"
|
repoDir = path.parentDir / "repo"
|
||||||
|
|
|
@ -11,7 +11,7 @@ import ../helpers
|
||||||
suite "Memory Store tests":
|
suite "Memory Store tests":
|
||||||
test "putBlock":
|
test "putBlock":
|
||||||
let
|
let
|
||||||
newBlock = Block.init("New Block".toBytes()).get()
|
newBlock = Block.init("New Block".toBytes()).tryGet()
|
||||||
store = MemoryStore.new()
|
store = MemoryStore.new()
|
||||||
|
|
||||||
check await store.putBlock(newBlock)
|
check await store.putBlock(newBlock)
|
||||||
|
@ -19,7 +19,7 @@ suite "Memory Store tests":
|
||||||
|
|
||||||
test "getBlock":
|
test "getBlock":
|
||||||
let
|
let
|
||||||
newBlock = Block.init("New Block".toBytes()).get()
|
newBlock = Block.init("New Block".toBytes()).tryGet()
|
||||||
store = MemoryStore.new(@[newBlock])
|
store = MemoryStore.new(@[newBlock])
|
||||||
|
|
||||||
let blk = await store.getBlock(newBlock.cid)
|
let blk = await store.getBlock(newBlock.cid)
|
||||||
|
@ -28,7 +28,7 @@ suite "Memory Store tests":
|
||||||
|
|
||||||
test "fail getBlock":
|
test "fail getBlock":
|
||||||
let
|
let
|
||||||
newBlock = Block.init("New Block".toBytes()).get()
|
newBlock = Block.init("New Block".toBytes()).tryGet()
|
||||||
store = MemoryStore.new(@[])
|
store = MemoryStore.new(@[])
|
||||||
|
|
||||||
let blk = await store.getBlock(newBlock.cid)
|
let blk = await store.getBlock(newBlock.cid)
|
||||||
|
@ -36,21 +36,21 @@ suite "Memory Store tests":
|
||||||
|
|
||||||
test "hasBlock":
|
test "hasBlock":
|
||||||
let
|
let
|
||||||
newBlock = Block.init("New Block".toBytes()).get()
|
newBlock = Block.init("New Block".toBytes()).tryGet()
|
||||||
store = MemoryStore.new(@[newBlock])
|
store = MemoryStore.new(@[newBlock])
|
||||||
|
|
||||||
check store.hasBlock(newBlock.cid)
|
check store.hasBlock(newBlock.cid)
|
||||||
|
|
||||||
test "fail hasBlock":
|
test "fail hasBlock":
|
||||||
let
|
let
|
||||||
newBlock = Block.init("New Block".toBytes()).get()
|
newBlock = Block.init("New Block".toBytes()).tryGet()
|
||||||
store = MemoryStore.new(@[])
|
store = MemoryStore.new(@[])
|
||||||
|
|
||||||
check not store.hasBlock(newBlock.cid)
|
check not store.hasBlock(newBlock.cid)
|
||||||
|
|
||||||
test "delBlock":
|
test "delBlock":
|
||||||
let
|
let
|
||||||
newBlock = Block.init("New Block".toBytes()).get()
|
newBlock = Block.init("New Block".toBytes()).tryGet()
|
||||||
store = MemoryStore.new(@[newBlock])
|
store = MemoryStore.new(@[newBlock])
|
||||||
|
|
||||||
check await store.delBlock(newBlock.cid)
|
check await store.delBlock(newBlock.cid)
|
||||||
|
|
|
@ -19,7 +19,7 @@ proc toSortedSeq[T](h: AsyncHeapQueue[T], queueType = QueueType.Min): seq[T] =
|
||||||
for d in h:
|
for d in h:
|
||||||
check tmp.pushNoWait(d).isOk
|
check tmp.pushNoWait(d).isOk
|
||||||
while tmp.len > 0:
|
while tmp.len > 0:
|
||||||
result.add(popNoWait(tmp).get())
|
result.add(popNoWait(tmp).tryGet())
|
||||||
|
|
||||||
suite "synchronous tests":
|
suite "synchronous tests":
|
||||||
test "test pushNoWait - Min":
|
test "test pushNoWait - Min":
|
||||||
|
@ -189,7 +189,7 @@ suite "asynchronous tests":
|
||||||
check heap[0] == ("b", 3) # sanity check for order
|
check heap[0] == ("b", 3) # sanity check for order
|
||||||
|
|
||||||
let fut = heap.pushOrUpdate(("c", 2)) # attempt to push a non existen item but block
|
let fut = heap.pushOrUpdate(("c", 2)) # attempt to push a non existen item but block
|
||||||
check heap.popNoWait().get() == ("b", 3) # pop one off
|
check heap.popNoWait().tryGet() == ("b", 3) # pop one off
|
||||||
await fut # wait for push to complete
|
await fut # wait for push to complete
|
||||||
|
|
||||||
check heap[0] == (name: "c", priority: 2) # check order again
|
check heap[0] == (name: "c", priority: 2) # check order again
|
||||||
|
|
|
@ -17,13 +17,13 @@ suite "Manifest":
|
||||||
test "Should produce valid tree hash checksum":
|
test "Should produce valid tree hash checksum":
|
||||||
without var manifest =? BlocksManifest.init(
|
without var manifest =? BlocksManifest.init(
|
||||||
blocks = @[
|
blocks = @[
|
||||||
Block.init("Block 1".toBytes).get().cid,
|
Block.init("Block 1".toBytes).tryGet().cid,
|
||||||
Block.init("Block 2".toBytes).get().cid,
|
Block.init("Block 2".toBytes).tryGet().cid,
|
||||||
Block.init("Block 3".toBytes).get().cid,
|
Block.init("Block 3".toBytes).tryGet().cid,
|
||||||
Block.init("Block 4".toBytes).get().cid,
|
Block.init("Block 4".toBytes).tryGet().cid,
|
||||||
Block.init("Block 5".toBytes).get().cid,
|
Block.init("Block 5".toBytes).tryGet().cid,
|
||||||
Block.init("Block 6".toBytes).get().cid,
|
Block.init("Block 6".toBytes).tryGet().cid,
|
||||||
Block.init("Block 7".toBytes).get().cid,
|
Block.init("Block 7".toBytes).tryGet().cid,
|
||||||
]):
|
]):
|
||||||
fail()
|
fail()
|
||||||
|
|
||||||
|
@ -35,22 +35,22 @@ suite "Manifest":
|
||||||
82, 184, 85]
|
82, 184, 85]
|
||||||
|
|
||||||
var mh: MultiHash
|
var mh: MultiHash
|
||||||
check MultiHash.decode(checksum, mh).get() > 0
|
check MultiHash.decode(checksum, mh).tryGet() > 0
|
||||||
|
|
||||||
let checkSumCid = Cid.init(manifest.version, manifest.codec, mh).get()
|
let checkSumCid = Cid.init(manifest.version, manifest.codec, mh).tryGet()
|
||||||
check checkSumCid == manifest.cid.get()
|
check checkSumCid == manifest.cid.tryGet()
|
||||||
|
|
||||||
test "Should encode/decode to/from manifest":
|
test "Should encode/decode to/from manifest":
|
||||||
let
|
let
|
||||||
blocks = (0..<1000).mapIt(
|
blocks = (0..<1000).mapIt(
|
||||||
Block.init(("Block " & $it).toBytes).get().cid
|
Block.init(("Block " & $it).toBytes).tryGet().cid
|
||||||
)
|
)
|
||||||
|
|
||||||
var
|
var
|
||||||
manifest = BlocksManifest.init(blocks).get()
|
manifest = BlocksManifest.init(blocks).tryGet()
|
||||||
|
|
||||||
let
|
let
|
||||||
e = manifest.encode().get()
|
e = manifest.encode().tryGet()
|
||||||
(cid, decoded) = BlocksManifest.decode(e).get()
|
(cid, decoded) = BlocksManifest.decode(e).tryGet()
|
||||||
|
|
||||||
check decoded == blocks
|
check decoded == blocks
|
||||||
|
|
|
@ -55,27 +55,27 @@ suite "Test Node":
|
||||||
storeFut = node.store(stream)
|
storeFut = node.store(stream)
|
||||||
|
|
||||||
var
|
var
|
||||||
manifest = BlocksManifest.init().get()
|
manifest = BlocksManifest.init().tryGet()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while (
|
while (
|
||||||
let chunk = await chunker.getBytes();
|
let chunk = await chunker.getBytes();
|
||||||
chunk.len > 0):
|
chunk.len > 0):
|
||||||
await stream.pushData(chunk)
|
await stream.pushData(chunk)
|
||||||
manifest.put(bt.Block.init(chunk).get().cid)
|
manifest.put(bt.Block.init(chunk).tryGet().cid)
|
||||||
finally:
|
finally:
|
||||||
await stream.pushEof()
|
await stream.pushEof()
|
||||||
await stream.close()
|
await stream.close()
|
||||||
|
|
||||||
let
|
let
|
||||||
manifestCid = (await storeFut).get()
|
manifestCid = (await storeFut).tryGet()
|
||||||
|
|
||||||
check:
|
check:
|
||||||
manifestCid in localStore
|
manifestCid in localStore
|
||||||
|
|
||||||
var
|
var
|
||||||
manifestBlock = (await localStore.getBlock(manifestCid)).get()
|
manifestBlock = (await localStore.getBlock(manifestCid)).tryGet()
|
||||||
localManifest = BlocksManifest.init(manifestBlock).get()
|
localManifest = BlocksManifest.init(manifestBlock).tryGet()
|
||||||
|
|
||||||
check:
|
check:
|
||||||
manifest.len == localManifest.len
|
manifest.len == localManifest.len
|
||||||
|
@ -83,7 +83,7 @@ suite "Test Node":
|
||||||
|
|
||||||
test "Retrieve Data Stream":
|
test "Retrieve Data Stream":
|
||||||
var
|
var
|
||||||
manifest = BlocksManifest.init().get()
|
manifest = BlocksManifest.init().tryGet()
|
||||||
original: seq[byte]
|
original: seq[byte]
|
||||||
|
|
||||||
while (
|
while (
|
||||||
|
@ -91,7 +91,7 @@ suite "Test Node":
|
||||||
chunk.len > 0):
|
chunk.len > 0):
|
||||||
|
|
||||||
let
|
let
|
||||||
blk = bt.Block.init(chunk).get()
|
blk = bt.Block.init(chunk).tryGet()
|
||||||
|
|
||||||
original &= chunk
|
original &= chunk
|
||||||
check await localStore.putBlock(blk)
|
check await localStore.putBlock(blk)
|
||||||
|
@ -99,8 +99,8 @@ suite "Test Node":
|
||||||
|
|
||||||
let
|
let
|
||||||
manifestBlock = bt.Block.init(
|
manifestBlock = bt.Block.init(
|
||||||
manifest.encode().get(),
|
manifest.encode().tryGet(),
|
||||||
codec = ManifestCodec).get()
|
codec = ManifestCodec).tryGet()
|
||||||
|
|
||||||
check await localStore.putBlock(manifestBlock)
|
check await localStore.putBlock(manifestBlock)
|
||||||
|
|
||||||
|
@ -124,7 +124,7 @@ suite "Test Node":
|
||||||
test "Retrieve One Block":
|
test "Retrieve One Block":
|
||||||
let
|
let
|
||||||
testString = "Block 1"
|
testString = "Block 1"
|
||||||
blk = bt.Block.init(testString.toBytes).get()
|
blk = bt.Block.init(testString.toBytes).tryGet()
|
||||||
|
|
||||||
var
|
var
|
||||||
stream = BufferStream.new()
|
stream = BufferStream.new()
|
||||||
|
|
Loading…
Reference in New Issue