Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

update tests

  • Loading branch information...
commit e46e80f68210825a3b82e647f9b08fb03344fd32 1 parent 7000ec6
@dominictarr authored
View
38 inmem.js
@@ -0,0 +1,38 @@
+
+var Docuset = require('./docuset')
+var es = require('event-stream')
+//this implements a server interface, but entirely in memory.
+
+module.exports = function (opts) {
+
+ var d = new Docuset(opts)
+ d.createServer = function (onConnect) {
+ this.listening = false
+ this.listen = function () {
+ var cb = arguments[arguments.length - 1]
+ this.listening = true
+ if('function' == typeof cb)
+ process.nextTick(cb)
+ return this
+ }
+
+ this._isInMemoryServer = true
+ this._connect = function () {
+ var ins = es.asyncThrough()
+ var out = es.asyncThrough()
+ this.createHandler(onConnect)(ins, out)
+ return es.duplex(ins, out)
+ }
+ return this
+ }
+ d.connect = d.createConnection =
+ function (other, cb) {
+ if(!other._isInMemoryServer)
+ throw new Error('other must be an inMemory Server')
+ if(!other.listening)
+ throw new Error('other is not listening')
+ return this.createHandler()(other._connect())
+ }
+
+ return d
+}
View
5 test/all.js
@@ -0,0 +1,5 @@
+require('./snob-test')
+require('./docuset-test')
+require('./inmem')
+require('./snob-test-replicate')
+require('./replication-test')
View
73 test/docuset-test.js
@@ -53,8 +53,6 @@ function contract() {
}
}
-d.on('connection', console.log)
-
d.createRawHandler(validCon)(toD, toE)
e.createRawHandler(function (con){
@@ -88,40 +86,43 @@ e.createRawHandler(function (con){
//make a commit on the E side.
repo.commit(state, {parent: 'master'})
- c.validate()
-
- a.deepEqual(d.repos.test.getRevs('master'), repo.getRevs('master'))
-
- a.deepEqual(repo.checkout('master'), state)
- a.deepEqual(
- repo.checkout('master'),
- d.repos.test.checkout('master')
- )
-
- //the repos have been syncronised!
-
- var dData = d.repos.test.checkout('master')
- dData.goodbye = ['test2']
-
- toE.once('data', c.isCalled(function (data) {
- a.has(data, ['UPDATE', 'test', 'master', []])
- }, 'UPDATE is sent 2'))
-
- e.repos.test.once('update', c.isCalled(function (revs, branch) {
- a.equal(branch, 'master')
- }, 'update is emitted'))
-
- //make a commit on the D side
-
- d.repos.test.commit(dData, {parent: 'master'})
-
- c.validate()
-
- a.deepEqual(d.repos.test.getRevs('master'), repo.getRevs('master'))
- console.log(d.repos.test.getRevs('master'), repo.getRevs('master'))
- a.equal(repo.branch('master'), d.repos.test.branch('master'))
- a.deepEqual(repo.checkout('master'), d.repos.test.checkout('master'))
-
+ //need to flush
+ process.nextTick(function () {
+ c.validate()
+
+ a.deepEqual(d.repos.test.getRevs('master'), repo.getRevs('master'))
+
+ a.deepEqual(repo.checkout('master'), state)
+ a.deepEqual(
+ repo.checkout('master'),
+ d.repos.test.checkout('master')
+ )
+
+ //the repos have been syncronised!
+
+ var dData = d.repos.test.checkout('master')
+ dData.goodbye = ['test2']
+
+ toE.once('data', c.isCalled(function (data) {
+ a.has(data, ['UPDATE', 'test', 'master', []])
+ }, 'UPDATE is sent 2'))
+
+ e.repos.test.once('update', c.isCalled(function (revs, branch) {
+ a.equal(branch, 'master')
+ }, 'update is emitted'))
+
+ //make a commit on the D side
+
+ d.repos.test.commit(dData, {parent: 'master'})
+ process.nextTick(function () {
+ c.validate()
+
+ a.deepEqual(d.repos.test.getRevs('master'), repo.getRevs('master'))
+ console.log(d.repos.test.getRevs('master'), repo.getRevs('master'))
+ a.equal(repo.branch('master'), d.repos.test.branch('master'))
+ a.deepEqual(repo.checkout('master'), d.repos.test.checkout('master'))
+ })
+ })
})(toE, toD)
View
65 test/inmem.js
@@ -4,23 +4,54 @@ var assert = require('assert')
var server = d().createServer().listen()
var client = d().connect(server)
+function logger (name) {
+ return function (m,n) {
+ console.log(name+":", m,n)
+ }
+}
+
var r = client.sub('hello')
//would be nice to just default to {parent: 'master'}
//oh, will probably be adding author too...
-r.commit({a: true})
-
-r.commit({a: 2})
-
-var m = server.repos.hello.checkout()
-
-assert.deepEqual(m, {a: 2})
-
-// a second connection to the hello document!
-
-var client2 = d().connect(server)
-var r2 = client2.sub('hello')
-assert.deepEqual(r2.checkout(), {a: 2})
-
-r2.commit({a: 2, b: 3})
-
-assert.deepEqual(r.checkout(), {a: 2, b: 3})
+//r.on('update', logger('1'))
+
+var a2 = r.commit({a: true})
+var a2 = r.commit({a: 2})
+
+function wait (repo, commitish, fn) {
+ function handler (revs) {
+ var last = revs[revs.length - 1].id
+ console.log('at', last)
+ if(last !== commitish) return
+
+ repo.removeListener('update', handler)
+ fn(revs)
+ }
+ repo.on('update', handler)
+}
+
+server.once('new', function (r0) {
+ console.log('new', r0)
+ wait(r0, a2.id, function () {
+ assert.deepEqual(r0.checkout(), {a: 2})
+ console.log('here')
+ // a second connection to the hello document!
+ var client2 = d().connect(server)
+ var r2 = client2.sub('hello')
+ var n = 1
+
+ /*r.once('update', function () {
+ assert.deepEqual(r.checkout(), {a: 2})
+ next()
+ })*/
+ r2.once('update', function () {
+ assert.deepEqual(r2.checkout(), {a: 2})
+ r2.commit({a: 2, b: 3})
+
+ r.once('update', function () {
+ assert.deepEqual(r.checkout(), {a: 2, b: 3})
+ console.log('PASSED')
+ })
+ })
+ })
+})
View
77 test/protocol.js
@@ -0,0 +1,77 @@
+/*
+ TEST THAT QUERIES PRODUCE THE CORRECT RESPONSES
+
+ write a message
+ call a function
+*/
+
+var Docuset = require('../docuset')
+var es = require('event-stream')
+var a = require('assertions')
+var Repo = require('../')
+
+function assertStream() {
+ var c = 0
+ var stream = es.mapSync(function (data) {
+ console.log(data, stream.queue)
+ c ++
+ if(!stream.queue.length)
+ throw new Error('need an assertion for chunk ' + c)
+ var exp = stream.queue.shift()
+ if('object' === typeof a)
+ a.has(data, exp)
+ else if('function' == typeof a)
+ exp(data)
+ else
+ throw new Error('assertion should be object or function for chunk' + c)
+ })
+ stream.queue = []
+ return stream
+}
+
+function assertDocuset (name, test) {
+ var d = new Docuset()
+ var ins = es.stringify()
+ var outs = es.parse()
+ var d = new Docuset()
+ d.createHandler()(ins, outs)
+ var as = assertStream()
+ outs.pipe(as)
+ console.log('TEST:', name)
+ test(d, ins, as)
+}
+
+assertDocuset('simple', function (d, ins, out) {
+ out.queue.push(['UPDATE', 'hello', 'master', a._isArray()])
+ ins.write(['SUB', 'hello', {}])
+ d.createRepo('hello').commit({a: 1})
+})
+
+assertDocuset('sub after', function (d, ins, out) {
+ out.queue.push(['UPDATE', 'hello', 'master', a._isArray()])
+ d.createRepo('hello').commit({a: 1})
+ ins.write(['SUB', 'hello', {}])
+})
+
+assertDocuset('sub after 2 commits', function (d, ins, out) {
+ out.queue.push(['UPDATE', 'hello', 'master', a._property('length', 2)])
+ var r = d.createRepo('hello')
+ r.commit({a: 1})
+ r.commit({a: 2})
+ ins.write(['SUB', 'hello', {}])
+})
+
+assertDocuset('sub then update', function (d, ins, out) {
+
+ var r = new Repo()
+ var a1 = r.commit({a: 1})
+ var a2 = r.commit({a: 2})
+
+ ins.write(['SUB', 'hello', {}])
+ ins.write(['UPDATE', 'hello', 'master', [a1, a2]])
+ //this should not write back the update.
+ //because it should remember that I've sent those updates.
+ //but currently, it calls writes 'update' before it returns
+ //from recieve. need to queue that instead.
+})
+
View
2  test/snob-test.js
@@ -78,6 +78,8 @@ assert.throws(function () {
_world.hello.push('WTF!?')
snob.branch('branchy', init.id)
+assert.notDeepEqual(snob.checkout('branchy'), _world)
+
var branch = snob.commit(_world, {
message: 'branch',
parent: 'branchy'
View
18 test/sync.js
@@ -0,0 +1,18 @@
+
+
+var D = require('../docuset')
+var d = D()
+d.createServer().listen(4242)
+var r = d.createRepo('t')
+var s = D().connect(4242).sub('t')
+
+
+var obj = {}
+var obj2 = {}
+
+r.sync(obj)
+s.sync(obj2)
+obj.h = {__id__: 'onaehu'}
+setTimeout(function (e) {
+ obj2.h = [undefined]
+}, 2)
Please sign in to comment.
Something went wrong with that request. Please try again.