2022-03-02 16:30:42 +00:00
|
|
|
import std/strutils
|
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/stew/byteutils
|
|
|
|
import pkg/questionable/results
|
2022-05-19 19:56:03 +00:00
|
|
|
import pkg/codex/stores/cachestore
|
|
|
|
import pkg/codex/chunker
|
2022-03-02 16:30:42 +00:00
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
import ./commonstoretests
|
|
|
|
|
2022-03-02 16:30:42 +00:00
|
|
|
import ../helpers
|
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
checksuite "Cache Store":
|
2022-03-02 16:30:42 +00:00
|
|
|
var
|
|
|
|
newBlock, newBlock1, newBlock2, newBlock3: Block
|
|
|
|
store: CacheStore
|
|
|
|
|
|
|
|
setup:
|
2022-03-18 19:50:53 +00:00
|
|
|
newBlock = Block.new("New Kids on the Block".toBytes()).tryGet()
|
|
|
|
newBlock1 = Block.new("1".repeat(100).toBytes()).tryGet()
|
|
|
|
newBlock2 = Block.new("2".repeat(100).toBytes()).tryGet()
|
|
|
|
newBlock3 = Block.new("3".repeat(100).toBytes()).tryGet()
|
2022-03-02 16:30:42 +00:00
|
|
|
store = CacheStore.new()
|
|
|
|
|
|
|
|
test "constructor":
|
|
|
|
# cache size cannot be smaller than chunk size
|
|
|
|
expect ValueError:
|
|
|
|
discard CacheStore.new(cacheSize = 1, chunkSize = 2)
|
|
|
|
|
|
|
|
store = CacheStore.new(cacheSize = 100, chunkSize = 1)
|
2023-07-06 23:23:27 +00:00
|
|
|
check store.currentSize == 0'nb
|
2022-12-03 00:00:55 +00:00
|
|
|
|
2022-03-02 16:30:42 +00:00
|
|
|
store = CacheStore.new(@[newBlock1, newBlock2, newBlock3])
|
2023-07-06 23:23:27 +00:00
|
|
|
check store.currentSize == 300'nb
|
2022-03-02 16:30:42 +00:00
|
|
|
|
|
|
|
# initial cache blocks total more than cache size, currentSize should
|
|
|
|
# never exceed max cache size
|
|
|
|
store = CacheStore.new(
|
|
|
|
blocks = @[newBlock1, newBlock2, newBlock3],
|
|
|
|
cacheSize = 200,
|
|
|
|
chunkSize = 1)
|
2023-07-06 23:23:27 +00:00
|
|
|
check store.currentSize == 200'nb
|
2022-03-02 16:30:42 +00:00
|
|
|
|
|
|
|
# cache size cannot be less than chunks size
|
|
|
|
expect ValueError:
|
|
|
|
discard CacheStore.new(
|
|
|
|
cacheSize = 99,
|
|
|
|
chunkSize = 100)
|
|
|
|
|
|
|
|
test "putBlock":
|
2022-07-28 00:39:17 +00:00
|
|
|
(await store.putBlock(newBlock1)).tryGet()
|
|
|
|
check (await store.hasBlock(newBlock1.cid)).tryGet()
|
2022-03-02 16:30:42 +00:00
|
|
|
|
|
|
|
# block size bigger than entire cache
|
|
|
|
store = CacheStore.new(cacheSize = 99, chunkSize = 98)
|
2022-07-28 00:39:17 +00:00
|
|
|
(await store.putBlock(newBlock1)).tryGet()
|
|
|
|
check not (await store.hasBlock(newBlock1.cid)).tryGet()
|
2022-03-02 16:30:42 +00:00
|
|
|
|
|
|
|
# block being added causes removal of LRU block
|
|
|
|
store = CacheStore.new(
|
|
|
|
@[newBlock1, newBlock2, newBlock3],
|
|
|
|
cacheSize = 200,
|
|
|
|
chunkSize = 1)
|
|
|
|
check:
|
2022-07-28 00:39:17 +00:00
|
|
|
not (await store.hasBlock(newBlock1.cid)).tryGet()
|
|
|
|
(await store.hasBlock(newBlock2.cid)).tryGet()
|
|
|
|
(await store.hasBlock(newBlock2.cid)).tryGet()
|
2023-07-06 23:23:27 +00:00
|
|
|
store.currentSize.int == newBlock2.data.len + newBlock3.data.len # 200
|
2022-03-02 16:30:42 +00:00
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
commonBlockStoreTests(
|
|
|
|
"Cache", proc: BlockStore =
|
|
|
|
BlockStore(CacheStore.new(cacheSize = 500, chunkSize = 1)))
|