|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const test = require('tape') |
| 4 | +const tempy = require('tempy') |
| 5 | +const fork = require('child_process').fork |
| 6 | +const path = require('path') |
| 7 | +const { ClassicLevel } = require('..') |
| 8 | + |
| 9 | +test('lock held by same process', async function (t) { |
| 10 | + t.plan(2) |
| 11 | + |
| 12 | + const location = tempy.directory() |
| 13 | + const db1 = new ClassicLevel(location) |
| 14 | + await db1.open() |
| 15 | + const db2 = new ClassicLevel(location) |
| 16 | + |
| 17 | + try { |
| 18 | + await db2.open() |
| 19 | + } catch (err) { |
| 20 | + t.is(err.code, 'LEVEL_DATABASE_NOT_OPEN', 'second instance failed to open') |
| 21 | + t.is(err.cause.code, 'LEVEL_LOCKED', 'second instance got lock error') |
| 22 | + } |
| 23 | + |
| 24 | + return db1.close() |
| 25 | +}) |
| 26 | + |
| 27 | +test('lock held by other process', function (t) { |
| 28 | + t.plan(6) |
| 29 | + |
| 30 | + const location = tempy.directory() |
| 31 | + const db = new ClassicLevel(location) |
| 32 | + |
| 33 | + db.open(function (err) { |
| 34 | + t.ifError(err, 'no open error') |
| 35 | + |
| 36 | + const child = fork(path.join(__dirname, 'lock.js'), [location]) |
| 37 | + |
| 38 | + child.on('message', function (err) { |
| 39 | + t.is(err.code, 'LEVEL_DATABASE_NOT_OPEN', 'second process failed to open') |
| 40 | + t.is(err.cause.code, 'LEVEL_LOCKED', 'second process got lock error') |
| 41 | + |
| 42 | + child.disconnect() |
| 43 | + }) |
| 44 | + |
| 45 | + child.on('exit', function (code, sig) { |
| 46 | + t.is(code, 0, 'child exited normally') |
| 47 | + t.is(sig, null, 'not terminated due to signal') |
| 48 | + |
| 49 | + db.close(t.ifError.bind(t)) |
| 50 | + }) |
| 51 | + }) |
| 52 | +}) |
0 commit comments