forked from crossbario/autobahn-js
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
5 changed files
with
207 additions
and
2 deletions.
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
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,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); | ||
} | ||
); | ||
} |
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,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 .." |