2020-12-21 11:45:07 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2022-11-23 09:53:04 +00:00
|
|
|
std/options,
|
2022-11-08 16:20:03 +00:00
|
|
|
testutils/unittests,
|
2022-11-09 18:51:06 +00:00
|
|
|
chronos,
|
|
|
|
chronicles,
|
2022-08-17 15:58:04 +00:00
|
|
|
libp2p/crypto/crypto
|
2022-08-01 11:31:00 +00:00
|
|
|
import
|
2021-03-26 09:14:23 +00:00
|
|
|
../../waku/v2/node/peer_manager/peer_manager,
|
2022-10-28 18:55:40 +00:00
|
|
|
../../waku/v2/protocol/waku_message,
|
|
|
|
../../waku/v2/protocol/waku_store,
|
|
|
|
../../waku/v2/protocol/waku_store/client,
|
2022-10-24 09:08:51 +00:00
|
|
|
./testlib/common,
|
|
|
|
./testlib/switch
|
2020-12-21 11:45:07 +00:00
|
|
|
|
|
|
|
|
2022-09-13 11:02:33 +00:00
|
|
|
|
2022-11-21 10:59:26 +00:00
|
|
|
proc newTestWakuStore(switch: Switch, handler: HistoryQueryHandler): Future[WakuStore] {.async.} =
|
|
|
|
let
|
|
|
|
peerManager = PeerManager.new(switch)
|
|
|
|
rng = crypto.newRng()
|
|
|
|
proto = WakuStore.new(peerManager, rng, handler)
|
|
|
|
|
|
|
|
await proto.start()
|
|
|
|
switch.mount(proto)
|
|
|
|
|
|
|
|
return proto
|
|
|
|
|
2022-11-23 09:53:04 +00:00
|
|
|
proc newTestWakuStoreClient(switch: Switch): WakuStoreClient =
|
2022-10-20 16:47:08 +00:00
|
|
|
let
|
|
|
|
peerManager = PeerManager.new(switch)
|
|
|
|
rng = crypto.newRng()
|
2022-11-23 09:53:04 +00:00
|
|
|
WakuStoreClient.new(peerManager, rng)
|
2022-10-20 16:47:08 +00:00
|
|
|
|
|
|
|
|
2022-11-21 10:59:26 +00:00
|
|
|
suite "Waku Store - query handler":
|
|
|
|
|
|
|
|
asyncTest "history query handler should be called":
|
|
|
|
## Setup
|
|
|
|
let
|
|
|
|
serverSwitch = newTestSwitch()
|
|
|
|
clientSwitch = newTestSwitch()
|
|
|
|
|
|
|
|
await allFutures(serverSwitch.start(), clientSwitch.start())
|
|
|
|
|
|
|
|
## Given
|
|
|
|
let serverPeerInfo = serverSwitch.peerInfo.toRemotePeerInfo()
|
|
|
|
|
|
|
|
let msg = fakeWakuMessage(contentTopic=DefaultContentTopic)
|
|
|
|
|
|
|
|
var queryHandlerFut = newFuture[(HistoryQuery)]()
|
|
|
|
let queryHandler = proc(req: HistoryQuery): HistoryResult =
|
|
|
|
queryHandlerFut.complete(req)
|
|
|
|
return ok(HistoryResponse(messages: @[msg]))
|
|
|
|
|
|
|
|
let
|
|
|
|
server = await newTestWakuStore(serverSwitch, handler=queryhandler)
|
|
|
|
client = newTestWakuStoreClient(clientSwitch)
|
|
|
|
|
|
|
|
let req = HistoryQuery(contentTopics: @[DefaultContentTopic], ascending: true)
|
|
|
|
|
|
|
|
## When
|
|
|
|
let queryRes = await client.query(req, peer=serverPeerInfo)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
not queryHandlerFut.failed()
|
|
|
|
queryRes.isOk()
|
|
|
|
|
|
|
|
let request = queryHandlerFut.read()
|
|
|
|
check:
|
|
|
|
request == req
|
|
|
|
|
|
|
|
let response = queryRes.tryGet()
|
|
|
|
check:
|
|
|
|
response.messages.len == 1
|
|
|
|
response.messages == @[msg]
|
|
|
|
|
|
|
|
## Cleanup
|
|
|
|
await allFutures(serverSwitch.stop(), clientSwitch.stop())
|
|
|
|
|
|
|
|
asyncTest "history query handler should be called and return an error":
|
|
|
|
## Setup
|
|
|
|
let
|
|
|
|
serverSwitch = newTestSwitch()
|
|
|
|
clientSwitch = newTestSwitch()
|
|
|
|
|
|
|
|
await allFutures(serverSwitch.start(), clientSwitch.start())
|
|
|
|
|
|
|
|
## Given
|
|
|
|
let serverPeerInfo = serverSwitch.peerInfo.toRemotePeerInfo()
|
|
|
|
|
|
|
|
var queryHandlerFut = newFuture[(HistoryQuery)]()
|
|
|
|
let queryHandler = proc(req: HistoryQuery): HistoryResult =
|
|
|
|
queryHandlerFut.complete(req)
|
|
|
|
return err(HistoryError(kind: HistoryErrorKind.BAD_REQUEST))
|
|
|
|
|
|
|
|
let
|
|
|
|
server = await newTestWakuStore(serverSwitch, handler=queryhandler)
|
|
|
|
client = newTestWakuStoreClient(clientSwitch)
|
|
|
|
|
|
|
|
let req = HistoryQuery(contentTopics: @[DefaultContentTopic], ascending: true)
|
|
|
|
|
|
|
|
## When
|
|
|
|
let queryRes = await client.query(req, peer=serverPeerInfo)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
not queryHandlerFut.failed()
|
|
|
|
queryRes.isErr()
|
|
|
|
|
|
|
|
let request = queryHandlerFut.read()
|
|
|
|
check:
|
|
|
|
request == req
|
|
|
|
|
|
|
|
let error = queryRes.tryError()
|
|
|
|
check:
|
|
|
|
error.kind == HistoryErrorKind.BAD_REQUEST
|
|
|
|
|
|
|
|
## Cleanup
|
|
|
|
await allFutures(serverSwitch.stop(), clientSwitch.stop())
|