| … | … | @@ -0,0 +1,114 @@ |
| 1 | import test from 'brittle' |
| 2 | import Corestore from 'corestore' |
| 3 | import path from 'bare-path' |
| 4 | import fs from 'bare-fs' |
| 5 | import os from 'bare-os' |
| 6 | import { MediaStore } from '../../lib/media-store.js' |
| 7 | |
| 8 | function tmpdir () { |
| 9 | const dir = path.join(os.tmpdir(), 'hn-gc-test-' + Date.now() + '-' + Math.random().toString(36).slice(2)) |
| 10 | fs.mkdirSync(dir, { recursive: true }) |
| 11 | return dir |
| 12 | } |
| 13 | |
| 14 | function rmrf (dir) { |
| 15 | try { fs.rmSync(dir, { recursive: true, force: true }) } catch {} |
| 16 | } |
| 17 | |
| 18 | test('GC - scheduleOrphan clears blob after grace period', async (t) => { |
| 19 | const dir = tmpdir() |
| 20 | try { |
| 21 | const store = new Corestore(dir) |
| 22 | const media = new MediaStore(store, { |
| 23 | graceMs: 100, |
| 24 | isReferencedAnywhere: async () => false |
| 25 | }) |
| 26 | await media.ready() |
| 27 | |
| 28 | const buf = Buffer.from('orphan data') |
| 29 | const hash = await media.put(buf, 'text/plain') |
| 30 | t.is(await media.has(hash), true) |
| 31 | |
| 32 | media.scheduleOrphan(hash) |
| 33 | |
| 34 | // Wait for grace period + buffer |
| 35 | await new Promise(r => setTimeout(r, 250)) |
| 36 | |
| 37 | t.is(await media.has(hash), false) |
| 38 | } finally { |
| 39 | rmrf(dir) |
| 40 | } |
| 41 | }) |
| 42 | |
| 43 | test('GC - cancelOrphan prevents deletion', async (t) => { |
| 44 | const dir = tmpdir() |
| 45 | try { |
| 46 | const store = new Corestore(dir) |
| 47 | const media = new MediaStore(store, { |
| 48 | graceMs: 200, |
| 49 | isReferencedAnywhere: async () => false |
| 50 | }) |
| 51 | await media.ready() |
| 52 | |
| 53 | const buf = Buffer.from('rescued data') |
| 54 | const hash = await media.put(buf, 'text/plain') |
| 55 | |
| 56 | media.scheduleOrphan(hash) |
| 57 | |
| 58 | // Cancel before grace period |
| 59 | await new Promise(r => setTimeout(r, 50)) |
| 60 | media.cancelOrphan(hash) |
| 61 | |
| 62 | // Wait past original grace period |
| 63 | await new Promise(r => setTimeout(r, 300)) |
| 64 | |
| 65 | t.is(await media.has(hash), true) |
| 66 | } finally { |
| 67 | rmrf(dir) |
| 68 | } |
| 69 | }) |
| 70 | |
| 71 | test('GC - scheduleOrphan skips if still referenced', async (t) => { |
| 72 | const dir = tmpdir() |
| 73 | try { |
| 74 | const store = new Corestore(dir) |
| 75 | const media = new MediaStore(store, { |
| 76 | graceMs: 100, |
| 77 | isReferencedAnywhere: async () => true |
| 78 | }) |
| 79 | await media.ready() |
| 80 | |
| 81 | const buf = Buffer.from('still in use') |
| 82 | const hash = await media.put(buf, 'text/plain') |
| 83 | |
| 84 | media.scheduleOrphan(hash) |
| 85 | await new Promise(r => setTimeout(r, 250)) |
| 86 | |
| 87 | t.is(await media.has(hash), true) |
| 88 | } finally { |
| 89 | rmrf(dir) |
| 90 | } |
| 91 | }) |
| 92 | |
| 93 | test('GC - duplicate scheduleOrphan is idempotent', async (t) => { |
| 94 | const dir = tmpdir() |
| 95 | try { |
| 96 | const store = new Corestore(dir) |
| 97 | const media = new MediaStore(store, { |
| 98 | graceMs: 100, |
| 99 | isReferencedAnywhere: async () => false |
| 100 | }) |
| 101 | await media.ready() |
| 102 | |
| 103 | const buf = Buffer.from('double schedule') |
| 104 | const hash = await media.put(buf, 'text/plain') |
| 105 | |
| 106 | media.scheduleOrphan(hash) |
| 107 | media.scheduleOrphan(hash) |
| 108 | |
| 109 | await new Promise(r => setTimeout(r, 250)) |
| 110 | t.is(await media.has(hash), false) |
| 111 | } finally { |
| 112 | rmrf(dir) |
| 113 | } |
| 114 | }) |