Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SionoiS committed Jan 13, 2025
1 parent 078f36b commit 7c8ab55
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tests/waku_store_sync/test_storage.nim
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,61 @@ suite "Waku Sync Storage":
check:
toSend == diffs

test "insert new element":
var rng = initRand()

let storage = SeqStorage.new(10)

let element1 = ID(time: Timestamp(1000), fingerprint: randomHash(rng))
let element2 = ID(time: Timestamp(2000), fingerprint: randomHash(rng))

let res1 = storage.insert(element1)
assert res1.isOk(), $res1.error
let count1 = storage.length()

let res2 = storage.insert(element2)
assert res2.isOk(), $res2.error
let count2 = storage.length()

check:
count1 == 1
count2 == 2

test "insert duplicate":
var rng = initRand()

let element = ID(time: Timestamp(1000), fingerprint: randomHash(rng))

let storage = SeqStorage.new(@[element])

let res = storage.insert(element)

check:
res.isErr() == true

test "prune elements":
var rng = initRand()
let count = 1000
var elements = newSeqOfCap[ID](count)

for i in 0 ..< count:
let id = ID(time: Timestamp(i), fingerprint: randomHash(rng))

elements.add(id)

let storage = SeqStorage.new(elements)

let beforeCount = storage.length()

let pruned = storage.prune(Timestamp(500))

let afterCount = storage.length()

check:
beforeCount == 1000
pruned == 500
afterCount == 500

## disabled tests are rough benchmark
#[ test "10M fingerprint":
var rng = initRand()
Expand Down
3 changes: 3 additions & 0 deletions waku/waku_store_sync/storage/seq_storage.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ type SeqStorage* = ref object of SyncStorage
partitionCount: int
lengthThreshold: int

method length*(self: SeqStorage): int =
return self.elements.len

method insert*(self: SeqStorage, element: ID): Result[void, string] {.raises: [].} =
let idx = self.elements.lowerBound(element, common.cmp)

Expand Down
3 changes: 3 additions & 0 deletions waku/waku_store_sync/storage/storage.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ method processPayload*(
hashToRecv: var seq[Fingerprint],
): SyncPayload {.base, gcsafe, raises: [].} =
discard

method length*(self: SyncStorage): int {.base, gcsafe, raises: [].} =
discard

0 comments on commit 7c8ab55

Please sign in to comment.