1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
# encoding=utf-8
root = File.expand_path('../../..', __FILE__)
require 'rspec/em'
require root + '/spec/ruby/encoding_helper'
require root + '/lib/faye/mixins/deferrable'
require root + '/lib/faye/mixins/logging'
require root + '/lib/faye/mixins/publisher'
require root + '/lib/faye/mixins/timeouts'
require root + '/lib/faye/protocol/channel'
require root + '/lib/faye/protocol/grammar'
require root + '/lib/faye/engines/proxy'
EngineSteps = RSpec::EM.async_steps do
def disconnect_engine(&resume)
engine.disconnect
resume.call
end
def create_client(name, &resume)
@inboxes ||= {}
@clients ||= {}
engine.create_client do |client_id|
@clients[name] = client_id
@inboxes[name] ||= []
resume.call
end
end
def connect(name, engine, &resume)
engine.connect(@clients[name]) do |m|
m.each do |message|
message.delete("id")
@inboxes[name] << message
end
end
EM.add_timer(0.1, &resume)
end
def destroy_client(name, &resume)
engine.destroy_client(@clients[name], &resume)
end
def check_client_id(name, pattern, &resume)
@clients[name].should =~ pattern
resume.call
end
def check_num_clients(n, &resume)
ids = Set.new
@clients.each { |name,id| ids.add(id) }
ids.size.should == n
resume.call
end
def check_client_exists(name, exists, &resume)
engine.client_exists(@clients[name]) do |actual|
actual.should == exists
resume.call
end
end
def subscribe(name, channel, &resume)
engine.subscribe(@clients[name], channel, &resume)
end
def unsubscribe(name, channel, &resume)
engine.unsubscribe(@clients[name], channel, &resume)
end
def publish(messages, &resume)
messages = [messages].flatten
messages.each do |message|
message = { "id" => Faye::Engine.random }.merge(message)
engine.publish(message)
end
EM.add_timer(0.1, &resume)
end
def publish_by(name, message, &resume)
message = { "clientId" => @clients[name], "id" => Faye::Engine.random }.merge(message)
engine.publish(message)
EM.add_timer(0.1, &resume)
end
def ping(name, &resume)
engine.ping(@clients[name])
resume.call
end
def clock_tick(time, &resume)
EM.add_timer(time, &resume)
end
def expect_non_exclusive_event(name, event, args, engine, &resume)
params = [@clients[name]] + args
handler = lambda { |*a| }
# we don't care if the event is called for other clients
filter = lambda do |*args|
handler.call(*args) if args[0] == params[0]
end
engine.bind(event, &filter)
handler.should_receive(:call).with(*params)
resume.call
end
def expect_event(name, event, args, &resume)
params = [@clients[name]] + args
handler = lambda { |*a| }
engine.bind(event, &handler)
handler.should_receive(:call).with(*params)
resume.call
end
def expect_no_event(name, event, args, &resume)
params = [@clients[name]] + args
handler = lambda { |*a| }
engine.bind(event, &handler)
handler.should_not_receive(:call).with(*params)
resume.call
end
def expect_message(name, messages, &resume)
@inboxes[name].should == messages
resume.call
end
def expect_no_message(name, &resume)
@inboxes[name].should == []
resume.call
end
def check_different_messages(a, b, &resume)
@inboxes[a].first.should_not be_equal(@inboxes[b].first)
resume.call
end
end
shared_examples_for "faye engine" do
include EncodingHelper
include EngineSteps
def create_engine
opts = options.merge(engine_opts)
Faye::Engine::Proxy.new(opts)
end
let(:options) { { :timeout => 1 } }
let(:engine) { create_engine }
before do
Faye::Engine.ensure_reactor_running!
create_client :alice
create_client :bob
create_client :carol
end
describe :create_client do
it "returns a client id" do
create_client :dave
check_client_id :dave, /^[a-z0-9]+$/
end
it "returns a different id every time" do
1.upto(7) { |i| create_client "client#{ i }" }
check_num_clients 10
end
it "publishes an event" do
engine.should_receive(:trigger).with(:handshake, match(/^[a-z0-9]+$/)).exactly(4)
create_client :dave
end
describe :gc do
let(:options) { { :timeout => 0.3, :gc => 0.2 } }
it "doesn't prematurely remove a client after creation" do
clock_tick 0.25
check_client_exists :alice, true
end
end
end
describe :client_exists do
it "returns true if the client id exists" do
check_client_exists :alice, true
end
it "returns false if the client id does not exist" do
check_client_exists :anything, false
end
end
describe :ping do
let(:options) { { :timeout => 0.3, :gc => 0.08 } }
it "removes a client if it does not ping often enough" do
clock_tick 0.7
check_client_exists :alice, false
end
it "prolongs the life of a client" do
clock_tick 0.45
ping :alice
clock_tick 0.45
check_client_exists :alice, true
clock_tick 0.45
check_client_exists :alice, false
end
end
describe :destroy_client do
it "removes the given client" do
destroy_client :alice
check_client_exists :alice, false
end
it "publishes an event" do
expect_event :alice, :disconnect, []
destroy_client :alice
end
describe "when the client has subscriptions" do
before do
@message = { "channel" => "/messages/foo", "data" => "ok" }
subscribe :alice, "/messages/foo"
end
it "stops the client receiving messages" do
connect :alice, engine
destroy_client :alice
publish @message
expect_no_message :alice
end
it "publishes an event" do
expect_event :alice, :disconnect, []
destroy_client :alice
end
end
end
describe :subscribe do
it "publishes an event" do
expect_event :alice, :subscribe, ["/messages/foo"]
subscribe :alice, "/messages/foo"
end
describe "when the client is subscribed to the channel" do
before { subscribe :alice, "/messages/foo" }
it "does not publish an event" do
expect_no_event :alice, :subscribe, ["/messages/foo"]
subscribe :alice, "/messages/foo"
end
end
end
describe :unsubscribe do
before { subscribe :alice, "/messages/bar" }
it "does not publish an event" do
expect_no_event :alice, :unsubscribe, ["/messages/foo"]
unsubscribe :alice, "/messages/foo"
end
describe "when the client is subscribed to the channel" do
before { subscribe :alice, "/messages/foo" }
it "publishes an event" do
expect_event :alice, :unsubscribe, ["/messages/foo"]
unsubscribe :alice, "/messages/foo"
end
end
end
describe :publish do
before do
@message = { "channel" => "/messages/foo", "data" => "ok", "blank" => nil }
connect :alice, engine
connect :bob, engine
connect :carol, engine
end
describe "with no subscriptions" do
it "delivers no messages" do
publish @message
expect_no_message :alice
expect_no_message :bob
expect_no_message :carol
end
it "publishes a :publish event with a clientId" do
expect_event :bob, :publish, ["/messages/foo", "ok"]
publish_by :bob, @message
end
it "publishes a :publish event with no clientId" do
expect_event nil, :publish, ["/messages/foo", "ok"]
publish @message
end
end
describe "with a subscriber" do
before { subscribe :alice, "/messages/foo" }
it "delivers messages to the subscribed client" do
publish @message
expect_message :alice, [@message]
end
it "delivers multibyte messages correctly" do
@message["data"] = encode "Apple = "
publish @message
expect_message :alice, [@message]
end
it "publishes a :publish event" do
expect_event :bob, :publish, ["/messages/foo", "ok"]
publish_by :bob, @message
end
end
describe "with a subscriber that is removed" do
before do
subscribe :alice, "/messages/foo"
unsubscribe :alice, "/messages/foo"
end
it "does not deliver messages to unsubscribed clients" do
publish @message
expect_no_message :alice
expect_no_message :bob
expect_no_message :carol
end
it "publishes a :publish event" do
expect_event :bob, :publish, ["/messages/foo", "ok"]
publish_by :bob, @message
end
end
describe "with multiple subscribers" do
before do
subscribe :alice, "/messages/foo"
subscribe :bob, "/messages/bar"
subscribe :carol, "/messages/foo"
end
it "delivers messages to the subscribed clients" do
publish @message
expect_message :alice, [@message]
expect_no_message :bob
expect_message :carol, [@message]
end
end
describe "with a single wildcard" do
before do
subscribe :alice, "/messages/*"
subscribe :bob, "/messages/bar"
subscribe :carol, "/*"
end
it "delivers messages to matching subscriptions" do
publish @message
expect_message :alice, [@message]
expect_no_message :bob
expect_no_message :carol
end
end
describe "with a double wildcard" do
before do
subscribe :alice, "/messages/**"
subscribe :bob, "/messages/bar"
subscribe :carol, "/**"
end
it "delivers messages to matching subscriptions" do
publish @message
expect_message :alice, [@message]
expect_no_message :bob
expect_message :carol, [@message]
end
it "delivers a unique copy of the message to each client" do
publish @message
check_different_messages :alice, :carol
end
end
describe "with multiple matching subscriptions for the same client" do
before do
subscribe :alice, "/messages/foo"
subscribe :alice, "/messages/*"
end
it "delivers each message once to each client" do
publish @message
expect_message :alice, [@message]
end
it "delivers the message as many times as it is published" do
publish [@message, @message]
expect_message :alice, [@message, @message]
end
end
end
end
shared_examples_for "distributed engine" do
include EngineSteps
def create_engine
opts = options.merge(engine_opts)
Faye::Engine::Proxy.new(opts)
end
let(:options) { {} }
let(:left) { create_engine }
let(:right) { create_engine }
alias :engine :left
before do
Faye::Engine.ensure_reactor_running!
create_client :alice
create_client :bob
connect :alice, left
end
describe :publish do
before do
subscribe :alice, "/foo"
publish "channel" => "/foo", "data" => "first"
end
it "only delivers each message once" do
expect_message :alice, ["channel" => "/foo", "data" => "first"]
publish "channel" => "/foo", "data" => "second"
connect :alice, right
expect_message :alice, [{ "channel" => "/foo", "data" => "first" }, { "channel" => "/foo", "data" => "second" }]
end
end
describe :gc do
let(:options) { { :timeout => 0.3, :gc => 0.08 } }
it "calls close in each engine when a client is removed" do
expect_non_exclusive_event :alice, :close, [], left
expect_non_exclusive_event :alice, :close, [], right
clock_tick 0.7
end
end
end
|