diff --git a/package/test/test.js b/package/test/test.js index 39f7042f..b52019d4 100644 --- a/package/test/test.js +++ b/package/test/test.js @@ -20,6 +20,7 @@ var rpc_options = require('./test_rpc_options.js'); var rpc_progress = require('./test_rpc_progress.js'); var rpc_slowsquare = require('./test_rpc_slowsquare.js'); var rpc_routing = require('./test_rpc_routing.js'); +var pubsub_basic = require('./test_pubsub_basic.js'); exports.testRpcArguments = rpc_arguments.testRpcArguments; exports.testRpcComplex = rpc_complex.testRpcComplex; @@ -28,3 +29,4 @@ exports.testRpcOptions = rpc_options.testRpcOptions; exports.testRpcProgress = rpc_progress.testRpcProgress; exports.testRpcSlowsquare = rpc_slowsquare.testRpcSlowsquare; exports.testRpcRouting = rpc_routing.testRpcRouting; +exports.testPubsubBasic = pubsub_basic.testPubsubBasic; diff --git a/package/test/test_pubsub_basic.js b/package/test/test_pubsub_basic.js new file mode 100644 index 00000000..832c1e82 --- /dev/null +++ b/package/test/test_pubsub_basic.js @@ -0,0 +1,67 @@ +/////////////////////////////////////////////////////////////////////////////// +// +// AutobahnJS - http://autobahn.ws, http://wamp.ws +// +// A JavaScript library for WAMP ("The Web Application Messaging Protocol"). +// +// Copyright (C) 2011-2014 Tavendo GmbH, http://tavendo.com +// +// Licensed under the MIT License. +// http://www.opensource.org/licenses/mit-license.php +// +/////////////////////////////////////////////////////////////////////////////// + +var autobahn = require('./../index.js'); +var testutil = require('./testutil.js'); + + +exports.testPubsubBasic = function (testcase) { + + testcase.expect(1); + + var test = new testutil.Testlog("test/test_pubsub_basic.txt"); + + var dl = testutil.connect_n(2); + + autobahn.when.all(dl).then( + function (res) { + test.log("all sessions connected"); + + var session1 = res[0]; + var session2 = res[1]; + + var counter = 0; + + var t1 = setInterval(function () { + test.log("publishing to topic 'com.myapp.topic1': " + counter); + session1.publish('com.myapp.topic1', [counter]); + counter += 1; + }, 1000); + + var received = 0; + + var sub; + function onevent1(args) { + test.log("Got event:", args[0]); + received += 1; + if (received > 5) { + test.log("Closing .."); + + clearInterval(t1); + + session1.leave(); + session2.leave(); + + var chk = test.check() + testcase.ok(!chk, chk); + testcase.done(); + } + } + + sub = session2.subscribe('com.myapp.topic1', onevent1); + }, + function (err) { + test.log(err); + } + ); +} diff --git a/package/test/test_pubsub_basic.txt b/package/test/test_pubsub_basic.txt new file mode 100644 index 00000000..df311231 --- /dev/null +++ b/package/test/test_pubsub_basic.txt @@ -0,0 +1,14 @@ +0 "all sessions connected" +1 "publishing to topic 'com.myapp.topic1': 0" +2 "Got event:" 0 +3 "publishing to topic 'com.myapp.topic1': 1" +4 "Got event:" 1 +5 "publishing to topic 'com.myapp.topic1': 2" +6 "Got event:" 2 +7 "publishing to topic 'com.myapp.topic1': 3" +8 "Got event:" 3 +9 "publishing to topic 'com.myapp.topic1': 4" +10 "Got event:" 4 +11 "publishing to topic 'com.myapp.topic1': 5" +12 "Got event:" 5 +13 "Closing .."