Skip to content

Commit

Permalink
add new test case
Browse files Browse the repository at this point in the history
  • Loading branch information
oberstet committed Apr 15, 2017
1 parent 4701ec6 commit 6cad8f2
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ test_connect:

test_serialization_cbor:
nodeunit test/test_serialization_cbor.js

test_pubsub_multiple_matching_subs:
nodeunit test/test_pubsub_multiple_matching_subs.js
3 changes: 2 additions & 1 deletion lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,9 @@ var Session = function (socket, defer, onchallenge) {
);

for (var i = 0; i < subs.length; ++i) {
var sub = subs[i];
try {
subs[i].handler(args, kwargs, ed);
sub.handler(args, kwargs, ed, sub);
} catch (e) {
log.debug("Exception raised in event handler", e);
}
Expand Down
3 changes: 2 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var pubsub_exclude = require('./test_pubsub_exclude.js');
var pubsub_eligible = require('./test_pubsub_eligible.js');
var pubsub_prefix_sub = require('./test_pubsub_prefix_sub.js');
var pubsub_wildcard_sub = require('./test_pubsub_wildcard_sub.js');

var pubsub_multiple_matching_subs = require('./test_pubsub_multiple_matching_subs.js');

exports.testSync = sync.testSync;
exports.testAsync = async.testAsync;
Expand Down Expand Up @@ -69,3 +69,4 @@ exports.testPubsubExclude = pubsub_exclude.testPubsubExclude;
exports.testPubsubEligible = pubsub_eligible.testPubsubEligible;
exports.testPubsubPrefixSub = pubsub_prefix_sub.testPubsubPrefixSub;
exports.testPubsubWildcardSub = pubsub_wildcard_sub.testPubsubWildcardSub;
exports.testPubsubMultipleMatchingSubs = pubsub_multiple_matching_subs.testPubsubMultipleMatchingSubs;
180 changes: 180 additions & 0 deletions test/test_pubsub_multiple_matching_subs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
///////////////////////////////////////////////////////////////////////////////
//
// AutobahnJS - http://autobahn.ws, http://wamp.ws
//
// A JavaScript library for WAMP ("The Web Application Messaging Protocol").
//
// Copyright (c) Crossbar.io Technologies GmbH and contributors
//
// Licensed under the MIT License.
// http://www.opensource.org/licenses/mit-license.php
//
///////////////////////////////////////////////////////////////////////////////

var autobahn = require('./../index.js');
var testutil = require('./testutil.js');

/*
From https://github.com/crossbario/crossbar/issues/1038:
I used a simple NodeJS/Autobahn 0.12 client for testing:
* Client A: Publish messages to com.myapp.topic1
* Client B: Subscribe to com..topic1 with wildcard matching
* Client C: Subscribe to com.myapp with prefix matching
* Client D: Subscribe to com with prefix matching
* Client E: Subscribe to com.myapp.topic1 with exact matching
*/

exports.testPubsubMultipleMatchingSubs = function (testcase) {

testcase.expect(1);

var test = new testutil.Testlog("test/test_pubsub_multiple_matching_subs.txt");

var dl = testutil.connect_n(5);

function publish_later (session, delay, topic, args, kwargs, options) {

// create a deferred
var d = autobahn.when.defer();

// resolve the promise after 1s
setTimeout(function () {
options.acknowledge = true;
session.publish(topic, args, kwargs, options).then(
function (res) {
d.resolve(res);
},
function (err) {
d.reject(err);
}
);
}, delay);

// need to return the promise
return d.promise;
}

autobahn.when.all(dl).then(
function (res) {
test.log("all sessions connected");

var session_a = res[0];
var session_b = res[1];
var session_c = res[2];
var session_d = res[3];
var session_e = res[4];

session_a._ident = 'A';
session_b._ident = 'B';
session_c._ident = 'C';
session_d._ident = 'D';
session_e._ident = 'E';

var received = 0;

function shutdown () {
if (received >= 9) {
test.log("closing ..");

session_e.leave();
session_d.leave();
session_c.leave();
session_b.leave();
session_a.leave();

var chk = test.check()
testcase.ok(!chk, chk);
testcase.done();
} else {
console.log('received ' + received + ' events up to this point ..');
}
}

function on_event_b (args, kwargs, details, sub) {
test.log('event_b: args=', args, ', kwargs=', kwargs);
test.log('subscription: sub_topic=', sub.topic, ', sub_match=', sub.options.match, ', session_ident=', sub.session._ident);
received += 1;
shutdown();
}

function on_event_c (args, kwargs, details, sub) {
test.log('event_c: args=', args, ', kwargs=', kwargs);
test.log('subscription: sub_topic=', sub.topic, ', sub_match=', sub.options.match, ', session_ident=', sub.session._ident);
received += 1;
shutdown();
}

function on_event_d (args, kwargs, details, sub) {
test.log('event_d: args=', args, ', kwargs=', kwargs);
test.log('subscription: sub_topic=', sub.topic, ', sub_match=', sub.options.match, ', session_ident=', sub.session._ident);
received += 1;
shutdown();
}

function on_event_e (args, kwargs, details, sub) {
test.log('event_e: args=', args, ', kwargs=', kwargs);
test.log('subscription: sub_topic=', sub.topic, ', sub_match=', sub.options.match, ', session_ident=', sub.session._ident);
received += 1;
shutdown();
}

var dl2 = [];

dl2.push(session_b.subscribe('com..topic1', on_event_b, {match: 'wildcard'}));
dl2.push(session_c.subscribe('com.myapp', on_event_c, {match: 'prefix'}));
dl2.push(session_d.subscribe('com', on_event_d, {match: 'prefix'}));
dl2.push(session_e.subscribe('com.myapp.topic1', on_event_e, {match: 'exact'}));

autobahn.when.all(dl2).then(
function (subs) {
console.log('all test sessions subscribed!');
for (var i = 0; i < subs.length; ++i) {
console.log('session_ident=' + subs[i].session._ident + ' subscribed with topic=' + subs[i].topic + ', match=' + subs[i].options.match);
}

console.log('publishing test events ..');

var options = {acknowledge: true};

var msg = 'hello subscriber!';
var counter = 1;
var dl3 = [];

// these are received in sessions b-e
dl3.push(publish_later(session_a, 100, 'com.myapp.topic1', [msg, counter++], null, options));

// these are received in sessions c and d
dl3.push(publish_later(session_a, 200, 'com.myapp.topic123', [msg, counter++], null, options));

// these are received in sessions c and d
dl3.push(publish_later(session_a, 300, 'com.myapp.topic1.foobar', [msg, counter++], null, options));

// these are received in no session at all
dl3.push(publish_later(session_a, 400, 'de.myapp.topic1', [msg, counter++], null, options));

// these are received in session d only
dl3.push(publish_later(session_a, 500, 'com.foobar', [msg, counter++], null, options));

autobahn.when.all(dl3).then(
function (res) {
console.log('all test events published!');
},
function (err) {
console.log(err);
}
);
},
function (err) {
console.log(err);
}
);
},
function (err) {
test.log(err);
}
);
}
20 changes: 20 additions & 0 deletions test/test_pubsub_multiple_matching_subs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
0 "all sessions connected"
1 "event_e: args=" ["hello subscriber!",1] ", kwargs=" {}
2 "subscription: sub_topic=" "com.myapp.topic1" ", sub_match=" "exact" ", session_ident=" "E"
3 "event_d: args=" ["hello subscriber!",1] ", kwargs=" {}
4 "subscription: sub_topic=" "com" ", sub_match=" "prefix" ", session_ident=" "D"
5 "event_c: args=" ["hello subscriber!",1] ", kwargs=" {}
6 "subscription: sub_topic=" "com.myapp" ", sub_match=" "prefix" ", session_ident=" "C"
7 "event_b: args=" ["hello subscriber!",1] ", kwargs=" {}
8 "subscription: sub_topic=" "com..topic1" ", sub_match=" "wildcard" ", session_ident=" "B"
9 "event_d: args=" ["hello subscriber!",2] ", kwargs=" {}
10 "subscription: sub_topic=" "com" ", sub_match=" "prefix" ", session_ident=" "D"
11 "event_c: args=" ["hello subscriber!",2] ", kwargs=" {}
12 "subscription: sub_topic=" "com.myapp" ", sub_match=" "prefix" ", session_ident=" "C"
13 "event_d: args=" ["hello subscriber!",3] ", kwargs=" {}
14 "subscription: sub_topic=" "com" ", sub_match=" "prefix" ", session_ident=" "D"
15 "event_c: args=" ["hello subscriber!",3] ", kwargs=" {}
16 "subscription: sub_topic=" "com.myapp" ", sub_match=" "prefix" ", session_ident=" "C"
17 "event_d: args=" ["hello subscriber!",5] ", kwargs=" {}
18 "subscription: sub_topic=" "com" ", sub_match=" "prefix" ", session_ident=" "D"
19 "closing .."

0 comments on commit 6cad8f2

Please sign in to comment.