From 5cd23e23e256af3bef7629dfc9c6c1bc5d9849c0 Mon Sep 17 00:00:00 2001 From: mixmix Date: Thu, 11 Apr 2024 16:29:05 +1200 Subject: [PATCH] failing test: bulk create adds messages to db in wrong order --- package.json | 4 ++-- test/create.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index d2847b67..698e8f24 100644 --- a/package.json +++ b/package.json @@ -68,8 +68,8 @@ "ssb-caps": "1.1.0", "ssb-db": "19.3.1", "ssb-fixtures": "4.0.1", - "tap-arc": "^0.3.4", - "tape": "^5.2.2", + "tap-arc": "^1.2.2", + "tape": "^5.7.5", "trammel": "~4.0.0" }, "scripts": { diff --git a/test/create.js b/test/create.js index 40054438..5963d329 100644 --- a/test/create.js +++ b/test/create.js @@ -10,7 +10,16 @@ const bipf = require('bipf') const mkdirp = require('mkdirp') const SecretStack = require('secret-stack') const caps = require('ssb-caps') -const { where, isDecrypted, toCallback } = require('../operators') +const pull = require('pull-stream') +const { promisify: p } = require('util') + +const { + where, + type, + isDecrypted, + toCallback, + toPullStream, +} = require('../operators') const dir = '/tmp/ssb-db2-create' @@ -19,7 +28,7 @@ mkdirp.sync(dir) const keys = ssbKeys.loadOrCreateSync(path.join(dir, 'secret')) -let sbot = SecretStack({ appKey: caps.shs }) +const sbot = SecretStack({ appKey: caps.shs }) .use(require('../')) .use(require('../compat/ebt')) .use(require('ssb-bendy-butt')) @@ -28,7 +37,7 @@ let sbot = SecretStack({ appKey: caps.shs }) keys, path: dir, }) -let db = sbot.db +const db = sbot.db test('create() classic', (t) => { db.create( @@ -85,6 +94,46 @@ test('create() classic "too big"', (t) => { }) }) +test('create() bulk, fast!', async (t) => { + const N = 4 + const many = new Array(N).fill('').map(() => { + return { type: 'counter' } + }) + + const expectedSequenceOrder = new Array(N).fill(0).map((_, i) => i + 1) + + await Promise.all( + many.map((content) => { + return p(db.create)({ feedFormat: 'classic', content }) + }) + ) + .then((msgs) => { + t.pass('bulk publishes messages') + t.deepEqual( + msgs.map((m) => m.value.sequence), + expectedSequenceOrder, + 'messages created in correct order' + ) + }) + .catch((err) => t.error(err, 'bulk publishes messages')) + + const sequencesFromDb = await pull( + db.query(where(type('counter')), toPullStream()), + pull.map((m) => m.value.sequence), + pull.collectAsPromise() + ) + + t.deepEqual( + sequencesFromDb, + expectedSequenceOrder, + 'messages come out of the DB in the right order' + ) + t.end() + + // TEMP + sbot.close(true, () => t.end()) +}) + test('create() classic box', (t) => { db.create( {