forked from timoxley/upnode
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
James Halliday
committed
Dec 17, 2011
1 parent
8ae5683
commit 460a528
Showing
2 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
var upnode = require('../'); | ||
var dnode = require('dnode'); | ||
var test = require('tap').test; | ||
|
||
test('simple', function (t) { | ||
t.plan(5); | ||
|
||
var port = Math.floor(Math.random() * 5e4 + 1e4); | ||
var up = upnode.connect(port); | ||
|
||
var messages = []; | ||
var iv = setInterval(function () { | ||
up(function (remote) { | ||
remote.time(function (t) { | ||
messages.push(t); | ||
}); | ||
}); | ||
}, 250); | ||
|
||
setTimeout(on, 500); | ||
setTimeout(function () { | ||
off(); | ||
}, 1000); | ||
setTimeout(function () { | ||
on(); | ||
}, 2000); | ||
setTimeout(function () { | ||
var r0 = messages.slice(0,3).reduce(function (acc, x) { | ||
if (x > acc.max) acc.max = x; | ||
if (x < acc.min) acc.min = x; | ||
return acc; | ||
}, { min : Infinity, max : -Infinity }); | ||
t.ok(r0.max < Date.now()); | ||
t.ok(r0.max > Date.now() - 5000); | ||
t.ok(r0.max - r0.min < 10); | ||
|
||
t.ok(messages[0] < messages[messages.length-1]); | ||
t.ok(messages.length > 5); | ||
|
||
off(); | ||
up.close(); | ||
clearInterval(iv); | ||
t.end(); | ||
}, 3000); | ||
|
||
var server; | ||
function on () { | ||
server = dnode(function (client, conn) { | ||
this.time = function (cb) { cb(Date.now()) }; | ||
}); | ||
server.use(upnode.ping); | ||
server.listen(port); | ||
} | ||
|
||
function off () { | ||
server.end(); | ||
server.close(); | ||
} | ||
}); |