Skip to content

Commit

Permalink
test: null terminated strings
Browse files Browse the repository at this point in the history
PR: #75
  • Loading branch information
Nick Desaulniers authored and reqshark committed Mar 26, 2015
1 parent 7a64664 commit 727f552
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions test/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,73 @@ test('send returns number of bytes queued for unbound socket', function (t) {
sock.close();
});

test('send can take a string', function (t) {
t.plan(3);

var pub = nano.socket('pub');
var sub = nano.socket('sub');
var addr = 'inproc://some_address';
var msg = 'hi';

pub.bind(addr);
sub.connect(addr);

sub.on('message', function (buf) {
t.equal(buf.toString(), msg);
t.equal(buf.length, msg.length);
pub.close();
sub.close();
});

var bytes = pub.send(msg);
t.equal(bytes, msg.length);
});

test('send can take a buffer', function (t) {
t.plan(3);

var pub = nano.socket('pub');
var sub = nano.socket('sub');
var addr = 'inproc://some_address';
var msg = new Buffer('hello');

pub.bind(addr);
sub.connect(addr);

sub.on('message', function (buf) {
t.equal(buf.toString(), msg.toString());
t.equal(buf.length, msg.length);
pub.close();
sub.close();
});

var bytes = pub.send(msg);
t.equal(bytes, msg.length);
});

test('should not null terminate when sending strings', function (t) {
t.plan(3);

var pub = nano.socket('pub');
var sub = nano.socket('sub');
var addr = 'inproc://some_address';
var msg = 'hello';

pub.bind(addr);
sub.connect(addr);

sub.on('message', function (buf) {
if (buf[buf.length - 1] === 0) {
t.fail();
}
t.equal(buf.equals(new Buffer(msg)), true);
t.equal(buf.length, msg.length);
pub.close();
sub.close();
});

var bytes = pub.send(msg);
t.equal(bytes, msg.length);

});

0 comments on commit 727f552

Please sign in to comment.