From 043faed83680a1292a63bf3aedcf410698192025 Mon Sep 17 00:00:00 2001 From: Jacob Karlsson Date: Fri, 28 Jun 2024 15:14:05 +0200 Subject: [PATCH] Import realtime test from sync module --- test/realtime.test.js | 98 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 test/realtime.test.js diff --git a/test/realtime.test.js b/test/realtime.test.js new file mode 100644 index 0000000..95e963d --- /dev/null +++ b/test/realtime.test.js @@ -0,0 +1,98 @@ +const test = require('node:test') +const assert = require('node:assert') +const p = require('node:util').promisify +const { createPeer } = require('./util') + +async function flatten(iter) { + const ary = [] + for await (const it of iter) { + ary.push(it) + } + return ary +} + +// copy of test in pzp-sync, but that doesn't test if it works with gc enabled +test('create 200 messages that manage to replicate with low "newest" goals', async (t) => { + const n = 200 + + const alice = createPeer({ name: 'alice' }) + const bob = createPeer({ name: 'bob' }) + + await alice.db.loaded() + await bob.db.loaded() + + const aliceID = await p(alice.db.account.create)({ + subdomain: 'account', + _nonce: 'alice', + }) + const bobID = await p(bob.db.account.create)({ + subdomain: 'account', + _nonce: 'bob', + }) + + const bobPostsID = bob.db.feed.getID(bobID, 'post') + + { + const arr = (await flatten(alice.db.msgs())) + .filter((msg) => msg.metadata.account === bobID && msg.data) + .map((msg) => msg.data.text) + assert.deepEqual(arr, [], 'alice has no posts from bob') + } + + const confirmed = [] + // for keeping track of which msgs have arrived + for (let i = 0; i < n; i++) { + confirmed.push(false) + } + + alice.db.onRecordAdded(rec => { + if (rec.msg.data?.text) { + const num = Number.parseInt(rec.msg.data.text) + confirmed[num] = true + } + }) + + await p(alice.set.load)(aliceID) + await p(bob.set.load)(bobID) + + // TODO: do we need to follow each other? + assert(await p(alice.set.add)('follows', bobID), 'alice follows bob') + + //bob.goals.set(bobPostsID, 'newest-50') + //alice.goals.set(bobPostsID, 'newest-50') + // TODO: lower to newest-50, alice then bob + // supposedly [myrules, theirrules], do they need to match? + await p(alice.conductor.start)(aliceID, [['post@all'], ['post@all']], 64_000_000) + await p(bob.conductor.start)(bobID, [['post@all'], ['post@all']], 64_000_000) + + const remoteAlice = await p(bob.connect)(alice.getAddress()) + assert('alice and bob connected') + + bob.sync.start() + await p(setTimeout)(1000) + assert('sync!') + + const hundred = [] + for (let i = 0; i < n; i++) { + hundred.push(i) + } + Promise.all(hundred.map(i => p(bob.db.feed.publish)({ + account: bobID, + domain: 'post', + data: { text: `${i}` }, + }))) + assert(`bob published ${n} posts in parallel`) + + //let tries = 30 + let tries = 100 + // just waiting for them to arrive + do { + await p(setTimeout)(100) + } while (!confirmed.every(v => v === true) && tries-- > 0) + + assert.equal(confirmed.filter(v => v === true).length, n, `alice has all of bob's posts`) + + await p(remoteAlice.close)(true) + await p(alice.close)(true) + await p(bob.close)(true) +}) \ No newline at end of file