Skip to content

Commit

Permalink
add pubsub tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reqshark committed Mar 16, 2015
1 parent 62868f6 commit e501d22
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 79 deletions.
20 changes: 10 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,18 +320,18 @@ Socket.prototype.rcvprio = opt('rcvprio');
Socket.prototype.chan = function(){
var args = arguments;
var channels = args.length;
while(--channels > -1) {
if(nn.Setopt(this.binding, nn.NN_SUB, nn.NN_SUB_SUBSCRIBE, args[channels]) == 0)
this.channels[args[channels]] = 'active';
while(channels--) {
if (nn.Setopt(this.binding, nn.NN_SUB, nn.NN_SUB_SUBSCRIBE, args[channels]) == 0)
this.channels[args[channels]] = true;
};
}

Socket.prototype.rmchan = function(channel){
Socket.prototype.rmchan = function(){
var args = arguments;
var channels = args.length;
while(--channels > -1) {
if(nn.Setopt(this.binding, nn.NN_SUB, nn.NN_SUB_UNSUBSCRIBE, args[channels]) == 0)
this.channels[args[channels]] = 'active';
while(channels--) {
if (nn.Setopt(this.binding, nn.NN_SUB, nn.NN_SUB_UNSUBSCRIBE, args[channels]) == 0)
this.channels[args[channels]] = true;
};
}

Expand All @@ -342,18 +342,18 @@ Socket.prototype.tcpnodelay = function (bool) {
if(bool){
if(nn.Setopt(this.binding, nn.NN_TCP, nn.NN_TCP_NODELAY, 1) > -1)
return true;
throw new Error(nn.Err() + ': '+this.type + ' nodelay@'+'activing'+'\n');
throw new Error(nn.Err() + ': '+this.type + ' nodelay@'+'activing\n');
} else {
if(nn.Setopt(this.binding, nn.NN_TCP, nn.NN_TCP_NODELAY, 0) > -1)
return false;
throw new Error(nn.Err() + ': '+this.type+' nodelay@'+'deactiving'+'\n');
throw new Error(nn.Err() + ': '+this.type+' nodelay@'+'deactiving\n');
}
} else {
switch(nn.Getopt(this.binding, nn.NN_TCP, nn.NN_TCP_NODELAY)){
case 1: return true;
case 0: return false;
default:
throw new Error(nn.Err() +': '+this.type+' nodelay@'+'getsockopt'+'\n');
throw new Error(nn.Err() +': '+this.type+' nodelay@'+'getsockopt\n');
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions src/node_nanomsg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ NAN_METHOD(Socket) {
// Invoke nanomsg function.
int ret = nn_socket(domain, protocol);

//if( (ret >= 0) && (protocol == NN_SUB)) {
//if (nn_setsockopt(ret, NN_SUB, NN_SUB_SUBSCRIBE, "", 0) != 0) {
//return NanThrowError("Could not set subscribe option.");
//}
//}

NanReturnValue(NanNew<Number>(ret));
}

Expand Down
8 changes: 5 additions & 3 deletions test/inproc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ test('inproc socket pub sub', function (t) {
var addr = 'inproc://pubsub';
var msg = 'hello world';

sub.chan('');

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

Expand Down Expand Up @@ -221,9 +223,9 @@ test('inproc multiple socket pub sub', function (t) {
var msg = 'hello world';

pub.bind(addr);
sub1.connect(addr);
sub2.connect(addr);
sub3.connect(addr);
sub1.connect(addr); sub1.chan('');
sub2.connect(addr); sub2.chan('');
sub3.connect(addr); sub3.chan('');

var responses = 0;

Expand Down
8 changes: 5 additions & 3 deletions test/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ test('ipc socket pub sub', function (t) {
pub.bind(addr);
sub.connect(addr);

sub.chan('');

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

Expand Down Expand Up @@ -206,9 +208,9 @@ test('ipc multiple socket pub sub', function (t) {
var msg = 'hello world';

pub.bind(addr);
sub1.connect(addr);
sub2.connect(addr);
sub3.connect(addr);
sub1.connect(addr); sub1.chan('');
sub2.connect(addr); sub2.chan('');
sub3.connect(addr); sub3.chan('');

var responses = 0;

Expand Down
1 change: 0 additions & 1 deletion test/mksymbols.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,3 @@
#{o}
];
FOO

101 changes: 48 additions & 53 deletions test/sockoptapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,55 @@ var test = require('tape');
test('sockopt api methods', function(t){

//set sockopts when starting the socket
var sock = nano.socket('push', {
tcpnodelay:true,
linger: 3000,
sndbuf: 202400
});
t.equal( sock.tcpnodelay(), true, 'sock.tcpnodelay() gets: true');
t.equal( sock.linger(), 3000, 'sock.linger() gets: 3000');
t.equal( sock.sndbuf(), 202400, 'sock.sndbuf() gets: 202400');
sock.tcpnodelay(false);
var sock = nano.socket('push');
//t.equal( sock.tcpnodelay(), true, 'sock.tcpnodelay() gets: true');
//t.equal( sock.linger(), 3000, 'sock.linger() gets: 3000');
//t.equal( sock.sndbuf(), 202400, 'sock.sndbuf() gets: 202400');
//sock.tcpnodelay(false);


//`socket.tcpnodelay()` method
t.equal( sock.tcpnodelay(), false, 'sock.tcpnodelay(): false');
t.equal( sock.tcpnodelay(true), true, 'sock.tcpnodelay(true) set: true');
t.equal( sock.tcpnodelay(), true, 'sock.tcpnodelay() gets: true');
t.equal( sock.tcpnodelay(false), false,'sock.tcpnodelay(false) set: false');
t.equal( sock.tcpnodelay(), false, 'sock.tcpnodelay() gets: false');

//linger
t.equal( sock.linger(5000), true, 'sock.linger(5000) sets: 5000ms');
t.equal( sock.linger(), 5000, 'sock.linger() gets: 5000');

//sndbuf
t.equal( sock.sndbuf(1024), true, 'sock.sndbuf(1024) sets: 1024 bytes');
t.equal( sock.sndbuf(), 1024, 'sock.sndbuf() gets: 1024');

//rcvbuf
t.equal( sock.rcvbuf(102400), true, 'sock.rcvbuf(102400) sets: 102400 bytes');
t.equal( sock.rcvbuf(), 102400, 'sock.rcvbuf() gets: 102400');

//sndtimeo
t.equal( sock.sndtimeo(500), true, 'sock.sndtimeo(500) sets: 500ms');
t.equal( sock.sndtimeo(), 500, 'sock.sndtimeo() gets: 500');

//rcvtimeo
t.equal( sock.rcvtimeo(200), true, 'sock.rcvtimeo(200) sets: 200ms');
t.equal( sock.rcvtimeo(), 200, 'sock.rcvtimeo() gets: 200');

//reconn
t.equal( sock.reconn(500), true, 'sock.reconn(500) sets: 500ms');
t.equal( sock.reconn(), 500, 'sock.reconn() gets: 500');

//maxreconn
t.equal( sock.maxreconn(100000), true, 'sock.maxreconn(100000) sets: 100000ms');
t.equal( sock.maxreconn(), 100000, 'sock.maxreconn() gets: 100000');

//sndprio
t.equal( sock.sndprio(3), true, 'sock.sndprio(3) sets: 3 priority');
t.equal( sock.sndprio(), 3, 'sock.sndprio() gets: 3');

//rcvprio
t.equal( sock.rcvprio(10), true, 'sock.rcvprio(10) sets: 10 priority');
t.equal( sock.rcvprio(), 10, 'sock.rcvprio() gets: 10');

sock.close();
t.end();
//t.equal( sock.tcpnodelay(), false, 'sock.tcpnodelay(): false');
//t.equal( sock.tcpnodelay(true), true, 'sock.tcpnodelay(true) set: true');
//t.equal( sock.tcpnodelay(), true, 'sock.tcpnodelay() gets: true');
//t.equal( sock.tcpnodelay(false), false,'sock.tcpnodelay(false) set: false');
//t.equal( sock.tcpnodelay(), false, 'sock.tcpnodelay() gets: false');

setTimeout(function(){
//sndbuf
t.equal( sock.sndbuf(1024), true, 'sock.sndbuf(1024) sets: 1024 bytes');
t.equal( sock.sndbuf(), 1024, 'sock.sndbuf() gets: 1024');

//rcvbuf
t.equal( sock.rcvbuf(102400), true, 'sock.rcvbuf(102400) sets: 102400 bytes');
t.equal( sock.rcvbuf(), 102400, 'sock.rcvbuf() gets: 102400');

//sndtimeo
t.equal( sock.sndtimeo(500), true, 'sock.sndtimeo(500) sets: 500ms');
t.equal( sock.sndtimeo(), 500, 'sock.sndtimeo() gets: 500');

//rcvtimeo
t.equal( sock.rcvtimeo(200), true, 'sock.rcvtimeo(200) sets: 200ms');
t.equal( sock.rcvtimeo(), 200, 'sock.rcvtimeo() gets: 200');

//reconn
t.equal( sock.reconn(500), true, 'sock.reconn(500) sets: 500ms');
t.equal( sock.reconn(), 500, 'sock.reconn() gets: 500');

//maxreconn
t.equal( sock.maxreconn(100000), true, 'sock.maxreconn(100000) sets: 100000ms');
t.equal( sock.maxreconn(), 100000, 'sock.maxreconn() gets: 100000');

//sndprio
t.equal( sock.sndprio(3), true, 'sock.sndprio(3) sets: 3 priority');
t.equal( sock.sndprio(), 3, 'sock.sndprio() gets: 3');

//rcvprio
t.equal( sock.rcvprio(10), true, 'sock.rcvprio(10) sets: 10 priority');
t.equal( sock.rcvprio(), 10, 'sock.rcvprio() gets: 10');

sock.close();
t.end();
}, 100)

})
8 changes: 5 additions & 3 deletions test/tcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ test('tcp socket pub sub', function (t) {
pub.bind(addr);
sub.connect(addr);

sub.chan('');

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

Expand Down Expand Up @@ -203,9 +205,9 @@ test('inproc multiple socket pub sub', function (t) {
var msg = 'hello world';

pub.bind(addr);
sub1.connect(addr);
sub2.connect(addr);
sub3.connect(addr);
sub1.connect(addr); sub1.chan('');
sub2.connect(addr); sub2.chan('');
sub3.connect(addr); sub3.chan('');

var responses = 0;

Expand Down
2 changes: 2 additions & 0 deletions test/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ test('inproc socket pub sub', function (t) {
pub.bind(addr);
sub.connect(addr);

sub.chan('');

sub.on('message', function (buf) {
t.equal(buf.slice(2).toString(), msg);
t.equal(buf[0], 0xFF);
Expand Down

0 comments on commit e501d22

Please sign in to comment.