Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

split and ported tests over into browser stuff

  • Loading branch information...
commit d7c6c81dfbddf605121f4076f956a729016e1267 1 parent 174a7c0
@substack authored
View
4 package.json
@@ -21,10 +21,10 @@
"tape" : "~0.1.1"
},
"scripts" : {
- "test" : "tap test/*.js"
+ "test" : "tap test/*.js test/server/*.js"
},
"testling" : {
- "files" : ["test/args.js"],
+ "files" : "test/*.js",
"browsers" : {
"iexplore" : [ "6.0", "7.0", "8.0", "9.0" ],
"chrome" : [ "20.0" ],
View
17 test/_id.js
@@ -1,18 +1,13 @@
var dnode = require('../');
-var test = require('tap').test;
+var test = require('tape');
test('_id', function (t) {
t.plan(1);
- var port = Math.floor(Math.random() * 40000 + 10000);
- var server = dnode({ _id : 1337 }).listen(port);
-
- server.on('listening', function () {
- dnode.connect(port, function (remote, conn) {
- t.equal(remote._id, 1337);
- conn.end();
- server.close();
- t.end();
- });
+ var server = dnode({ _id : 1337 });
+ var client = dnode();
+ client.on('remote', function (remote, conn) {
+ t.equal(remote._id, 1337);
});
+ client.pipe(server).pipe(client);
});
View
37 test/bidirectional.js
@@ -1,41 +1,30 @@
var dnode = require('../');
-var test = require('tap').test;
+var test = require('tape');
test('bidirectional', function (t) {
- t.plan(5);
- var port = Math.floor(Math.random() * 40000 + 10000);
- var counts = { timesX : 0, clientX : 0, x : 0 };
+ t.plan(3);
var server = dnode(function (client) {
this.timesX = function (n,f) {
t.equal(n, 3, "timesX's n == 3");
- counts.timesX ++;
+
client.x(function (x) {
t.equal(x, 20, 'client.x == 20');
- counts.clientX ++;
f(n * x);
});
};
- }).listen(port);
+ });
- server.on('listening', function () {
- dnode({
- x : function (f) {
- counts.x ++;
- f(20);
- }
- }).connect(port, function (remote, conn) {
- remote.timesX(3, function (res) {
- t.equal(res, 60, 'result of 20 * 3 == 60');
- conn.end();
- server.close();
- });
+ var client = dnode({
+ x : function (f) { f(20) }
+ });
+ client.on('remote', function (remote) {
+ remote.timesX(3, function (res) {
+ t.equal(res, 60, 'result of 20 * 3 == 60');
+ client.end();
+ server.end();
});
});
- server.once('close', function () {
- t.equal(counts.timesX, 1, 'timesX called once');
- t.equal(counts.clientX, 1, 'clientX called once');
- t.end();
- });
+ client.pipe(server).pipe(client);
});
View
89 test/broadcast.js
@@ -1,61 +1,63 @@
var dnode = require('../');
var EventEmitter = require('events').EventEmitter;
var test = require('tap').test;
-var destroy = require('./lib/destroy');
test('broadcast', function (t) {
t.plan(3);
- var port = Math.floor(Math.random() * 40000 + 10000);
var em = new EventEmitter;
- var server = dnode(function (client, conn) {
- conn.on('ready', function () {
- em.on('message', client.message);
+ var server = function () {
+ return dnode(function (client, conn) {
+ conn.on('ready', function () {
+ em.on('message', client.message);
+ });
+
+ conn.on('end', function () {
+ em.removeListener('message', client.message);
+ });
+
+ this.message = function (msg) {
+ em.emit('message', client.name + ' says: ' + msg);
+ };
});
-
- conn.on('end', function () {
- em.removeListener('message', client.message);
- });
-
- this.message = function (msg) {
- em.emit('message', client.name + ' says: ' + msg);
- };
- }).listen(port);
+ };
var recv = { 0 : [], 1 : [], 2 : [] };
- server.on('listening', function () {
- dnode({
- name : '#0',
- message : function (msg) { recv[0].push(msg) },
- }).connect(port, function (remote) {
- setTimeout(function () {
- remote.message('hello!');
- }, 25);
- });
-
- dnode({
- name : '#1',
- message : function (msg) { recv[1].push(msg) },
- }).connect(port, function (remote) {
- setTimeout(function () {
- remote.message('hey');
- }, 50);
- });
-
- dnode({
- name : '#2',
- message : function (msg) { recv[2].push(msg) },
- }).connect(port, function (remote) {
- setTimeout(function () {
- remote.message('wowsy');
- }, 75);
- });
+ var client0 = dnode({
+ name : '#0',
+ message : function (msg) { recv[0].push(msg) },
+ });
+ client0.on('remote', function (remote) {
+ setTimeout(function () {
+ remote.message('hello!');
+ }, 25);
+ });
+ client0.pipe(server()).pipe(client0);
+
+ var client1 = dnode({
+ name : '#1',
+ message : function (msg) { recv[1].push(msg) },
+ });
+ client1.on('remote', function (remote) {
+ setTimeout(function () {
+ remote.message('hey');
+ }, 50);
+ });
+ client1.pipe(server()).pipe(client1);
+
+ var client2 = dnode({
+ name : '#2',
+ message : function (msg) { recv[2].push(msg) },
+ });
+ client2.on('remote', function (remote) {
+ setTimeout(function () {
+ remote.message('wowsy');
+ }, 75);
});
+ client2.pipe(server()).pipe(client2);
setTimeout(function () {
- destroy(server);
-
t.deepEqual(
recv[0],
[ '#0 says: hello!', '#1 says: hey', '#2 says: wowsy' ],
@@ -63,6 +65,5 @@ test('broadcast', function (t) {
);
t.deepEqual(recv[0], recv[1], "#1 didn't get the messages");
t.deepEqual(recv[0], recv[2], "#2 didn't get the messages");
- t.end();
}, 150);
});
View
31 test/circular.js
@@ -1,9 +1,8 @@
var dnode = require('../');
-var test = require('tap').test;
+var test = require('tape');
test('circular refs', function (t) {
t.plan(7);
- var port = Math.floor(Math.random() * 40000 + 10000);
var server = dnode({
sendObj : function (ref, f) {
@@ -15,22 +14,20 @@ test('circular refs', function (t) {
f(ref);
},
- }).listen(port);
+ });
- server.on('listening', function () {
- dnode.connect(port, function (remote, conn) {
- var obj = { a : 1, b : 2 };
- obj.c = obj;
-
- remote.sendObj(obj, function (ref) {
- t.equal(ref.a, 1);
- t.equal(ref.b, 2);
- t.deepEqual(ref.c, ref);
- t.deepEqual(ref.d, ref);
- conn.end();
- server.close();
- t.end();
- });
+ var client = dnode();
+ client.on('remote', function (remote) {
+ var obj = { a : 1, b : 2 };
+ obj.c = obj;
+
+ remote.sendObj(obj, function (ref) {
+ t.equal(ref.a, 1);
+ t.equal(ref.b, 2);
+ t.deepEqual(ref.c, ref);
+ t.deepEqual(ref.d, ref);
});
});
+
+ client.pipe(server).pipe(client);
});
View
42 test/double.js
@@ -1,5 +1,5 @@
var dnode = require('../');
-var test = require('tap').test;
+var test = require('tape');
test('double', function (t) {
t.plan(4);
@@ -13,28 +13,26 @@ test('double', function (t) {
})
})
}
- }).listen(port);
+ });
- server.on('listening', function () {
- dnode.connect(port, function (remote, conn) {
- remote.z(
- function (x,f) { f(x * 2) },
- function (x,f) { f(x / 2) },
- function (x,y) {
- t.equal(x, 20, 'double, not equal');
- t.equal(y, 5, 'double, not equal');
- }
- );
-
- function plusTen(n,f) { f(n + 10) }
-
- remote.z(plusTen, plusTen, function (x,y) {
- t.equal(x, 20, 'double, equal');
- t.equal(y, 20, 'double, equal');
- conn.end();
- server.close();
- t.end();
- });
+ var client = dnode();
+ client.on('remote', function (remote) {
+ remote.z(
+ function (x,f) { f(x * 2) },
+ function (x,f) { f(x / 2) },
+ function (x,y) {
+ t.equal(x, 20, 'double, not equal');
+ t.equal(y, 5, 'double, not equal');
+ }
+ );
+
+ function plusTen(n,f) { f(n + 10) }
+
+ remote.z(plusTen, plusTen, function (x,y) {
+ t.equal(x, 20, 'double, equal');
+ t.equal(y, 20, 'double, equal');
});
});
+
+ client.pipe(server).pipe(client);
});
View
91 test/emit.js
@@ -1,10 +1,9 @@
-var test = require('tap').test;
+var test = require('tape');
var EventEmitter = require('events').EventEmitter;
var dnode = require('../');
test('emit events', function (t) {
t.plan(2);
- var port = Math.floor(1e4 + (Math.random() * 5e4 - 1e4));
var subs = [];
function publish (name) {
@@ -14,55 +13,53 @@ test('emit events', function (t) {
});
}
- var server = dnode(function (remote, conn) {
- this.subscribe = function (emit) {
- subs.push({ emit : emit, id : conn.id });
-
- conn.on('end', function () {
- for (var i = 0; i < subs.length; i++) {
- if (subs.id === conn.id) {
- subs.splice(i, 1);
- break;
+ var server = function () {
+ return dnode(function (remote, conn) {
+ this.subscribe = function (emit) {
+ subs.push({ emit : emit, id : conn.id });
+
+ conn.on('end', function () {
+ for (var i = 0; i < subs.length; i++) {
+ if (subs.id === conn.id) {
+ subs.splice(i, 1);
+ break;
+ }
}
- }
- });
- };
- }).listen(port);
+ });
+ };
+ });
+ };
- server.on('listening', function () {
- setTimeout(function () {
- var iv = setInterval(function () {
- publish('data', Math.floor(Math.random() * 100));
- }, 20);
-
- server.on('close', function () {
- clearInterval(iv);
- });
+ setTimeout(function () {
+ var iv = setInterval(function () {
+ publish('data', Math.floor(Math.random() * 100));
}, 20);
- var xs = [];
- var x = dnode.connect(port, function (remote) {
- var em = new EventEmitter;
- em.on('data', function (n) { xs.push(n) });
- remote.subscribe(em.emit.bind(em));
- });
-
- var ys = [];
- var y = dnode.connect(port, function (remote) {
- var em = new EventEmitter;
- em.on('data', function (n) { ys.push(n) });
- remote.subscribe(em.emit.bind(em))
+ t.on('end', function () {
+ clearInterval(iv);
});
-
- setTimeout(function () {
- t.ok(xs.length > 5);
- t.deepEqual(xs, ys);
-
- x.end();
- y.end();
-
- server.close();
- t.end();
- }, 200);
+ }, 20);
+
+ var xs = [];
+ var x = dnode();
+ x.on('remote', function (remote) {
+ var em = new EventEmitter;
+ em.on('data', function (n) { xs.push(n) });
+ remote.subscribe(em.emit.bind(em));
+ });
+ x.pipe(server()).pipe(x);
+
+ var ys = [];
+ var y = dnode();
+ y.on('remote', function (remote) {
+ var em = new EventEmitter;
+ em.on('data', function (n) { ys.push(n) });
+ remote.subscribe(em.emit.bind(em))
});
+ y.pipe(server()).pipe(y);
+
+ setTimeout(function () {
+ t.ok(xs.length > 5);
+ t.deepEqual(xs, ys);
+ }, 200);
});
View
19 test/middleware.js
@@ -1,10 +1,9 @@
var dnode = require('../');
-var test = require('tap').test;
+var test = require('tape');
test('middleware', function (t) {
t.plan(5);
- var port = Math.floor(Math.random() * 40000 + 10000);
var tf = setTimeout(function () {
t.fail('never finished');
}, 1000);
@@ -28,7 +27,7 @@ test('middleware', function (t) {
}).bind(this));
this.baz = 42;
- }).listen(port);
+ });
server.on('local', function (client, conn) {
conn.zing = true;
@@ -41,13 +40,11 @@ test('middleware', function (t) {
});
});
- server.on('listening', function () {
- dnode.connect(port, function (remote, conn) {
- clearTimeout(tf);
- t.ok(remote.baz);
- conn.end();
- server.close();
- t.end();
- });
+ var client = dnode();
+ client.on('remote', function (remote, conn) {
+ clearTimeout(tf);
+ t.ok(remote.baz);
});
+
+ server.pipe(client).pipe(server);
});
View
60 test/nested.js
@@ -1,48 +1,46 @@
var dnode = require('../');
-var test = require('tap').test;
+var test = require('tape');
+var EventEmitter = require('events').EventEmitter;
test('nested', function (t) {
t.plan(4);
- var port = Math.floor(Math.random() * 40000 + 10000);
- var EventEmitter = require('events').EventEmitter;
-
- var server1 = dnode({
- timesTen : function (n,reply) { reply(n * 10) }
- }).listen(port);
+ var server1 = function () {
+ return dnode({
+ timesTen : function (n,reply) { reply(n * 10) }
+ });
+ };
- var server2 = dnode({
- timesTwenty : function (n,reply) { reply(n * 20) }
- }).listen(port + 1);
+ var server2 = function () {
+ return dnode({
+ timesTwenty : function (n,reply) { reply(n * 20) }
+ });
+ };
var moo = new EventEmitter;
- // Don't worry, real code does't look like this:
- server1.on('listening', function () {
- server2.on('listening', function () {
- dnode.connect(port, function (remote1, conn1) {
- dnode.connect(port + 1, function (remote2, conn2) {
- moo.on('hi', function (x) {
- remote1.timesTen(x, function (res) {
- t.equal(res, 5000, 'emitted value times ten');
- remote2.timesTwenty(res, function (res2) {
- t.equal(res2, 100000, 'result times twenty');
- conn1.end(); conn2.end();
- server1.close(); server2.close();
- t.end();
- });
- });
- });
- remote2.timesTwenty(5, function (n) {
- t.equal(n, 100);
- remote1.timesTen(0.1, function (n) {
- t.equal(n, 1);
- });
+ var client1 = dnode();
+ client1.on('remote', function (remote1, conn1) {
+ var client2 = dnode();
+ client2.on('remote', function (remote2, conn2) {
+ moo.on('hi', function (x) {
+ remote1.timesTen(x, function (res) {
+ t.equal(res, 5000, 'emitted value times ten');
+ remote2.timesTwenty(res, function (res2) {
+ t.equal(res2, 100000, 'result times twenty');
});
});
});
+ remote2.timesTwenty(5, function (n) {
+ t.equal(n, 100);
+ remote1.timesTen(0.1, function (n) {
+ t.equal(n, 1);
+ });
+ });
});
+ client2.pipe(server2()).pipe(client2);
});
+ client1.pipe(server1()).pipe(client1);
setTimeout(function() {
moo.emit('hi', 500);
View
34 test/null.js
@@ -1,9 +1,8 @@
var dnode = require('../');
-var test = require('tap').test;
+var test = require('tape');
test('null', function (t) {
- t.plan(7);
- var port = Math.floor(Math.random() * 40000 + 10000);
+ t.plan(5);
var server = dnode({
empty : null,
@@ -16,25 +15,20 @@ test('null', function (t) {
t.equal(n, 5);
cb(n * 10, null);
},
- }).listen(port.toString()); // test for stringified ports too why not
+ });
- server.on('listening', function () {
- dnode.connect(port, function (remote, conn) {
- t.ok(conn.id);
- t.equal(conn.stream.remoteAddress, '127.0.0.1');
-
- remote.moo(function (x) {
- t.equal(x, 100, 'remote moo == 100');
- });
- remote.sTimesTen(5, function (m) {
- t.equal(m, 50, '5 * 10 == 50');
- remote.timesTen(m, function (n) {
- t.equal(n, 500, '50 * 10 == 500');
- conn.end();
- server.close();
- t.end();
- });
+ var client = dnode();
+ client.on('remote', function (remote) {
+ remote.moo(function (x) {
+ t.equal(x, 100, 'remote moo == 100');
+ });
+ remote.sTimesTen(5, function (m) {
+ t.equal(m, 50, '5 * 10 == 50');
+ remote.timesTen(m, function (n) {
+ t.equal(n, 500, '50 * 10 == 500');
});
});
});
+
+ server.pipe(client).pipe(server);
});
View
37 test/obj.js
@@ -1,34 +1,29 @@
var dnode = require('../');
-var test = require('tap').test;
+var test = require('tape');
test('object ref tests', function (t) {
t.plan(8);
- var port = Math.floor(Math.random() * 40000 + 10000);
-
var obj = { a : 1, b : 2, f : function (n,g) { g(n * 20) } };
var server = dnode({
getObject : function (f) { f(obj) },
- }).listen(port);
+ });
- server.on('listening', function () {
- dnode.connect(port, function (remote, conn) {
- remote.getObject(function (rObj) {
- t.equal(rObj.a, 1);
- t.equal(rObj.b, 2);
- t.equal(typeof rObj.f, 'function');
- rObj.a += 100; rObj.b += 100;
- t.equal(obj.a, 1);
- t.equal(obj.b, 2);
- t.notEqual(obj.f, rObj.g);
- t.equal(typeof obj.f, 'function');
- rObj.f(13, function (res) {
- t.equal(res, 260);
- conn.end();
- server.close();
- t.end();
- });
+ var client = dnode();
+ client.on('remote', function (remote, conn) {
+ remote.getObject(function (rObj) {
+ t.equal(rObj.a, 1);
+ t.equal(rObj.b, 2);
+ t.equal(typeof rObj.f, 'function');
+ rObj.a += 100; rObj.b += 100;
+ t.equal(obj.a, 1);
+ t.equal(obj.b, 2);
+ t.notEqual(obj.f, rObj.g);
+ t.equal(typeof obj.f, 'function');
+ rObj.f(13, function (res) {
+ t.equal(res, 260);
});
});
});
+ client.pipe(server).pipe(client);
});
View
19 test/refs.js
@@ -1,22 +1,19 @@
var dnode = require('../');
-var test = require('tap').test;
+var test = require('tape');
test('refs', function (t) {
t.plan(2);
- var port = Math.floor(Math.random() * 40000 + 10000);
var server = dnode({
a : 1,
b : 2,
- }).listen(port);
+ });
- server.on('listening', function () {
- dnode.connect(port, function (remote, conn) {
- conn.end();
- server.close();
- t.equal(remote.a, 1);
- t.equal(remote.b, 2);
- t.end();
- });
+ var client = dnode();
+ client.on('remote', function (remote) {
+ t.equal(remote.a, 1);
+ t.equal(remote.b, 2);
});
+
+ client.pipe(server).pipe(client);
});
View
32 test/self-referential.js
@@ -1,10 +1,8 @@
var dnode = require('../')
-var test = require('tap').test;
-var util = require('util');
+var test = require('tape');
test('self-referential', function (t) {
- t.plan(6);
- var port = Math.floor(Math.random() * 40000 + 10000);
+ t.plan(7);
var server = dnode({
timesTen : function (n,reply) {
@@ -18,21 +16,19 @@ test('self-referential', function (t) {
t.strictEqual(n[3],n);
reply(n);
},
- }).listen(port);
+ });
- server.on('listening', function () {
- dnode.connect(port, function (remote, conn) {
- t.equal(conn.stream.remoteAddress, '127.0.0.1');
- var args = [1,2,3]
- args.push(args)
-
- remote.print(args, function (m) {
- t.equal(util.inspect(m), util.inspect(args));
-
- conn.end();
- server.close();
- t.end();
- });
+ var client = dnode();
+ client.on('remote', function (remote) {
+ var args = [1,2,3]
+ args.push(args)
+
+ remote.print(args, function (m) {
+ t.same(m.slice(0,3), args.slice(0,3));
+ t.equal(m, m[3]);
+ t.equal(args, args[3]);
});
});
+
+ client.pipe(server).pipe(client);
});
View
40 test/server/null.js
@@ -0,0 +1,40 @@
+var dnode = require('../../');
+var test = require('tap').test;
+
+test('null', function (t) {
+ t.plan(7);
+ var port = Math.floor(Math.random() * 40000 + 10000);
+
+ var server = dnode({
+ empty : null,
+ timesTen : function (n, reply) {
+ t.equal(n, 50);
+ reply(n * 10);
+ },
+ moo : function (reply) { reply(100) },
+ sTimesTen : function (n, cb) {
+ t.equal(n, 5);
+ cb(n * 10, null);
+ },
+ }).listen(port.toString()); // test for stringified ports too why not
+
+ server.on('listening', function () {
+ dnode.connect(port, function (remote, conn) {
+ t.ok(conn.id);
+ t.equal(conn.stream.remoteAddress, '127.0.0.1');
+
+ remote.moo(function (x) {
+ t.equal(x, 100, 'remote moo == 100');
+ });
+ remote.sTimesTen(5, function (m) {
+ t.equal(m, 50, '5 * 10 == 50');
+ remote.timesTen(m, function (n) {
+ t.equal(n, 500, '50 * 10 == 500');
+ conn.end();
+ server.close();
+ t.end();
+ });
+ });
+ });
+ });
+});
View
33 test/server/stream.js
@@ -0,0 +1,33 @@
+var dnode = require('../../');
+var net = require('net');
+var test = require('tap').test;
+
+test('stream', function (t) {
+ t.plan(2);
+ var port = Math.floor(Math.random() * 40000 + 10000);
+
+ var server = net.createServer(function (stream) {
+ var d = dnode({
+ meow : function f (g) { g('cats') }
+ });
+ d.on('remote', function (remote) {
+ t.equal(remote.x, 5);
+ });
+ stream.pipe(d).pipe(stream);
+ });
+ server.listen(port);
+
+ server.on('listening', function () {
+ var d = dnode({ x : 5 });
+ d.on('remote', function (remote) {
+ remote.meow(function (cats) {
+ t.equal(cats, 'cats');
+ server.close();
+ d.end();
+ });
+ });
+
+ var stream = net.connect(port);
+ d.pipe(stream).pipe(d);
+ });
+});
View
2  test/single.js → test/server/tcp.js
@@ -1,4 +1,4 @@
-var dnode = require('../')
+var dnode = require('../../')
var test = require('tap').test;
var util = require('util');
View
2  test/unicode.js → test/server/unicode.js
@@ -1,4 +1,4 @@
-var dnode = require('../');
+var dnode = require('../../');
var test = require('tap').test;
test('unicode', function (t) {
View
2  test/unix.js → test/server/unix.js
@@ -1,4 +1,4 @@
-var dnode = require('../');
+var dnode = require('../../');
var test = require('tap').test;
test('unix', function (t) {
View
2  test/zero.js → test/server/zero.js
@@ -1,4 +1,4 @@
-var dnode = require('../');
+var dnode = require('../../');
var test = require('tap').test;
test('listen on port 0', function (t) {
View
35 test/stream.js
@@ -1,33 +1,22 @@
var dnode = require('../');
-var net = require('net');
-var test = require('tap').test;
+var test = require('tape');
test('stream', function (t) {
t.plan(2);
- var port = Math.floor(Math.random() * 40000 + 10000);
- var server = net.createServer(function (stream) {
- var d = dnode({
- meow : function f (g) { g('cats') }
- });
- d.on('remote', function (remote) {
- t.equal(remote.x, 5);
- });
- stream.pipe(d).pipe(stream);
+ var server = dnode({
+ meow : function f (g) { g('cats') }
+ });
+ server.on('remote', function (remote) {
+ t.equal(remote.x, 5);
});
- server.listen(port);
- server.on('listening', function () {
- var d = dnode({ x : 5 });
- d.on('remote', function (remote) {
- remote.meow(function (cats) {
- t.equal(cats, 'cats');
- server.close();
- d.end();
- });
+ var client = dnode({ x : 5 });
+ client.on('remote', function (remote) {
+ remote.meow(function (cats) {
+ t.equal(cats, 'cats');
});
-
- var stream = net.connect(port);
- d.pipe(stream).pipe(d);
});
+
+ client.pipe(server).pipe(client);
});
Please sign in to comment.
Something went wrong with that request. Please try again.