diff --git a/Gemfile b/Gemfile index 9ceeece71..449911a01 100644 --- a/Gemfile +++ b/Gemfile @@ -26,4 +26,5 @@ group :test do gem 'guard', '>= 0.10.0' gem 'guard-rspec', '>= 0.7.3' gem 'pg_migrate','0.1.5' #:path => "#{workspace}/pg_migrate_ruby" + gem 'amqp-spec' end diff --git a/bin/websocket_gateway b/bin/websocket_gateway index b82d6e035..480ca4f1a 100755 --- a/bin/websocket_gateway +++ b/bin/websocket_gateway @@ -1,4 +1,4 @@ -#!/usr/bin/env jruby +#!/usr/bin/env ruby require 'jam_websockets' diff --git a/lib/jam_websockets/router.rb b/lib/jam_websockets/router.rb index 55fcdd099..5b1a6b708 100644 --- a/lib/jam_websockets/router.rb +++ b/lib/jam_websockets/router.rb @@ -38,22 +38,21 @@ module JamWebsockets @thread_pool = nil end - def start(options={:host => "localhost", :port => 5432}) + def start(options={:host => "localhost", :port => 5672}) @log.info "startup" begin - - @connection = AMQP.connect(:host => options[:host]) + @connection = AMQP.connect(:host => options[:host], :port => options[:port]) @channel = AMQP::Channel.new(@connection) #@channel.prefetch = 10 - register_topics rescue => e cleanup raise e end + @log.info "started" end def add_client(client_id, client) diff --git a/lib/jam_websockets/server.rb b/lib/jam_websockets/server.rb index dcc5a37e3..d3b37c8be 100644 --- a/lib/jam_websockets/server.rb +++ b/lib/jam_websockets/server.rb @@ -7,7 +7,7 @@ module JamWebsockets def initialize(options={}) @log = Logging.logger[self] @count=0 - @router = Router.new :transport => self + @router = Router.new end def run(options={}) @@ -17,20 +17,20 @@ module JamWebsockets @log.info "starting server #{host}:#{port}" - @router.start + EventMachine.run do + @router.start - # if you don't do this, the app won't exit unless you kill -9 - at_exit do - @log.info "cleaning up server" - @router.cleanup - end + # if you don't do this, the app won't exit unless you kill -9 + at_exit do + @log.info "cleaning up server" + @router.cleanup + end - EventMachine.run { EventMachine::WebSocket.start(:host => "0.0.0.0", :port => options[:port], :debug => options[:emwebsocket_debug]) do |ws| @log.info "new client #{ws}" @router.new_client(ws) end - } + end end end diff --git a/spec/jam_websockets/router_spec.rb b/spec/jam_websockets/router_spec.rb index 0dc7349fe..84940dd0d 100644 --- a/spec/jam_websockets/router_spec.rb +++ b/spec/jam_websockets/router_spec.rb @@ -74,137 +74,149 @@ def login_music_session(router, client, music_session) end -describe Router do +describe Router do + include AMQP::Spec message_factory = MessageFactory.new - before do - + amqp_before do @router = Router.new() @router.start() - end subject { @router } - after do + amqp_after do @router.stop end describe "servicability" do it "should start and stop", :mq => true do + em do + done + end end it "should register for client events", :mq => true do - client = double("client") - client.should_receive(:onopen) - client.should_receive(:onclose) - client.should_receive(:onerror) - client.should_receive(:onmessage) - client.should_receive(:encode_json=) - - @router.new_client(client) + em do + client = double("client") + client.should_receive(:onopen) + client.should_receive(:onclose) + client.should_receive(:onerror) + client.should_receive(:onmessage) + client.should_receive(:encode_json=) + @router.new_client(client) + done + end end end describe "topic routing helpers" do it "create and delete user lookup set" do - user = double(User) - user.should_receive(:id).any_number_of_times.and_return("1") - client = double("client") - context = ClientContext.new(user, client) + em do + user = double(User) + user.should_receive(:id).any_number_of_times.and_return("1") + client = double("client") + context = ClientContext.new(user, client) - @router.user_context_lookup.length.should == 0 + @router.user_context_lookup.length.should == 0 - @router.add_user(context) + @router.add_user(context) - @router.user_context_lookup.length.should == 1 + @router.user_context_lookup.length.should == 1 - @router.remove_user(context) + @router.remove_user(context) - @router.user_context_lookup.length.should == 0 + @router.user_context_lookup.length.should == 0 + done + end end end describe "login" do it "should not allow login of bogus user", :mq => true do - TestClient = Class.new do + em do + TestClient = Class.new do - attr_accessor :onmsgblock, :onopenblock, :encode_json, :client_id + attr_accessor :onmsgblock, :onopenblock, :encode_json, :client_id - def initialize() + def initialize() + end + + def onopen(&block) + @onopenblock = block + end + + def onmessage(&block) + @onmsgblock = block + end + + def close_websocket() + end + + def close() + end end - def onopen(&block) - @onopenblock = block - end + client = TestClient.new - def onmessage(&block) - @onmsgblock = block - end + error_msg = message_factory.server_rejection_error("invalid login") - def close_websocket() - end + @router.should_receive(:send_to_client).with(client, error_msg) + client.should_receive(:close_websocket) + client.should_receive(:onclose) + client.should_receive(:onerror) + client.should_receive(:request).and_return({"query" => {"pb" => "true"}}) - def close() - end + + @router.new_client(client) + client.onopenblock.call + + # create a login message, and pass it into the router via onmsgblock.call + login = message_factory.login_with_user_pass("baduser@example.com", "foobar") + + client.onmsgblock.call login.to_s + done end - - client = TestClient.new - - error_msg = message_factory.server_rejection_error("invalid login") - - @router.should_receive(:send_to_client).with(client, error_msg) - client.should_receive(:close_websocket) - client.should_receive(:onclose) - client.should_receive(:onerror) - client.should_receive(:request).and_return({"query" => {"pb" => "true"}}) - - - @router.new_client(client) - client.onopenblock.call - - # create a login message, and pass it into the router via onmsgblock.call - login = message_factory.login_with_user_pass("baduser@example.com", "foobar") - - client.onmsgblock.call login.to_s - end it "should allow login of valid user", :mq => true do - @user = User.new(:name => "Example User", :email => "user@example.com", - :password => "foobar", :password_confirmation => "foobar") - @user.save + em do + @user = User.new(:name => "Example User", :email => "user@example.com", + :password => "foobar", :password_confirmation => "foobar") + @user.save - client1 = login(@router, @user, "foobar", "1") + client1 = login(@router, @user, "foobar", "1") + done + end end it "should allow music_session_join of valid user", :mq => true do + em do + user1 = FactoryGirl.create(:user) # in the music session + user2 = FactoryGirl.create(:user) # in the music session + user3 = FactoryGirl.create(:user) # not in the music session - user1 = FactoryGirl.create(:user) # in the music session - user2 = FactoryGirl.create(:user) # in the music session - user3 = FactoryGirl.create(:user) # not in the music session + music_session = FactoryGirl.create(:music_session, :creator => user1) - music_session = FactoryGirl.create(:music_session, :creator => user1) + music_session_member1 = FactoryGirl.create(:music_session_client, :user => user1, :music_session => music_session, :client_id => "1") + music_session_member2 = FactoryGirl.create(:music_session_client, :user => user2, :music_session => music_session, :client_id => "2") - music_session_member1 = FactoryGirl.create(:music_session_client, :user => user1, :music_session => music_session, :client_id => "1") - music_session_member2 = FactoryGirl.create(:music_session_client, :user => user2, :music_session => music_session, :client_id => "2") - - # make a music_session and define two members - - # create client 1, log him in, and log him in to music session - client1 = login(@router, user1, "foobar", "1") - login_music_session(@router, client1, music_session) \ + # make a music_session and define two members + # create client 1, log him in, and log him in to music session + client1 = login(@router, user1, "foobar", "1") + login_music_session(@router, client1, music_session) \ + done + end end it "should allow two valid subscribers to communicate with session-directed messages", :mq => true do - - EventMachine.run do + em do user1 = FactoryGirl.create(:user) # in the music session user2 = FactoryGirl.create(:user) # in the music session @@ -223,14 +235,12 @@ describe Router do music_session_member1 = FactoryGirl.create(:music_session_client, :user => user1, :music_session => music_session, :client_id => "1") music_session_member2 = FactoryGirl.create(:music_session_client, :user => user2, :music_session => music_session, :client_id => "2") - - EM.stop + done end end it "should allow two valid subscribers to communicate with p2p messages", :mq => true do - - EventMachine.run do + em do user1 = FactoryGirl.create(:user) # in the music session user2 = FactoryGirl.create(:user) # in the music session @@ -260,8 +270,7 @@ describe Router do #music_session_member2 = FactoryGirl.create(:music_session_client, :user => user2, :music_session => music_session, :client_id => "2") - - EM.stop + done end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 203534124..0e2fb42f1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,10 +1,9 @@ - require 'active_record' require 'jam_db' require 'spec_db' require 'jam_websockets' require 'timeout' - +require 'amqp-spec/rspec' jamenv = ENV['JAMENV'] jamenv ||= 'development'