logos-messaging-nim/tests/v2/test_waku_store.nim
Sanaz Taheri Boshrooyeh ea5f9993a7
Pagination feature/indexing waku messages (#233)
* changes the digest type to MDigest[256] and modifies the computeIndex

* fixes formatting issue

* adds the pagination with its tests

stores and retrieves IndexedWakuMessage


adds the paginate proc


adds the paginate function


fixes some formatting issues


minor


edits indentation and fixes a bug


removes unused imports


minor


fixes indentations and adds a new testcase


adds indexedWakuMessageComparison


adds `==` proc for IndexedWakuMessage


separates the comparison of index and indexed waku messages


adds testcases for the Index comparison and IndexedWakuMessage comparison


WIP


WIP: adds an decoder for Index


removes an unnecessary imports


WIP


adds findIndex() proc


removes the equality check '==' for IndexedWakuMessages


edits the code format and adds the pagination test


edits paginate() proc to work on a copy of the input list


deletes unnecessary echo


adds the boundary calculations for forward and backward pagination


adds test cases for the page boundaries


tests corner cases for the queried cursor and pagesize


minor


adds some comments


adds a proc to extract WakuMessages from a list of IndexedWakuMessages


integrates pagination into the findMessages proc


adds some comments


changes paginate to paginateWithIndex


removes some echos


modifies paginateWithIndex to handle invalid cursors


adds test case for an invalid cursor


WIP: adds a `$` proc for IndexedWakuMessages


adds some debugging message prints 


adds an integration test for handling query with pagination

* fixes a type mismatch issue in the min proc

* replaces boolean direction with their enums and updates contentTopics

* adds the unit test for the sorting of the indexed waku messages

* fixes a flaky test

* fixes a flaky test

* removes index equality check proc

* handles an initial query with an empty cursor

* adds test for the initial query

* adds integration test for pagination

* adds a test for empty message list

* adds comments and fixes an issue

* adds comments

* code cleanup

* removes the content topic validation check

* resolves the errors related to the windows CI tests

* Update waku/protocol/v2/waku_store.nim

Co-authored-by: Oskar Thorén <ot@oskarthoren.com>

* Update waku/protocol/v2/waku_store.nim

Co-authored-by: Oskar Thorén <ot@oskarthoren.com>

* Update tests/v2/test_waku_pagination.nim

Co-authored-by: Oskar Thorén <ot@oskarthoren.com>

* Update waku/protocol/v2/waku_store.nim

Co-authored-by: Oskar Thorén <ot@oskarthoren.com>

* Update waku/protocol/v2/waku_store.nim

Co-authored-by: Oskar Thorén <ot@oskarthoren.com>

* Update waku/protocol/v2/waku_store.nim

Co-authored-by: Oskar Thorén <ot@oskarthoren.com>

* changes the output type of findIndex to Option

* Update tests/v2/test_waku_pagination.nim

Co-authored-by: Dean Eigenmann <7621705+decanus@users.noreply.github.com>

* Update tests/v2/test_waku_pagination.nim

Co-authored-by: Dean Eigenmann <7621705+decanus@users.noreply.github.com>

* Update tests/v2/test_waku_pagination.nim

Co-authored-by: Dean Eigenmann <7621705+decanus@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Dean Eigenmann <7621705+decanus@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Dean Eigenmann <7621705+decanus@users.noreply.github.com>

* adds some comments

* fixes an indentation issue

* some code modification for array initialization

* Apply suggestions from code review

Co-authored-by: Oskar Thorén <ot@oskarthoren.com>
Co-authored-by: Dean Eigenmann <7621705+decanus@users.noreply.github.com>

* does some code reorganizations and clean up

* CreateSampleList to createSampleList

* replaces a byte array literal initialization with a for loop

* relocates indexedWakuMessageComparison and indexComparison

* minor

Co-authored-by: Oskar Thorén <ot@oskarthoren.com>
Co-authored-by: Dean Eigenmann <7621705+decanus@users.noreply.github.com>
2020-11-09 12:48:09 +08:00

315 lines
11 KiB
Nim

{.used.}
import
std/[unittest, options, tables, sets],
chronos, chronicles,
libp2p/switch,
libp2p/protobuf/minprotobuf,
libp2p/stream/[bufferstream, connection],
libp2p/crypto/crypto,
libp2p/protocols/pubsub/rpc/message,
libp2p/multistream,
libp2p/transports/transport,
libp2p/transports/tcptransport,
../../waku/protocol/v2/[waku_store, message_notifier],
../../waku/node/v2/waku_types,
../test_helpers, ./utils
procSuite "Waku Store":
asyncTest "handle query":
let
key = PrivateKey.random(ECDSA, rng[]).get()
peer = PeerInfo.init(key)
topic = ContentTopic(1)
msg = WakuMessage(payload: @[byte 1, 2, 3], contentTopic: topic)
msg2 = WakuMessage(payload: @[byte 1, 2, 3], contentTopic: ContentTopic(2))
var dialSwitch = newStandardSwitch()
discard await dialSwitch.start()
var listenSwitch = newStandardSwitch(some(key))
discard await listenSwitch.start()
let
proto = WakuStore.init(dialSwitch, crypto.newRng())
subscription = proto.subscription()
rpc = HistoryQuery(topics: @[topic])
proto.setPeer(listenSwitch.peerInfo)
var subscriptions = newTable[string, MessageNotificationSubscription]()
subscriptions["test"] = subscription
listenSwitch.mount(proto)
await subscriptions.notify("foo", msg)
await subscriptions.notify("foo", msg2)
var completionFut = newFuture[bool]()
proc handler(response: HistoryResponse) {.gcsafe, closure.} =
check:
response.messages.len() == 1
response.messages[0] == msg
completionFut.complete(true)
await proto.query(rpc, handler)
check:
(await completionFut.withTimeout(5.seconds)) == true
asyncTest "handle query with forward pagination":
let
key = PrivateKey.random(ECDSA, rng[]).get()
peer = PeerInfo.init(key)
var
msgList = @[WakuMessage(payload: @[byte 0], contentTopic: ContentTopic(2)),
WakuMessage(payload: @[byte 1],contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 2],contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 3],contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 4],contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 5],contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 6],contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 7],contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 8],contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 9],contentTopic: ContentTopic(2))]
var dialSwitch = newStandardSwitch()
discard await dialSwitch.start()
var listenSwitch = newStandardSwitch(some(key))
discard await listenSwitch.start()
let
proto = WakuStore.init(dialSwitch, crypto.newRng())
subscription = proto.subscription()
rpc = HistoryQuery(topics: @[ContentTopic(1)], pagingInfo: PagingInfo(pageSize: 2, direction: PagingDirection.FORWARD) )
proto.setPeer(listenSwitch.peerInfo)
var subscriptions = newTable[string, MessageNotificationSubscription]()
subscriptions["test"] = subscription
listenSwitch.mount(proto)
for wakuMsg in msgList:
await subscriptions.notify("foo", wakuMsg)
var completionFut = newFuture[bool]()
proc handler(response: HistoryResponse) {.gcsafe, closure.} =
check:
response.messages.len() == 2
response.pagingInfo.pageSize == 2
response.pagingInfo.direction == PagingDirection.FORWARD
response.pagingInfo.cursor != Index()
completionFut.complete(true)
await proto.query(rpc, handler)
check:
(await completionFut.withTimeout(5.seconds)) == true
asyncTest "handle query with backward pagination":
let
key = PrivateKey.random(ECDSA, rng[]).get()
peer = PeerInfo.init(key)
var
msgList = @[WakuMessage(payload: @[byte 0], contentTopic: ContentTopic(2)),
WakuMessage(payload: @[byte 1],contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 2],contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 3],contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 4],contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 5],contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 6],contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 7],contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 8],contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 9],contentTopic: ContentTopic(2))]
var dialSwitch = newStandardSwitch()
discard await dialSwitch.start()
var listenSwitch = newStandardSwitch(some(key))
discard await listenSwitch.start()
let
proto = WakuStore.init(dialSwitch, crypto.newRng())
subscription = proto.subscription()
proto.setPeer(listenSwitch.peerInfo)
var subscriptions = newTable[string, MessageNotificationSubscription]()
subscriptions["test"] = subscription
listenSwitch.mount(proto)
for wakuMsg in msgList:
await subscriptions.notify("foo", wakuMsg)
var completionFut = newFuture[bool]()
proc handler(response: HistoryResponse) {.gcsafe, closure.} =
check:
response.messages.len() == 2
response.pagingInfo.pageSize == 2
response.pagingInfo.direction == PagingDirection.BACKWARD
response.pagingInfo.cursor != Index()
completionFut.complete(true)
let rpc = HistoryQuery(topics: @[ContentTopic(1)], pagingInfo: PagingInfo(pageSize: 2, direction: PagingDirection.BACKWARD) )
await proto.query(rpc, handler)
check:
(await completionFut.withTimeout(5.seconds)) == true
asyncTest "handle queries with no pagination":
let
key = PrivateKey.random(ECDSA, rng[]).get()
peer = PeerInfo.init(key)
var
msgList = @[WakuMessage(payload: @[byte 0], contentTopic: ContentTopic(2)),
WakuMessage(payload: @[byte 1], contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 2], contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 3], contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 4], contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 5], contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 6], contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 7], contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 8], contentTopic: ContentTopic(1)),
WakuMessage(payload: @[byte 9], contentTopic: ContentTopic(2))]
var dialSwitch = newStandardSwitch()
discard await dialSwitch.start()
var listenSwitch = newStandardSwitch(some(key))
discard await listenSwitch.start()
let
proto = WakuStore.init(dialSwitch, crypto.newRng())
subscription = proto.subscription()
proto.setPeer(listenSwitch.peerInfo)
var subscriptions = newTable[string, MessageNotificationSubscription]()
subscriptions["test"] = subscription
listenSwitch.mount(proto)
for wakuMsg in msgList:
await subscriptions.notify("foo", wakuMsg)
var completionFut = newFuture[bool]()
proc handler(response: HistoryResponse) {.gcsafe, closure.} =
check:
response.messages.len() == 8
response.pagingInfo == PagingInfo()
completionFut.complete(true)
let rpc = HistoryQuery(topics: @[ContentTopic(1)] )
await proto.query(rpc, handler)
check:
(await completionFut.withTimeout(5.seconds)) == true
test "Index Protobuf encoder/decoder test":
let
index = computeIndex(WakuMessage(payload: @[byte 1], contentTopic: ContentTopic(1)))
pb = index.encode()
decodedIndex = Index.init(pb.buffer)
check:
# the fields of decodedIndex must be the same as the original index
decodedIndex.isErr == false
decodedIndex.value == index
let
emptyIndex = Index()
epb = emptyIndex.encode()
decodedEmptyIndex = Index.init(epb.buffer)
check:
# check the correctness of init and encode for an empty Index
decodedEmptyIndex.isErr == false
decodedEmptyIndex.value == emptyIndex
test "PagingDirection Protobuf encod/init test":
let
pagingDirection = PagingDirection.BACKWARD
pb = pagingDirection.encode()
decodedPagingDirection = PagingDirection.init(pb.buffer)
check:
# the decodedPagingDirection must be the same as the original pagingDirection
decodedPagingDirection.isErr == false
decodedPagingDirection.value == pagingDirection
test "PagingInfo Protobuf encod/init test":
let
index = computeIndex(WakuMessage(payload: @[byte 1], contentTopic: ContentTopic(1)))
pagingInfo = PagingInfo(pageSize: 1, cursor: index, direction: PagingDirection.BACKWARD)
pb = pagingInfo.encode()
decodedPagingInfo = PagingInfo.init(pb.buffer)
check:
# the fields of decodedPagingInfo must be the same as the original pagingInfo
decodedPagingInfo.isErr == false
decodedPagingInfo.value == pagingInfo
let
emptyPagingInfo = PagingInfo()
epb = emptyPagingInfo.encode()
decodedEmptyPagingInfo = PagingInfo.init(epb.buffer)
check:
# check the correctness of init and encode for an empty PagingInfo
decodedEmptyPagingInfo.isErr == false
decodedEmptyPagingInfo.value == emptyPagingInfo
test "HistoryQuery Protobuf encod/init test":
let
index = computeIndex(WakuMessage(payload: @[byte 1], contentTopic: ContentTopic(1)))
pagingInfo = PagingInfo(pageSize: 1, cursor: index, direction: PagingDirection.BACKWARD)
query=HistoryQuery(topics: @[ContentTopic(1)], pagingInfo: pagingInfo)
pb = query.encode()
decodedQuery = HistoryQuery.init(pb.buffer)
check:
# the fields of decoded query decodedQuery must be the same as the original query query
decodedQuery.isErr == false
decodedQuery.value == query
let
emptyQuery=HistoryQuery()
epb = emptyQuery.encode()
decodedEmptyQuery = HistoryQuery.init(epb.buffer)
check:
# check the correctness of init and encode for an empty HistoryQuery
decodedEmptyQuery.isErr == false
decodedEmptyQuery.value == emptyQuery
test "HistoryResponse Protobuf encod/init test":
let
wm = WakuMessage(payload: @[byte 1], contentTopic: ContentTopic(1))
index = computeIndex(wm)
pagingInfo = PagingInfo(pageSize: 1, cursor: index, direction: PagingDirection.BACKWARD)
res = HistoryResponse(messages: @[wm], pagingInfo:pagingInfo)
pb = res.encode()
decodedRes = HistoryResponse.init(pb.buffer)
check:
# the fields of decoded response decodedRes must be the same as the original response res
decodedRes.isErr == false
decodedRes.value == res
let
emptyRes=HistoryResponse()
epb = emptyRes.encode()
decodedEmptyRes = HistoryResponse.init(epb.buffer)
check:
# check the correctness of init and encode for an empty HistoryResponse
decodedEmptyRes.isErr == false
decodedEmptyRes.value == emptyRes