Skip to content
This repository has been archived by the owner on Apr 5, 2018. It is now read-only.

Commit

Permalink
test: add mapsize and maxreaders tests again.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Sep 10, 2016
1 parent 87e7391 commit c99d7ba
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
57 changes: 57 additions & 0 deletions test/mapsize-test.js
@@ -0,0 +1,57 @@
const test = require('tape')
, lmdb = require('../')
, testCommon = require('abstract-leveldown/testCommon')
// 1MBish
, bigBlob = require('crypto').randomBytes(1000000)
//, bigBlob = Array.apply(null, Array(1024 * 100)).map(function () { return 'aaaaaaaaaa' }).join('')

test('setUp common', testCommon.setUp)

test('test default mapSize should bork', function (t) {
var db = lmdb(testCommon.location())
, puts = 20
, fails = 0
, donePuts = 0
, done = function () {
if (++donePuts == puts) {
t.ok(fails > 8 && fails <= 11, 'got expected number of fails (' + fails + ')')
db.close(testCommon.tearDown.bind(null, t))
}
}
db.open(function (err) {
t.notOk(err, 'no error')

for (var i = 0; i < puts; i++) {
(function (i) {
db.put(i, bigBlob, function (err) {
err && fails++
done()
})
}(i))
}
})
})

test('test mapSize increase', function (t) {
var db = lmdb(testCommon.location())
, puts = 20
, donePuts = 0
, done = function () {
if (++donePuts == puts)
db.close(testCommon.tearDown.bind(null, t))
}

// 25MB
db.open({ mapSize: 25 << 20 }, function (err) {
t.notOk(err, 'no error')

for (var i = 0; i < puts; i++) {
(function (i) {
db.put(i, bigBlob, function (err) {
t.notOk(err, 'no error from large put #' + i)
done()
})
}(i))
}
})
})
53 changes: 53 additions & 0 deletions test/maxreaders-with-test.js
@@ -0,0 +1,53 @@
const test = require('tape')
, lmdb = require('../')
, testCommon = require('abstract-leveldown/testCommon')

test('setUp common', testCommon.setUp)

test('test maxReaders increase', function (t) {
var db = lmdb(testCommon.location())
// The default maxreaders is 126, so having that many active iterators
// saturates all slots unless the max value is changed.
, nConcurrentIterators = 126
, nDone = 0;

var setupDone = function(iteratorCreationError) {
if (++nDone == nConcurrentIterators) {
t.notOk(iteratorCreationError, 'no iterator creation error')

// Create one last iterator. This errors if the readers table is
// full, succeeds otherwise
var iter = db.iterator();
iter.next(function(err) {
t.notOk(err, 'last concurrent reader was fine');

// Force exit; no cleanup
// ----------------------
//
// Doing this instead of a proper
//
// db.close(testCommon.tearDown.bind(null, t))
//
// because it's impossible to close an iterator from within the
// callback to its `next`-call, which is where all this gets
// called from.
//
// This doesn't affect the test's validity, as the reader limit
// is what counts here.
// process.exit(0);
t.end();
})
}
}

db.open({ maxReaders : 200 }, function (err) {
t.notOk(err, 'no db open error')

// Create saturating iterators and `next` once to make sure they're active
// and hence occupying a reader slot.
for (var i = 0; i < nConcurrentIterators; i++) {
db.iterator().next(setupDone);
}

})
})
54 changes: 54 additions & 0 deletions test/maxreaders-without-test.js
@@ -0,0 +1,54 @@
const test = require('tape')
, lmdb = require('../')
, testCommon = require('abstract-leveldown/testCommon')

test('setUp common', testCommon.setUp)

test('test default maxReaders should bork', function (t) {
var db = lmdb(testCommon.location())
// The default maxreaders is 126, so this should saturate all slots.
, nConcurrentIterators = 126
, nDone = 0;

var setupDone = function(iteratorCreationError) {
if (++nDone == nConcurrentIterators) {
t.notOk(iteratorCreationError, 'no iterator creation error')

// Create one last iterator. This should fail because the readers
// table is full.
var iter = db.iterator();
iter.next(function(err) {
var correctError = err && err.message
=== 'MDB_READERS_FULL: Environment maxreaders limit reached';
t.ok(correctError, 'got read error from last concurrent reader');

// Force exit; no cleanup
// ----------------------
//
// Doing this instead of a proper
//
// db.close(testCommon.tearDown.bind(null, t))
//
// because it's impossible to close an iterator from within the
// callback to its `next`-call, which is where all this gets
// called from.
//
// This doesn't affect the test's validity, as the reader limit
// is what counts here.
// process.exit(0);
t.end();
})
}
}

db.open(function (err) {
t.notOk(err, 'no db open error')

// Create saturating iterators and `next` once to make sure they're active
// and hence occupying a reader slot.
for (var i = 0; i < nConcurrentIterators; i++) {
db.iterator().next(setupDone);
}

})
})

0 comments on commit c99d7ba

Please sign in to comment.