2020-08-31 03:32:41 +00:00
|
|
|
{.used.}
|
2020-08-27 02:44:09 +00:00
|
|
|
|
2020-08-31 03:32:41 +00:00
|
|
|
import
|
2022-11-23 09:08:00 +00:00
|
|
|
std/options,
|
2022-11-08 15:10:23 +00:00
|
|
|
testutils/unittests,
|
2022-11-09 17:50:18 +00:00
|
|
|
chronos,
|
|
|
|
chronicles,
|
2022-08-02 11:14:13 +00:00
|
|
|
libp2p/crypto/crypto
|
2022-07-25 11:01:37 +00:00
|
|
|
import
|
2023-08-09 17:11:50 +00:00
|
|
|
../../../waku/node/peer_manager,
|
|
|
|
../../../waku/waku_core,
|
|
|
|
../../../waku/waku_store,
|
|
|
|
../../../waku/waku_store/client,
|
2023-01-27 13:31:58 +00:00
|
|
|
../testlib/common,
|
2023-04-05 14:01:51 +00:00
|
|
|
../testlib/wakucore
|
2020-08-27 02:44:09 +00:00
|
|
|
|
2022-11-21 10:16:57 +00:00
|
|
|
proc newTestWakuStore(switch: Switch, handler: HistoryQueryHandler): Future[WakuStore] {.async.} =
|
|
|
|
let
|
|
|
|
peerManager = PeerManager.new(switch)
|
|
|
|
proto = WakuStore.new(peerManager, rng, handler)
|
|
|
|
|
|
|
|
await proto.start()
|
|
|
|
switch.mount(proto)
|
|
|
|
|
|
|
|
return proto
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
proc newTestWakuStoreClient(switch: Switch): WakuStoreClient =
|
2023-05-25 15:34:34 +00:00
|
|
|
let peerManager = PeerManager.new(switch)
|
2022-11-23 09:08:00 +00:00
|
|
|
WakuStoreClient.new(peerManager, rng)
|
2022-10-20 16:09:40 +00:00
|
|
|
|
|
|
|
|
2022-11-21 10:16:57 +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)]()
|
2023-05-25 15:34:34 +00:00
|
|
|
|
|
|
|
let queryHandler = proc(req: HistoryQuery): Future[HistoryResult] {.async, gcsafe.} =
|
2022-11-21 10:16:57 +00:00
|
|
|
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)]()
|
2023-05-25 15:34:34 +00:00
|
|
|
let queryHandler = proc(req: HistoryQuery): Future[HistoryResult] {.async, gcsafe.} =
|
2022-11-21 10:16:57 +00:00
|
|
|
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
|
2023-08-17 12:11:18 +00:00
|
|
|
await allFutures(serverSwitch.stop(), clientSwitch.stop())
|