Skip to content

Commit

Permalink
chore: extract test procedures and constants into a common module
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzo Delgado authored Oct 21, 2022
1 parent c19081b commit 9b59052
Show file tree
Hide file tree
Showing 19 changed files with 120 additions and 318 deletions.
10 changes: 2 additions & 8 deletions tests/v2/test_message_cache.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@ import
chronicles
import
../../waku/v2/protocol/waku_message,
../../waku/v2/node/message_cache
../../waku/v2/node/message_cache,
./testlib/common


proc fakeWakuMessage(payload = toBytes("TEST"), contentTopic = "test"): WakuMessage =
WakuMessage(
payload: payload,
contentTopic: contentTopic,
version: 1,
timestamp: 2022
)

type PubsubTopicString = string

Expand Down
8 changes: 2 additions & 6 deletions tests/v2/test_message_store_queue_index.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ import
import
../../waku/v2/protocol/waku_message,
../../waku/v2/utils/time,
../../waku/v2/node/storage/message/queue_store/index


const
DefaultPubsubTopic = "/waku/2/default-waku/proto"
DefaultContentTopic = ContentTopic("/waku/2/default-content/proto")
../../waku/v2/node/storage/message/queue_store/index,
./testlib/common


## Helpers
Expand Down
16 changes: 3 additions & 13 deletions tests/v2/test_message_store_queue_pagination.nim
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
{.used.}

import
std/[options, sequtils, times, algorithm],
std/[options, sequtils, algorithm],
testutils/unittests,
nimcrypto/sha2,
libp2p/protobuf/minprotobuf
import
../../waku/v2/node/storage/message/waku_store_queue,
../../waku/v2/protocol/waku_store,
../../waku/v2/protocol/waku_message,
../../waku/v2/utils/time


const
DefaultPubsubTopic = "/waku/2/default-waku/proto"
DefaultContentTopic = ContentTopic("/waku/2/default-content/proto")
../../waku/v2/utils/time,
./testlib/common


proc getTestStoreQueue(numMessages: int): StoreQueueRef =
Expand All @@ -36,12 +32,6 @@ proc getTestStoreQueue(numMessages: int): StoreQueueRef =

return testStoreQueue

proc now(): Timestamp =
getNanosecondTime(getTime().toUnixFloat())

proc ts(offset=0, origin=now()): Timestamp =
origin + getNanosecondTime(offset)


suite "Queue store - pagination":
test "Forward pagination test":
Expand Down
26 changes: 2 additions & 24 deletions tests/v2/test_message_store_sqlite.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,13 @@ import
../../waku/v2/protocol/waku_message,
../../waku/v2/protocol/waku_store/pagination,
../../waku/v2/utils/time,
./utils


const
DefaultPubsubTopic = "/waku/2/default-waku/proto"
DefaultContentTopic = ContentTopic("/waku/2/default-content/proto")
./utils,
./testlib/common


proc newTestDatabase(): SqliteDatabase =
SqliteDatabase.init("", inMemory = true).tryGet()

proc now(): Timestamp =
getNanosecondTime(getTime().toUnixFloat())

proc ts(offset=0, origin=now()): Timestamp =
origin + getNanosecondTime(offset)

proc fakeWakuMessage(
payload = "TEST-PAYLOAD",
contentTopic = DefaultContentTopic,
ts = now()
): WakuMessage =
WakuMessage(
payload: toBytes(payload),
contentTopic: contentTopic,
version: 1,
timestamp: ts
)


suite "SQLite message store - init store":
test "init store":
Expand Down
40 changes: 9 additions & 31 deletions tests/v2/test_message_store_sqlite_query.nim
Original file line number Diff line number Diff line change
@@ -1,45 +1,23 @@
{.used.}

import
std/[options, tables, sets, times, strutils, sequtils, algorithm],
stew/byteutils,
std/[options, tables, sets, strutils, sequtils, algorithm],
unittest2,
chronos,
chronicles,
chronicles
import
../../waku/v2/node/storage/message/sqlite_store,
../../waku/v2/node/storage/sqlite,
../../waku/v2/protocol/waku_message,
../../waku/v2/protocol/waku_store/pagination,
../../waku/v2/utils/time,
./utils


const
DefaultPubsubTopic = "/waku/2/default-waku/proto"
DefaultContentTopic = ContentTopic("/waku/2/default-content/proto")
./utils,
./testlib/common


proc now(): Timestamp =
getNanosecondTime(getTime().toUnixFloat())

proc ts(offset=0, origin=now()): Timestamp =
origin + getNanosecondTime(offset)

proc newTestDatabase(): SqliteDatabase =
SqliteDatabase.init("", inMemory = true).tryGet()

proc fakeWakuMessage(
payload = "TEST-PAYLOAD",
contentTopic = DefaultContentTopic,
ts = now()
): WakuMessage =
WakuMessage(
payload: toBytes(payload),
contentTopic: contentTopic,
version: 1,
timestamp: ts
)


suite "message store - history query":

Expand Down Expand Up @@ -477,7 +455,7 @@ suite "message store - history query":
test "single content topic and valid time range":
## Given
const contentTopic = "test-content-topic"
let timeOrigin = getNanosecondTime(epochTime())
let timeOrigin = now()

let
database = newTestDatabase()
Expand Down Expand Up @@ -522,7 +500,7 @@ suite "message store - history query":
test "single content topic and invalid time range - no results":
## Given
const contentTopic = "test-content-topic"
let timeOrigin = getNanosecondTime(epochTime())
let timeOrigin = now()

let
database = newTestDatabase()
Expand Down Expand Up @@ -561,7 +539,7 @@ suite "message store - history query":
test "single content topic and only time range start":
## Given
const contentTopic = "test-content-topic"
let timeOrigin = getNanosecondTime(epochTime())
let timeOrigin = now()

let
database = newTestDatabase()
Expand Down Expand Up @@ -602,7 +580,7 @@ suite "message store - history query":
test "single content topic, cursor and only time range start":
## Given
const contentTopic = "test-content-topic"
let timeOrigin = getNanosecondTime(epochTime())
let timeOrigin = now()

let
database = newTestDatabase()
Expand Down
16 changes: 5 additions & 11 deletions tests/v2/test_rest_relay_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import
../../waku/v2/protocol/waku_message,
../../waku/v2/node/waku_node,
../../waku/v2/node/rest/[server, client, base64, utils],
../../waku/v2/node/rest/relay/[api_types, relay_api, topic_cache]
../../waku/v2/node/rest/relay/[api_types, relay_api, topic_cache],
../../waku/v2/utils/time,
./testlib/common


proc testWakuNode(): WakuNode =
Expand All @@ -26,14 +28,6 @@ proc testWakuNode(): WakuNode =

WakuNode.new(privkey, bindIp, port, some(extIp), some(port))

proc fakeWakuMessage(payload = toBytes("TEST"), contentTopic = "test"): WakuMessage =
WakuMessage(
payload: payload,
contentTopic: contentTopic,
version: 1,
timestamp: 2022
)


suite "REST API - Relay":
asyncTest "Subscribe a node to an array of topics - POST /relay/v1/subscriptions":
Expand Down Expand Up @@ -167,8 +161,8 @@ suite "REST API - Relay":
response.data.all do (msg: RelayWakuMessage) -> bool:
msg.payload == Base64String.encode("TEST-1") and
msg.contentTopic.get().string == "content-topic-x" and
msg.version.get() == Natural(1) and
msg.timestamp.get() == int64(2022)
msg.version.get() == 2 and
msg.timestamp.get() != Timestamp(0)


check:
Expand Down
14 changes: 3 additions & 11 deletions tests/v2/test_waku_filter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,20 @@ import
testutils/unittests,
chronos,
chronicles,
libp2p/switch,
libp2p/crypto/crypto,
libp2p/multistream
import
../../waku/v2/node/peer_manager/peer_manager,
../../waku/v2/protocol/waku_message,
../../waku/v2/protocol/waku_filter,
../test_helpers,
./utils
./utils,
./testlib/common,
./testlib/switch


const
DefaultPubsubTopic = "/waku/2/default-waku/proto"
DefaultContentTopic = ContentTopic("/waku/2/default-content/proto")

const dummyHandler = proc(requestId: string, msg: MessagePush) {.async, gcsafe, closure.} = discard

proc newTestSwitch(key=none(PrivateKey), address=none(MultiAddress)): Switch =
let peerKey = key.get(PrivateKey.random(ECDSA, rng[]).get())
let peerAddr = address.get(MultiAddress.init("/ip4/127.0.0.1/tcp/0").get())
return newStandardSwitch(some(peerKey), addrs=peerAddr)


# TODO: Extend test coverage
procSuite "Waku Filter":
Expand Down
8 changes: 2 additions & 6 deletions tests/v2/test_waku_lightpush.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ import
../../waku/v2/node/peer_manager/peer_manager,
../../waku/v2/protocol/waku_message,
../../waku/v2/protocol/waku_lightpush,
../test_helpers


const
DefaultPubsubTopic = "/waku/2/default-waku/proto"
DefaultContentTopic = ContentTopic("/waku/2/default-content/proto")
../test_helpers,
./testlib/common


# TODO: Extend lightpush protocol test coverage
Expand Down
3 changes: 2 additions & 1 deletion tests/v2/test_waku_rln_relay.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import
testutils/unittests, chronos, chronicles, stint,
stew/byteutils, stew/shims/net as stewNet,
libp2p/crypto/crypto,
json,
json
import
../../waku/v2/protocol/waku_message,
../../waku/v2/protocol/waku_rln_relay/[rln,
waku_rln_relay_utils,
Expand Down
41 changes: 7 additions & 34 deletions tests/v2/test_waku_store.nim
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{.used.}

import
std/[options, tables, sets, sequtils, times],
std/[options, tables, sets, sequtils],
stew/byteutils,
testutils/unittests,
chronos,
chronicles,
libp2p/switch,
libp2p/crypto/crypto
import
../../waku/v2/protocol/waku_message,
Expand All @@ -17,39 +16,13 @@ import
../../waku/v2/node/storage/message/sqlite_store,
../../waku/v2/node/peer_manager/peer_manager,
../../waku/v2/utils/time,
../test_helpers
./testlib/common,
./testlib/switch


const
DefaultPubsubTopic = "/waku/2/default-waku/proto"
DefaultContentTopic = ContentTopic("/waku/2/default-content/proto")


proc now(): Timestamp =
getNanosecondTime(getTime().toUnixFloat())

proc newTestDatabase(): SqliteDatabase =
SqliteDatabase.init("", inMemory = true).tryGet()

proc fakeWakuMessage(
payload = "TEST-PAYLOAD",
contentTopic = DefaultContentTopic,
ts = getNanosecondTime(epochTime()),
ephemeral = false,
): WakuMessage =
WakuMessage(
payload: toBytes(payload),
contentTopic: contentTopic,
version: 1,
timestamp: ts,
ephemeral: ephemeral,
)

proc newTestSwitch(key=none(PrivateKey), address=none(MultiAddress)): Switch =
let peerKey = key.get(PrivateKey.random(ECDSA, rng[]).get())
let peerAddr = address.get(MultiAddress.init("/ip4/127.0.0.1/tcp/0").get())
return newStandardSwitch(some(peerKey), addrs=peerAddr)

proc newTestMessageStore(): MessageStore =
let database = newTestDatabase()
SqliteStore.init(database).tryGet()
Expand Down Expand Up @@ -340,7 +313,7 @@ procSuite "Waku Store - history query":
client.setPeer(serverSwitch.peerInfo.toRemotePeerInfo())

## Given
let currentTime = getNanosecondTime(getTime().toUnixFloat())
let currentTime = now()
let msgList = @[
WakuMessage(payload: @[byte 0], contentTopic: ContentTopic("2"), timestamp: currentTime - 9),
WakuMessage(payload: @[byte 1], contentTopic: DefaultContentTopic, timestamp: currentTime - 8),
Expand Down Expand Up @@ -409,7 +382,7 @@ procSuite "Waku Store - history query":
client.setPeer(serverSwitch.peerInfo.toRemotePeerInfo())

## Given
let currentTime = getNanosecondTime(getTime().toUnixFloat())
let currentTime = now()
let msgList = @[
WakuMessage(payload: @[byte 0], contentTopic: ContentTopic("2"), timestamp: currentTime - 9),
WakuMessage(payload: @[byte 1], contentTopic: DefaultContentTopic, timestamp: currentTime - 8),
Expand Down Expand Up @@ -695,7 +668,7 @@ suite "Waku Store - message handling":

## Given
let
now = getNanoSecondTime(getTime().toUnixFloat())
now = now()
invalidSenderTime = now + MaxMessageTimestampVariance + 1

let message = fakeWakuMessage(ts=invalidSenderTime)
Expand All @@ -718,7 +691,7 @@ suite "Waku Store - message handling":

## Given
let
now = getNanoSecondTime(getTime().toUnixFloat())
now = now()
invalidSenderTime = now - MaxMessageTimestampVariance - 1

let message = fakeWakuMessage(ts=invalidSenderTime)
Expand Down
Loading

0 comments on commit 9b59052

Please sign in to comment.