From cb557ef15a73cb6dbebfbdc5a90a79f419477d09 Mon Sep 17 00:00:00 2001 From: Jacob Karlsson Date: Fri, 28 Jun 2024 12:54:28 +0200 Subject: [PATCH 1/2] Add test for low newest goals --- test/realtime.test.js | 71 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/test/realtime.test.js b/test/realtime.test.js index 45ee76c..9fe1217 100644 --- a/test/realtime.test.js +++ b/test/realtime.test.js @@ -306,6 +306,77 @@ test('create 100 messages in parallel that still manage to sync realtime (withou } assert.equal(arr.length, n, `alice has ${arr.length} posts from bob`) + await p(remoteAlice.close)(true) + await p(alice.close)(true) + await p(bob.close)(true) +}) + +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 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 + } + }) + + bob.goals.set(bobPostsID, 'newest-50') + alice.goals.set(bobPostsID, 'newest-50') + + 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 + // just waiting for them to arrive + do { + await p(setTimeout)(100) + } while (!confirmed.every(v => v === true) && tries-- > 0) + console.log({tries}) + + 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) From 39f8cca20892f288626ace1c5496138114ab1387 Mon Sep 17 00:00:00 2001 From: Jacob Karlsson Date: Fri, 28 Jun 2024 13:02:38 +0200 Subject: [PATCH 2/2] Remove log --- test/realtime.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/realtime.test.js b/test/realtime.test.js index 9fe1217..e3b5a73 100644 --- a/test/realtime.test.js +++ b/test/realtime.test.js @@ -373,7 +373,6 @@ test('create 200 messages that manage to replicate with low "newest" goals', asy do { await p(setTimeout)(100) } while (!confirmed.every(v => v === true) && tries-- > 0) - console.log({tries}) assert.equal(confirmed.filter(v => v === true).length, n, `alice has all of bob's posts`)