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
|
2021-03-26 08:49:51 +00:00
|
|
|
../../waku/v2/node/peer_manager/peer_manager,
|
2022-10-28 18:13:41 +00:00
|
|
|
../../waku/v2/protocol/waku_message,
|
|
|
|
../../waku/v2/protocol/waku_store,
|
|
|
|
../../waku/v2/protocol/waku_store/client,
|
2022-10-21 13:01:39 +00:00
|
|
|
./testlib/common,
|
|
|
|
./testlib/switch
|
2020-08-27 02:44:09 +00:00
|
|
|
|
|
|
|
|
2022-09-13 10:37:06 +00:00
|
|
|
|
2022-11-21 10:16:57 +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:08:00 +00:00
|
|
|
proc newTestWakuStoreClient(switch: Switch): WakuStoreClient =
|
2022-10-20 16:09:40 +00:00
|
|
|
let
|
|
|
|
peerManager = PeerManager.new(switch)
|
|
|
|
rng = crypto.newRng()
|
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)]()
|
|
|
|
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())
|