spec/javascript/engine_spec.js in faye-0.6.8 vs spec/javascript/engine_spec.js in faye-0.7.0

- old
+ new

@@ -60,29 +60,58 @@ this.engine.publish(message) }, this) setTimeout(resume, 20) }, + publish_by: function(name, message, resume) { + message = Faye.extend({clientId: this._clients[name], id: Faye.random()}, message) + this.engine.publish(message) + setTimeout(resume, 10) + }, + ping: function(name, resume) { this.engine.ping(this._clients[name]) resume() }, clock_tick: function(time, resume) { setTimeout(resume, time) }, + expect_event: function(name, event, args, resume) { + var params = [this._clients[name]].concat(args), + handler = function() {} + + this.engine.bind(event, handler) + this.expect(handler, "apply").given(undefined, params) + resume() + }, + + expect_no_event: function(name, event, args, resume) { + var params = [this._clients[name]].concat(args), + handler = function() {} + + this.engine.bind(event, handler) + this.expect(handler, "apply").exactly(0) + resume() + }, + expect_message: function(name, messages, resume) { this.assertEqual(messages, this._inboxes[name]) resume() }, expect_no_message: function(name, resume) { this.assertEqual([], this._inboxes[name]) resume() }, + check_different_messages: function(a, b, resume) { + this.assertNotSame(this._inboxes[a][0], this._inboxes[b][0]) + resume() + }, + clean_redis_db: function(resume) { this.engine.disconnect() var redis = require('redis').createClient(6379, 'localhost', {no_ready_check: true}) redis.auth(this.engineOpts.password) redis.flushall(function() { @@ -120,10 +149,15 @@ it("returns a different id every time", function() { with(this) { $R(1,7).forEach(function(i) { create_client("client" + i) }) check_num_clients(10) }}) + + it("publishes an event", function() { with(this) { + expect(engine, "trigger").given("handshake", match(/^[a-z0-9]+$/)) + create_client("dave") + }}) }}) describe("clientExists", function() { with(this) { it("returns true if the client id exists", function() { with(this) { check_client_exists("alice", true) @@ -141,24 +175,29 @@ clock_tick(700) check_client_exists("alice", false) }}) it("prolongs the life of a client", function() { with(this) { - clock_tick(330) + clock_tick(450) ping("alice") - clock_tick(330) + clock_tick(450) check_client_exists("alice", true) - clock_tick(330) + clock_tick(450) check_client_exists("alice", false) }}) }}) describe("destroyClient", function() { with(this) { it("removes the given client", function() { with(this) { destroy_client("alice") check_client_exists("alice", false) }}) + + it("publishes an event", function() { with(this) { + expect_event("alice", "disconnect", []) + destroy_client("alice") + }}) describe("when the client has subscriptions", function() { with(this) { before(function() { with(this) { this.message = {"channel": "/messages/foo", "data": "ok"} subscribe("alice", "/messages/foo") @@ -168,13 +207,53 @@ connect("alice", engine) destroy_client("alice") publish(message) expect_no_message("alice") }}) + + it("publishes an event", function() { with(this) { + expect_event("alice", "disconnect", []) + destroy_client("alice") + }}) }}) }}) + describe("subscribe", function() { with(this) { + it("publishes an event", function() { with(this) { + expect_event("alice", "subscribe", ["/messages/foo"]) + subscribe("alice", "/messages/foo") + }}) + + describe("when the client is subscribed to the channel", function() { with(this) { + before(function() { this.subscribe("alice", "/messages/foo") }) + + it("does not publish an event", function() { with(this) { + expect_no_event("alice", "subscribe", ["/messages/foo"]) + subscribe("alice", "/messages/foo") + }}) + }}) + }}) + + + describe("unsubscribe", function() { with(this) { + before(function() { this.subscribe("alice", "/messages/bar") }) + + it("does not publish an event", function() { with(this) { + expect_no_event("alice", "unsubscribe", ["/messages/foo"]) + unsubscribe("alice", "/messages/foo") + }}) + + describe("when the client is subscribed to the channel", function() { with(this) { + before(function() { this.subscribe("alice", "/messages/foo") }) + + it("publishes an event", function() { with(this) { + expect_event("alice", "unsubscribe", ["/messages/foo"]) + unsubscribe("alice", "/messages/foo") + }}) + }}) + }}) + describe("publish", function() { with(this) { before(function() { with(this) { this.message = {"channel": "/messages/foo", "data": "ok"} connect("alice", engine) connect("bob", engine) @@ -186,10 +265,20 @@ publish(message) expect_no_message("alice") expect_no_message("bob") expect_no_message("carol") }}) + + it("publishes a :publish event with a clientId", function() { with(this) { + expect_event("bob", "publish", ["/messages/foo", "ok"]) + publish_by("bob", message) + }}) + + it("publishes a :publish event with no clientId", function() { with(this) { + expect_event(null, "publish", ["/messages/foo", "ok"]) + publish(message) + }}) }}) describe("with a subscriber", function() { with(this) { before(function() { with(this) { subscribe("alice", "/messages/foo") @@ -203,10 +292,15 @@ it("delivers messages to the subscribed client", function() { with(this) { publish(message) expect_message("alice", [message]) }}) + + it("publishes a :publish event with a clientId", function() { with(this) { + expect_event("bob", "publish", ["/messages/foo", "ok"]) + publish_by("bob", message) + }}) }}) describe("with a subscriber that is removed", function() { with(this) { before(function() { with(this) { subscribe("alice", "/messages/foo") @@ -217,10 +311,15 @@ publish(message) expect_no_message("alice") expect_no_message("bob") expect_no_message("carol") }}) + + it("publishes a :publish event with a clientId", function() { with(this) { + expect_event("bob", "publish", ["/messages/foo", "ok"]) + publish_by("bob", message) + }}) }}) describe("with multiple subscribers", function() { with(this) { before(function() { with(this) { subscribe("alice", "/messages/foo") @@ -262,10 +361,15 @@ publish(message) expect_message("alice", [message]) expect_no_message("bob") expect_message("carol", [message]) }}) + + it("delivers a unique copy of the message to each client", function() { with(this) { + publish(message) + check_different_messages("alice", "carol") + }}) }}) describe("with multiple matching subscriptions for the same client", function() { with(this) { before(function() { with(this) { subscribe("alice", "/messages/*") @@ -330,10 +434,19 @@ this.engineOpts = {password: "foobared", namespace: new Date().getTime().toString()} }) after(function() { this.clean_redis_db() }) itShouldBehaveLike("faye engine") + describe("distribution", function() { with(this) { itShouldBehaveLike("distributed engine") + }}) + + describe("using a Unix socket", function() { with(this) { + before(function() { with(this) { + this.engineOpts.socket = "/tmp/redis.sock" + }}) + + itShouldBehaveLike("faye engine") }}) }}) }})