start() will automatically stop()

This commit is contained in:
Andre Staltz 2023-12-21 11:54:07 +02:00
parent 6d7d31f878
commit d596754bd1
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
2 changed files with 44 additions and 0 deletions

View File

@ -214,6 +214,7 @@ function initGC(peer, config) {
* @param {number?} maxLogBytes
*/
function start(maxLogBytes) {
stop()
const actualMaxLogBytes = maxLogBytes ?? config.gc?.maxLogBytes ?? null
// prettier-ignore
if (!actualMaxLogBytes) throw new Error('gc plugin requires maxLogBytes via start() argument or config.gc.maxLogBytes')

View File

@ -103,3 +103,46 @@ test('Compaction is scheduled automatically', async (t) => {
await p(alice.close)(true)
})
test('start() will automatically stop()', async (t) => {
const alice = createPeer({ name: 'alice' })
await alice.db.loaded()
// Alice creates her own account
const aliceID = await p(alice.db.account.create)({
subdomain: 'account',
_nonce: 'alice',
})
alice.goals.set(aliceID, 'all') // alice wants her account tangle
const postFeedID = alice.db.feed.getID(aliceID, 'post')
alice.goals.set(postFeedID, 'newest-3')
assert('alice set a goal for newest-3 of post feed')
alice.gc.start(40 * 1024) // 40kB
for (let i = 0; i < 5; i++) {
await p(alice.db.feed.publish)({
account: aliceID,
domain: 'post',
data: { text: 'A' + i },
})
}
assert.deepEqual(
getTexts([...alice.db.msgs()]),
['A0', 'A1', 'A2', 'A3', 'A4'],
'alice has the whole feed'
)
alice.gc.start(4 * 1024) // 4kB, approximately 8 messages
await p(setTimeout)(3000)
assert.deepEqual(
getTexts([...alice.db.msgs()]),
['A2', 'A3', 'A4'],
'alice has only latest 3 msgs in the feed'
)
await p(alice.close)(true)
})