Permalink
Browse files

new binary event test for end events

  • Loading branch information...
1 parent 3c91107 commit 8b75d18265d7a2633bbf159a0753d1ba7270c3c3 @substack committed Jun 1, 2010
Showing with 72 additions and 0 deletions.
  1. +72 −0 tests/binary_event.js
View
@@ -0,0 +1,72 @@
+#!/usr/bin/env node
+
+var sys = require('sys');
+var net = require('net');
+var assert = require('assert');
+
+var BufferList = require('bufferlist').BufferList;
+var Binary = require('bufferlist/binary').Binary;
+
+function parser (sock) {
+ var bufferList = new BufferList;
+ sock.addListener('data', function (data) {
+ bufferList.push(data);
+ });
+
+ return Binary(bufferList)
+ .getWord16be('xLen')
+ .when('xLen', 0, function (vars) {
+ this
+ .getWord32le('msgLen')
+ .getBuffer('msg', 'msgLen')
+ .tap(function (vars) {
+ vars.moo = 'msg:' + vars.msgLen + ':' + vars.msg;
+ })
+ .exit()
+ ;
+ })
+ .getBuffer('xs', 'xLen')
+ .tap(function (vars) {
+ vars.moo = 'xs:' + vars.xLen + ':' + vars.xs;
+ })
+ .end()
+ ;
+}
+
+function serverSession (port, strings, moo) {
+ // fire up a server to write the strings when a client connects
+ var server = net.createServer(function (stream) {
+ strings.forEach(function (s) {
+ stream.write(s);
+ });
+ stream.end();
+ server.close();
+ });
+ server.listen(port);
+
+ // connect to the server and parse its output
+ var client = new net.Stream;
+ parser(client).addListener('end', function (vars) {
+ assert.equal(
+ moo, vars.moo,
+ 'moo != ' + sys.inspect(moo) + ', moo == ' + sys.inspect(vars.moo)
+ );
+ parsed ++;
+ client.end();
+ });
+ client.connect(port);
+}
+
+var parsed = 0;
+
+serverSession(20801,
+ ['\x00','\x04m','eow'],
+ 'xs:4:meow'
+);
+serverSession(20802,
+ ['\x00\x00','\x12\x00\x00\x00happy pur','ring c','ats'],
+ 'msg:18:happy purring cats'
+);
+setTimeout(function () {
+ assert.equal(parsed, 2, 'parsed != 2, parsed == ' + parsed);
+}, 500);

0 comments on commit 8b75d18

Please sign in to comment.