Skip to content

Commit

Permalink
a first pubsub test
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Oberstein committed Apr 19, 2014
1 parent 4961df1 commit f168dd5
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
2 changes: 2 additions & 0 deletions package/test/test.js
Expand Up @@ -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;
Expand All @@ -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;
67 changes: 67 additions & 0 deletions 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);
}
);
}
14 changes: 14 additions & 0 deletions 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 .."

0 comments on commit f168dd5

Please sign in to comment.