2023-05-31 08:28:48 +00:00
|
|
|
{.used.}
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
import std/[sequtils, options], testutils/unittests, chronos
|
2023-05-31 08:28:48 +00:00
|
|
|
import
|
2024-07-05 22:03:38 +00:00
|
|
|
waku/[
|
|
|
|
waku_archive,
|
|
|
|
waku_archive/driver/postgres_driver,
|
|
|
|
waku_core,
|
|
|
|
waku_core/message/digest,
|
|
|
|
],
|
2024-03-01 11:05:27 +00:00
|
|
|
../testlib/wakucore,
|
|
|
|
../testlib/testasync,
|
|
|
|
../testlib/postgres
|
2023-05-31 08:28:48 +00:00
|
|
|
|
|
|
|
suite "Postgres driver":
|
2024-03-01 11:05:27 +00:00
|
|
|
## Unique driver instance
|
|
|
|
var driver {.threadvar.}: PostgresDriver
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2024-03-01 11:05:27 +00:00
|
|
|
asyncSetup:
|
|
|
|
let driverRes = await newTestPostgresDriver()
|
|
|
|
if driverRes.isErr():
|
|
|
|
assert false, driverRes.error
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2024-03-01 11:05:27 +00:00
|
|
|
driver = PostgresDriver(driverRes.get())
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2024-03-01 11:05:27 +00:00
|
|
|
asyncTeardown:
|
|
|
|
let resetRes = await driver.reset()
|
|
|
|
if resetRes.isErr():
|
|
|
|
assert false, resetRes.error
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2024-03-01 11:05:27 +00:00
|
|
|
(await driver.close()).expect("driver to close")
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2024-03-01 11:05:27 +00:00
|
|
|
asyncTest "Asynchronous queries":
|
2023-06-07 08:08:43 +00:00
|
|
|
var futures = newSeq[Future[ArchiveDriverResult[void]]](0)
|
2023-05-31 08:28:48 +00:00
|
|
|
|
|
|
|
let beforeSleep = now()
|
2023-06-07 08:08:43 +00:00
|
|
|
for _ in 1 .. 100:
|
|
|
|
futures.add(driver.sleep(1))
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2023-06-07 08:08:43 +00:00
|
|
|
await allFutures(futures)
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2023-06-07 08:08:43 +00:00
|
|
|
let diff = now() - beforeSleep
|
|
|
|
# Actually, the diff randomly goes between 1 and 2 seconds.
|
|
|
|
# although in theory it should spend 1s because we establish 100
|
|
|
|
# connections and we spawn 100 tasks that spend ~1s each.
|
2024-03-06 19:50:22 +00:00
|
|
|
assert diff < 20_000_000_000
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2023-06-07 08:08:43 +00:00
|
|
|
asyncTest "Insert a message":
|
2023-05-31 08:28:48 +00:00
|
|
|
const contentTopic = "test-content-topic"
|
2024-05-06 08:20:21 +00:00
|
|
|
const meta = "test meta"
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2024-05-06 08:20:21 +00:00
|
|
|
let msg = fakeWakuMessage(contentTopic = contentTopic, meta = meta)
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let putRes = await driver.put(
|
2024-07-12 16:19:12 +00:00
|
|
|
computeMessageHash(DefaultPubsubTopic, msg), DefaultPubsubTopic, msg
|
2024-03-15 23:08:47 +00:00
|
|
|
)
|
2023-06-07 08:08:43 +00:00
|
|
|
assert putRes.isOk(), putRes.error
|
2023-05-31 08:28:48 +00:00
|
|
|
|
|
|
|
let storedMsg = (await driver.getAllMessages()).tryGet()
|
2024-03-06 19:50:22 +00:00
|
|
|
|
|
|
|
assert storedMsg.len == 1
|
|
|
|
|
2024-07-12 16:19:12 +00:00
|
|
|
let (_, pubsubTopic, actualMsg) = storedMsg[0]
|
2024-03-06 19:50:22 +00:00
|
|
|
assert actualMsg.contentTopic == contentTopic
|
|
|
|
assert pubsubTopic == DefaultPubsubTopic
|
|
|
|
assert toHex(actualMsg.payload) == toHex(msg.payload)
|
2024-05-06 08:20:21 +00:00
|
|
|
assert toHex(actualMsg.meta) == toHex(msg.meta)
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2023-06-07 08:08:43 +00:00
|
|
|
asyncTest "Insert and query message":
|
2023-05-31 08:28:48 +00:00
|
|
|
const contentTopic1 = "test-content-topic-1"
|
|
|
|
const contentTopic2 = "test-content-topic-2"
|
|
|
|
const pubsubTopic1 = "pubsubtopic-1"
|
|
|
|
const pubsubTopic2 = "pubsubtopic-2"
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let msg1 = fakeWakuMessage(contentTopic = contentTopic1)
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2024-07-12 16:19:12 +00:00
|
|
|
var putRes =
|
|
|
|
await driver.put(computeMessageHash(pubsubTopic1, msg1), pubsubTopic1, msg1)
|
2023-06-07 08:08:43 +00:00
|
|
|
assert putRes.isOk(), putRes.error
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let msg2 = fakeWakuMessage(contentTopic = contentTopic2)
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2024-07-12 16:19:12 +00:00
|
|
|
putRes =
|
|
|
|
await driver.put(computeMessageHash(pubsubTopic2, msg2), pubsubTopic2, msg2)
|
2023-06-07 08:08:43 +00:00
|
|
|
assert putRes.isOk(), putRes.error
|
2023-05-31 08:28:48 +00:00
|
|
|
|
|
|
|
let countMessagesRes = await driver.getMessagesCount()
|
|
|
|
|
2024-03-06 19:50:22 +00:00
|
|
|
assert countMessagesRes.isOk(), $countMessagesRes.error
|
|
|
|
assert countMessagesRes.get() == 2
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2024-07-12 16:19:12 +00:00
|
|
|
var messagesRes = await driver.getMessages(contentTopics = @[contentTopic1])
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2024-03-06 19:50:22 +00:00
|
|
|
assert messagesRes.isOk(), $messagesRes.error
|
|
|
|
assert messagesRes.get().len == 1
|
2023-05-31 08:28:48 +00:00
|
|
|
|
|
|
|
# Get both content topics, check ordering
|
2024-03-15 23:08:47 +00:00
|
|
|
messagesRes =
|
2024-07-12 16:19:12 +00:00
|
|
|
await driver.getMessages(contentTopics = @[contentTopic1, contentTopic2])
|
2023-06-07 08:08:43 +00:00
|
|
|
assert messagesRes.isOk(), messagesRes.error
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2024-03-06 19:50:22 +00:00
|
|
|
assert messagesRes.get().len == 2
|
2024-07-12 16:19:12 +00:00
|
|
|
assert messagesRes.get()[0][2].contentTopic == contentTopic1
|
2023-05-31 08:28:48 +00:00
|
|
|
|
|
|
|
# Descending order
|
2024-03-15 23:08:47 +00:00
|
|
|
messagesRes = await driver.getMessages(
|
2024-07-12 16:19:12 +00:00
|
|
|
contentTopics = @[contentTopic1, contentTopic2], ascendingOrder = false
|
2024-03-15 23:08:47 +00:00
|
|
|
)
|
2023-06-07 08:08:43 +00:00
|
|
|
assert messagesRes.isOk(), messagesRes.error
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2024-03-06 19:50:22 +00:00
|
|
|
assert messagesRes.get().len == 2
|
2024-07-12 16:19:12 +00:00
|
|
|
assert messagesRes.get()[0][2].contentTopic == contentTopic2
|
2023-05-31 08:28:48 +00:00
|
|
|
|
|
|
|
# cursor
|
|
|
|
# Get both content topics
|
2024-03-15 23:08:47 +00:00
|
|
|
messagesRes = await driver.getMessages(
|
2024-07-12 16:19:12 +00:00
|
|
|
contentTopics = @[contentTopic1, contentTopic2],
|
|
|
|
cursor = some(computeMessageHash(pubsubTopic1, messagesRes.get()[1][2])),
|
2024-03-15 23:08:47 +00:00
|
|
|
)
|
2024-03-06 19:50:22 +00:00
|
|
|
assert messagesRes.isOk()
|
|
|
|
assert messagesRes.get().len == 1
|
2023-05-31 08:28:48 +00:00
|
|
|
|
|
|
|
# Get both content topics but one pubsub topic
|
2024-03-15 23:08:47 +00:00
|
|
|
messagesRes = await driver.getMessages(
|
2024-07-12 16:19:12 +00:00
|
|
|
contentTopics = @[contentTopic1, contentTopic2], pubsubTopic = some(pubsubTopic1)
|
2024-03-15 23:08:47 +00:00
|
|
|
)
|
2023-06-07 08:08:43 +00:00
|
|
|
assert messagesRes.isOk(), messagesRes.error
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2024-03-06 19:50:22 +00:00
|
|
|
assert messagesRes.get().len == 1
|
2024-07-12 16:19:12 +00:00
|
|
|
assert messagesRes.get()[0][2].contentTopic == contentTopic1
|
2023-05-31 08:28:48 +00:00
|
|
|
|
|
|
|
# Limit
|
2024-03-15 23:08:47 +00:00
|
|
|
messagesRes = await driver.getMessages(
|
2024-07-12 16:19:12 +00:00
|
|
|
contentTopics = @[contentTopic1, contentTopic2], maxPageSize = 1
|
2024-03-15 23:08:47 +00:00
|
|
|
)
|
2023-06-07 08:08:43 +00:00
|
|
|
assert messagesRes.isOk(), messagesRes.error
|
2024-03-06 19:50:22 +00:00
|
|
|
assert messagesRes.get().len == 1
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2023-06-07 08:08:43 +00:00
|
|
|
asyncTest "Insert true duplicated messages":
|
2023-05-31 08:28:48 +00:00
|
|
|
# Validates that two completely equal messages can not be stored.
|
|
|
|
|
|
|
|
let now = now()
|
|
|
|
|
|
|
|
let msg1 = fakeWakuMessage(ts = now)
|
|
|
|
let msg2 = fakeWakuMessage(ts = now)
|
|
|
|
|
2024-03-18 14:59:45 +00:00
|
|
|
let initialNumMsgs = (await driver.getMessagesCount()).valueOr:
|
|
|
|
raiseAssert "could not get num mgs correctly: " & $error
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
var putRes = await driver.put(
|
2024-07-12 16:19:12 +00:00
|
|
|
computeMessageHash(DefaultPubsubTopic, msg1), DefaultPubsubTopic, msg1
|
2024-03-15 23:08:47 +00:00
|
|
|
)
|
2023-06-07 08:08:43 +00:00
|
|
|
assert putRes.isOk(), putRes.error
|
2023-05-31 08:28:48 +00:00
|
|
|
|
2024-03-18 14:59:45 +00:00
|
|
|
var newNumMsgs = (await driver.getMessagesCount()).valueOr:
|
|
|
|
raiseAssert "could not get num mgs correctly: " & $error
|
|
|
|
|
|
|
|
assert newNumMsgs == (initialNumMsgs + 1.int64),
|
|
|
|
"wrong number of messages: " & $newNumMsgs
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
putRes = await driver.put(
|
2024-07-12 16:19:12 +00:00
|
|
|
computeMessageHash(DefaultPubsubTopic, msg2), DefaultPubsubTopic, msg2
|
2024-03-15 23:08:47 +00:00
|
|
|
)
|
2024-03-18 14:59:45 +00:00
|
|
|
|
|
|
|
assert putRes.isOk()
|
|
|
|
|
|
|
|
newNumMsgs = (await driver.getMessagesCount()).valueOr:
|
|
|
|
raiseAssert "could not get num mgs correctly: " & $error
|
|
|
|
|
|
|
|
assert newNumMsgs == (initialNumMsgs + 1.int64),
|
|
|
|
"wrong number of messages: " & $newNumMsgs
|