Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Commit

Permalink
test for implicit snapshot on readStream()
Browse files Browse the repository at this point in the history
also add some basic snapshot stuff in the native layer, ready for
implementing in JS too.
  • Loading branch information
rvagg committed Dec 3, 2012
1 parent a27c152 commit 40dbde6
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 47 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"tar": "*",
"mkfiletree": "*",
"readfiletree": "*",
"slow-stream": "*",
"slow-stream": "~0.0.3",
"delayed": "*",
"boganipsum": "*"
},
Expand All @@ -43,7 +43,10 @@
"test": "./node_modules/.bin/buster-test",
"install": "node-gyp rebuild",
"functionaltests": "node ./test/functional/fstream-test.js && node ./test/functional/binary-data-test.js && node ./test/functional/compat-test.js",
"alltests": "npm test && npm run-script functionaltests"
"alltests": "npm test && npm run-script functionaltests",
"publish": "rm -rf ./node_modules/ npm-shrinkwrap.json && npm install --production && npm shrinkwrap && npm publish && rm npm-shrinkwrap.json && npm install",
"sr": "npm shrinkwrap",
"l": "npm ls"
},
"license": "MIT",
"gypfile": true
Expand Down
8 changes: 8 additions & 0 deletions src/database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ leveldb::Iterator* Database::NewIterator (ReadOptions* options) {
return db->NewIterator(*options);
}

const leveldb::Snapshot* Database::NewSnapshot () {
return db->GetSnapshot();
}

void Database::ReleaseSnapshot (const leveldb::Snapshot* snapshot) {
return db->ReleaseSnapshot(snapshot);
}

void Database::CloseDatabase () {
delete db;
db = NULL;
Expand Down
14 changes: 8 additions & 6 deletions src/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ class Database : public node::ObjectWrap {
static void Init ();
static v8::Handle<v8::Value> NewInstance (const v8::Arguments& args);

Status OpenDatabase (Options* options, string location);
Status PutToDatabase (WriteOptions* options, Slice key, Slice value);
Status GetFromDatabase (ReadOptions* options, Slice key, string& value);
Status DeleteFromDatabase (WriteOptions* options, Slice key);
Status WriteBatchToDatabase (WriteOptions* options, WriteBatch* batch);
Status OpenDatabase (Options* options, string location);
Status PutToDatabase (WriteOptions* options, Slice key, Slice value);
Status GetFromDatabase (ReadOptions* options, Slice key, string& value);
Status DeleteFromDatabase (WriteOptions* options, Slice key);
Status WriteBatchToDatabase (WriteOptions* options, WriteBatch* batch);
leveldb::Iterator* NewIterator (ReadOptions* options);
void CloseDatabase ();
const leveldb::Snapshot* NewSnapshot ();
void ReleaseSnapshot (const leveldb::Snapshot* snapshot);
void CloseDatabase ();

private:
Database ();
Expand Down
41 changes: 40 additions & 1 deletion test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,43 @@ module.exports.commonSetUp = function () {
this.closeableDatabases = []
this.openTestDatabase = module.exports.openTestDatabase.bind(this)
this.timeout = 10000
}
}

module.exports.readStreamSetUp = function () {
module.exports.commonSetUp.call(this)

this.readySpy = this.spy()
this.dataSpy = this.spy()
this.endSpy = this.spy()
this.sourceData = []

for (var i = 0; i < 100; i++) {
var k = (i < 10 ? '0' : '') + i
this.sourceData.push({
type : 'put'
, key : k
, value : Math.random()
})
}

this.verify = function (rs, done, data) {
if (!data) data = this.sourceData // can pass alternative data array for verification
assert.isFalse(rs.writable)
assert.isFalse(rs.readable)
assert.equals(this.readySpy.callCount, 1, 'ReadStream emitted single "ready" event')
assert.equals(this.endSpy.callCount, 1, 'ReadStream emitted single "end" event')
assert.equals(this.dataSpy.callCount, data.length, 'ReadStream emitted correct number of "data" events')
data.forEach(function (d, i) {
var call = this.dataSpy.getCall(i)
if (call) {
//console.log('call', i, ':', call.args[0].key, '=', call.args[0].value, '(expected', d.key, '=', d.value, ')')
assert.equals(call.args.length, 1, 'ReadStream "data" event #' + i + ' fired with 1 argument')
refute.isNull(call.args[0].key, 'ReadStream "data" event #' + i + ' argument has "key" property')
refute.isNull(call.args[0].value, 'ReadStream "data" event #' + i + ' argument has "value" property')
assert.equals(call.args[0].key, d.key, 'ReadStream "data" event #' + i + ' argument has correct "key"')
assert.equals(call.args[0].value, d.value, 'ReadStream "data" event #' + i + ' argument has correct "value"')
}
}.bind(this))
done()
}.bind(this)
}
39 changes: 1 addition & 38 deletions test/read-stream-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,7 @@ var buster = require('buster')
, async = require('async')

buster.testCase('ReadStream', {
'setUp': function () {
common.commonSetUp.call(this)

this.readySpy = this.spy()
this.dataSpy = this.spy()
this.endSpy = this.spy()
this.sourceData = []

for (var i = 0; i < 100; i++) {
var k = (i < 10 ? '0' : '') + i
this.sourceData.push({
type : 'put'
, key : k
, value : Math.random()
})
}

this.verify = function (rs, done, data) {
if (!data) data = this.sourceData // can pass alternative data array for verification
assert.isFalse(rs.writable)
assert.isFalse(rs.readable)
assert.equals(this.readySpy.callCount, 1, 'ReadStream emitted single "ready" event')
assert.equals(this.endSpy.callCount, 1, 'ReadStream emitted single "end" event')
assert.equals(this.dataSpy.callCount, data.length, 'ReadStream emitted correct number of "data" events')
data.forEach(function (d, i) {
var call = this.dataSpy.getCall(i)
if (call) {
//console.log('call', i, ':', call.args[0].key, '=', call.args[0].value, '(expected', d.key, '=', d.value, ')')
assert.equals(call.args.length, 1, 'ReadStream "data" event #' + i + ' fired with 1 argument')
refute.isNull(call.args[0].key, 'ReadStream "data" event #' + i + ' argument has "key" property')
refute.isNull(call.args[0].value, 'ReadStream "data" event #' + i + ' argument has "value" property')
assert.equals(call.args[0].key, d.key, 'ReadStream "data" event #' + i + ' argument has correct "key"')
assert.equals(call.args[0].value, d.value, 'ReadStream "data" event #' + i + ' argument has correct "value"')
}
}.bind(this))
done()
}.bind(this)
}
'setUp': common.readStreamSetUp

, 'tearDown': common.commonTearDown

Expand Down

0 comments on commit 40dbde6

Please sign in to comment.