nwaku/tests/v2/test_message_store_queue_pa...

403 lines
10 KiB
Nim

{.used.}
import
std/[options, sequtils, algorithm],
testutils/unittests,
libp2p/protobuf/minprotobuf
import
../../waku/v2/node/message_store/queue_store/queue_store {.all.},
../../waku/v2/node/message_store/queue_store/index,
../../waku/v2/protocol/waku_store,
../../waku/v2/protocol/waku_message,
../../waku/v2/utils/time,
./testlib/common
proc getTestStoreQueue(numMessages: int): StoreQueueRef =
let testStoreQueue = StoreQueueRef.new(numMessages)
var data {.noinit.}: array[32, byte]
for x in data.mitems: x = 1
for i in 0..<numMessages:
let msg = IndexedWakuMessage(
msg: WakuMessage(payload: @[byte i], timestamp: Timestamp(i)),
index: Index(
receiverTime: Timestamp(i),
senderTime: Timestamp(i),
digest: MessageDigest(data: data)
)
)
discard testStoreQueue.add(msg)
return testStoreQueue
procSuite "Queue store - pagination":
let store = getTestStoreQueue(10)
let
indexList: seq[Index] = toSeq(store.fwdIterator()).mapIt(it[0])
msgList: seq[WakuMessage] = toSeq(store.fwdIterator()).mapIt(it[1].msg)
test "Forward pagination - normal pagination":
## Given
let
pageSize: uint64 = 2
cursor: Option[Index] = some(indexList[3])
forward: bool = true
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let data = page.tryGet().mapIt(it[1])
check:
data.len == 2
data == msgList[4..5]
test "Forward pagination - initial pagination request with an empty cursor":
## Given
let
pageSize: uint64 = 2
cursor: Option[Index] = none(Index)
forward: bool = true
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let data = page.tryGet().mapIt(it[1])
check:
data.len == 2
data == msgList[0..1]
test "Forward pagination - initial pagination request with an empty cursor to fetch the entire history":
## Given
let
pageSize: uint64 = 13
cursor: Option[Index] = none(Index)
forward: bool = true
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let data = page.tryGet().mapIt(it[1])
check:
data.len == 10
data == msgList[0..9]
test "Forward pagination - empty msgList":
## Given
let store = getTestStoreQueue(0)
let
pageSize: uint64 = 2
cursor: Option[Index] = none(Index)
forward: bool = true
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let data = page.tryGet().mapIt(it[1])
check:
data.len == 0
test "Forward pagination - page size larger than the remaining messages":
## Given
let
pageSize: uint64 = 10
cursor: Option[Index] = some(indexList[3])
forward: bool = true
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let data = page.tryGet().mapIt(it[1])
check:
data.len == 6
data == msgList[4..9]
test "Forward pagination - page size larger than the maximum allowed page size":
## Given
let
pageSize: uint64 = MaxPageSize + 1
cursor: Option[Index] = some(indexList[3])
forward: bool = true
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let data = page.tryGet().mapIt(it[1])
check:
uint64(data.len) <= MaxPageSize
test "Forward pagination - cursor pointing to the end of the message list":
## Given
let
pageSize: uint64 = 10
cursor: Option[Index] = some(indexList[9])
forward: bool = true
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let data = page.tryGet().mapIt(it[1])
check:
data.len == 0
test "Forward pagination - invalid cursor":
## Given
let msg = fakeWakuMessage(payload= @[byte 10])
let index = HistoryCursor(
pubsubTopic: DefaultPubsubTopic,
senderTime: msg.timestamp,
storeTime: msg.timestamp,
digest: computeDigest(msg)
).toIndex()
let
pageSize: uint64 = 10
cursor: Option[Index] = some(index)
forward: bool = true
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let error = page.tryError()
check:
error == StoreQueueErrorKind.INVALID_CURSOR
test "Forward pagination - initial paging query over a message list with one message":
## Given
let store = getTestStoreQueue(1)
let
pageSize: uint64 = 10
cursor: Option[Index] = none(Index)
forward: bool = true
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let data = page.tryGet().mapIt(it[1])
check:
data.len == 1
test "Forward pagination - pagination over a message list with one message":
## Given
let store = getTestStoreQueue(1)
let
pageSize: uint64 = 10
cursor: Option[Index] = some(indexList[0])
forward: bool = true
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let data = page.tryGet().mapIt(it[1])
check:
data.len == 0
test "Forward pagination - with pradicate":
## Given
let
pageSize: uint64 = 3
cursor: Option[Index] = none(Index)
forward = true
proc onlyEvenTimes(i: IndexedWakuMessage): bool = i.msg.timestamp.int64 mod 2 == 0
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor, predicate=onlyEvenTimes)
## Then
let data = page.tryGet().mapIt(it[1])
check:
data.mapIt(it.timestamp.int) == @[0, 2, 4]
test "Backward pagination - normal pagination":
## Given
let
pageSize: uint64 = 2
cursor: Option[Index] = some(indexList[3])
forward: bool = false
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let data = page.tryGet().mapIt(it[1])
check:
data == msgList[1..2].reversed
test "Backward pagination - empty msgList":
## Given
let store = getTestStoreQueue(0)
let
pageSize: uint64 = 2
cursor: Option[Index] = none(Index)
forward: bool = false
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let data = page.tryGet().mapIt(it[1])
check:
data.len == 0
test "Backward pagination - initial pagination request with an empty cursor":
## Given
let
pageSize: uint64 = 2
cursor: Option[Index] = none(Index)
forward: bool = false
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let data = page.tryGet().mapIt(it[1])
check:
data.len == 2
data == msgList[8..9].reversed
test "Backward pagination - initial pagination request with an empty cursor to fetch the entire history":
## Given
let
pageSize: uint64 = 13
cursor: Option[Index] = none(Index)
forward: bool = false
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let data = page.tryGet().mapIt(it[1])
check:
data.len == 10
data == msgList[0..9].reversed
test "Backward pagination - page size larger than the remaining messages":
## Given
let
pageSize: uint64 = 5
cursor: Option[Index] = some(indexList[3])
forward: bool = false
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let data = page.tryGet().mapIt(it[1])
check:
data == msgList[0..2].reversed
test "Backward pagination - page size larger than the Maximum allowed page size":
## Given
let
pageSize: uint64 = MaxPageSize + 1
cursor: Option[Index] = some(indexList[3])
forward: bool = false
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let data = page.tryGet().mapIt(it[1])
check:
uint64(data.len) <= MaxPageSize
test "Backward pagination - cursor pointing to the begining of the message list":
## Given
let
pageSize: uint64 = 5
cursor: Option[Index] = some(indexList[0])
forward: bool = false
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let data = page.tryGet().mapIt(it[1])
check:
data.len == 0
test "Backward pagination - invalid cursor":
## Given
let msg = fakeWakuMessage(payload= @[byte 10])
let index = HistoryCursor(
pubsubTopic: DefaultPubsubTopic,
senderTime: msg.timestamp,
storeTime: msg.timestamp,
digest: computeDigest(msg)
).toIndex()
let
pageSize: uint64 = 2
cursor: Option[Index] = some(index)
forward: bool = false
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let error = page.tryError()
check:
error == StoreQueueErrorKind.INVALID_CURSOR
test "Backward pagination - initial paging query over a message list with one message":
## Given
let store = getTestStoreQueue(1)
let
pageSize: uint64 = 10
cursor: Option[Index] = none(Index)
forward: bool = false
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let data = page.tryGet().mapIt(it[1])
check:
data.len == 1
test "Backward pagination - paging query over a message list with one message":
## Given
let store = getTestStoreQueue(1)
let
pageSize: uint64 = 10
cursor: Option[Index] = some(indexList[0])
forward: bool = false
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor)
## Then
let data = page.tryGet().mapIt(it[1])
check:
data.len == 0
test "Backward pagination - with predicate":
## Given
let
pageSize: uint64 = 3
cursor: Option[Index] = none(Index)
forward = false
proc onlyOddTimes(i: IndexedWakuMessage): bool = i.msg.timestamp.int64 mod 2 != 0
## When
let page = store.getPage(pageSize=pageSize, forward=forward, cursor=cursor, predicate=onlyOddTimes)
## Then
let data = page.tryGet().mapIt(it[1])
check:
data.mapIt(it.timestamp.int) == @[5, 7,9].reversed