nim-codex/tests/codex/stores/testkeyutils.nim

94 lines
2.7 KiB
Nim
Raw Normal View History

Blockstore maintenance (#347) * setting up * Implements timer utility * applies async and cancellation to timer loop * Sets up mocktimer and mockblockstore to set up first maintenance module test * wip: first test that calls blockChecker * wip: adding user type to timer callback * Chronicles doesn't like type-arguments? Disables logging in timer module for now * Implementing block check test for blockMaintainer module * Sets up additional tests for blockmaintainer * Removes generic from timer module. Implements numberOfBlocks per interval in blockMaintainer. * Implements blockMaintainer * Sets up tests for blockChecker * Some comments by Mark * Cleanup repostore tests * Setting up the use of std/times for block TTL tracking * repostore adds expiration timestamp * Defaults the repostore clock to the system clock * Applies updates to repostore interface. * Implements retrieving of block expiration information from repostore * Sets up tests for maintenance module behavior * Implements block maintenance module * Wires maintenance module into codex. Sets up integration tests for block expiration * Sets up test for missing behavior: removing timestamp metadata on block delete * Implements removing of expiration metadata in repoStore * Fixes integration tests for block expiration * Adds block expiration tests to integration test run * Handled some comments by Dmitriy * Review comment by Dmitriy: Removes seq[cid] from runBlockCheck * Review comment by Dmitriy: Moves key formatting methods to keyutils. * Review comment by Dmitriy: Encodes durations using chronos * Fixes conversion of TTL type in conf. * Review comments by Dmitriy * Adds unit tests for keyUtils. * Adds test coverage for exception in maintenance module
2023-03-08 15:04:54 +00:00
## Nim-Codex
## Copyright (c) 2023 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
## at your option.
## This file may not be copied, modified, or distributed except according to
## those terms.
import std/random
import std/sequtils
import pkg/chronos
import pkg/asynctest
import pkg/questionable
import pkg/questionable/results
import pkg/codex/blocktype as bt
import pkg/codex/stores/repostore
import pkg/codex/clock
import ../helpers/mocktimer
import ../helpers/mockrepostore
import ../helpers/mockclock
import ../examples
import codex/namespaces
import codex/stores/keyutils
proc createManifestCid(): ?!Cid =
let
length = rand(4096)
bytes = newSeqWith(length, rand(uint8))
mcodec = multiCodec("sha2-256")
codec = multiCodec("dag-pb")
version = CIDv1
let hash = ? MultiHash.digest($mcodec, bytes).mapFailure
let cid = ? Cid.init(version, codec, hash).mapFailure
return success cid
checksuite "KeyUtils":
Blockstore maintenance (#347) * setting up * Implements timer utility * applies async and cancellation to timer loop * Sets up mocktimer and mockblockstore to set up first maintenance module test * wip: first test that calls blockChecker * wip: adding user type to timer callback * Chronicles doesn't like type-arguments? Disables logging in timer module for now * Implementing block check test for blockMaintainer module * Sets up additional tests for blockmaintainer * Removes generic from timer module. Implements numberOfBlocks per interval in blockMaintainer. * Implements blockMaintainer * Sets up tests for blockChecker * Some comments by Mark * Cleanup repostore tests * Setting up the use of std/times for block TTL tracking * repostore adds expiration timestamp * Defaults the repostore clock to the system clock * Applies updates to repostore interface. * Implements retrieving of block expiration information from repostore * Sets up tests for maintenance module behavior * Implements block maintenance module * Wires maintenance module into codex. Sets up integration tests for block expiration * Sets up test for missing behavior: removing timestamp metadata on block delete * Implements removing of expiration metadata in repoStore * Fixes integration tests for block expiration * Adds block expiration tests to integration test run * Handled some comments by Dmitriy * Review comment by Dmitriy: Removes seq[cid] from runBlockCheck * Review comment by Dmitriy: Moves key formatting methods to keyutils. * Review comment by Dmitriy: Encodes durations using chronos * Fixes conversion of TTL type in conf. * Review comments by Dmitriy * Adds unit tests for keyUtils. * Adds test coverage for exception in maintenance module
2023-03-08 15:04:54 +00:00
test "makePrefixKey should create block key":
let length = 6
let cid = Cid.example
let expectedPrefix = ($cid)[^length..^1]
let expectedPostfix = $cid
let key = !makePrefixKey(length, cid).option
let namespaces = key.namespaces
check:
namespaces.len == 4
namespaces[0].value == CodexRepoNamespace
namespaces[1].value == "blocks"
namespaces[2].value == expectedPrefix
namespaces[3].value == expectedPostfix
test "makePrefixKey should create manifest key":
let length = 6
let cid = !createManifestCid().option
let expectedPrefix = ($cid)[^length..^1]
let expectedPostfix = $cid
let key = !makePrefixKey(length, cid).option
let namespaces = key.namespaces
check:
namespaces.len == 4
namespaces[0].value == CodexRepoNamespace
namespaces[1].value == "manifests"
namespaces[2].value == expectedPrefix
namespaces[3].value == expectedPostfix
test "createBlockExpirationMetadataKey should create block TTL key":
let cid = Cid.example
let key = !createBlockExpirationMetadataKey(cid).option
let namespaces = key.namespaces
check:
namespaces.len == 3
namespaces[0].value == CodexMetaNamespace
namespaces[1].value == "ttl"
namespaces[2].value == $cid
test "createBlockExpirationMetadataQueryKey should create key for all block TTL entries":
let key = !createBlockExpirationMetadataQueryKey().option
let namespaces = key.namespaces
check:
namespaces.len == 3
namespaces[0].value == CodexMetaNamespace
namespaces[1].value == "ttl"
namespaces[2].value == "*"