Skip to content

Commit 4c5defd

Browse files
committed
Failing test for database reopening
This ensures that random-access-idb files are consistent across IndexedDB sessions, in particular with respect to the .length property
1 parent 7319386 commit 4c5defd

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

test/reopen.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
var test = require('tape')
2+
var rai = require('../')('testing-' + Math.random(), { size: 256 })
3+
var ram = require('random-access-memory')
4+
var randombytes = require('randombytes')
5+
var bequal = require('buffer-equals')
6+
var balloc = require('buffer-alloc')
7+
8+
test.only('random', function (t) {
9+
var nwrites = 100, nreads = 100
10+
t.plan(2 + nwrites*2 + nreads)
11+
12+
// this test differs from random.js in that it interleaves writes/reads, and randomly reopens the idb based storage some of the time
13+
var store = rai('cool.txt')
14+
function istore() {if (Math.random() > 0.75) store = rai('cool.txt'); return store }
15+
16+
var mstore = ram('cool.txt')
17+
18+
;(function () {
19+
var zeros = balloc(500+100)
20+
var pending = 2
21+
istore().write(0, zeros, function (err) {
22+
t.ifError(err)
23+
if (--pending === 0) write(0)
24+
})
25+
mstore.write(0, zeros, function (err) {
26+
t.ifError(err)
27+
if (--pending === 0) write(0)
28+
})
29+
})()
30+
31+
function write (i) {
32+
if (i === nwrites) return read(0)
33+
var offset = Math.floor(Math.random() * 500)
34+
var buf = randombytes(Math.floor(Math.random() * 100))
35+
var pending = 2
36+
istore().write(offset, buf, function (err) {
37+
t.ifError(err)
38+
if (--pending === 0) read(i+1)
39+
})
40+
mstore.write(offset, buf, function (err) {
41+
t.ifError(err)
42+
if (--pending === 0) read(i+1)
43+
})
44+
}
45+
46+
function read (i) {
47+
if (i === nreads) return
48+
var offset = Math.floor(Math.random() * 650)
49+
var len = Math.floor(Math.random()*100)
50+
var pending = 2, idata = {}, mdata = {}
51+
var store = istore()
52+
store.read(offset, len, function (err, buf) {
53+
idata = { err: err, buf: buf, length: store.length }
54+
if (--pending === 0) check()
55+
})
56+
mstore.read(offset, len, function (err, buf) {
57+
mdata = { err: err, buf: buf, length: mstore.length }
58+
if (--pending === 0) check()
59+
})
60+
function check () {
61+
t.deepEqual(idata, mdata)
62+
write(i+1)
63+
}
64+
}
65+
})

0 commit comments

Comments
 (0)