From c3616a0cb3ab4db1d55cb6d5c7f85d9b0b8fb751 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Sun, 7 Oct 2012 14:01:48 -0400 Subject: [PATCH] fixed issues found in unit tests --- lib/jam_websockets/router.rb | 30 +++--- lib/jam_websockets/server.rb | 14 +-- spec/jam_websockets/router_spec.rb | 161 +++++++++++++++-------------- 3 files changed, 106 insertions(+), 99 deletions(-) diff --git a/lib/jam_websockets/router.rb b/lib/jam_websockets/router.rb index 17e02d57f..33ddf6361 100644 --- a/lib/jam_websockets/router.rb +++ b/lib/jam_websockets/router.rb @@ -372,8 +372,8 @@ module JamWebsockets remove_user(context) # remove this connection from the database - connection = JamRuby::Connection.delete_all "user_id = '#{context.user.id}' AND client_id = '#{context.client.client_id}'" - send_friend_update(client, user, false) + #JamRuby::Connection.delete_all "user_id = '#{context.user.id}' AND client_id = '#{context.client.client_id}'" + #send_friend_update(context.user, false) if !context.session.nil? remove_session(context) @@ -471,10 +471,11 @@ module JamWebsockets add_user(context) # log this connection in the database - connection = JamRuby::Connection.new(user.id, client.id) + connection = JamRuby::Connection.new(:user => user, :client_id => client.client_id) + @log.debug "Created connection => #{connection.user}, #{connection.client_id}" - if connection.save? - send_friend_update(client, user, true) + if connection.save + send_friend_update(user, true) end end else @@ -482,16 +483,18 @@ module JamWebsockets end end - def send_friend_update(client, user, online) - unless user.friends.nil? - @log.debug "sending friend update message to friends" + def send_friend_update(user, online) + @log.debug "sending friend update for user #{user} online = #{online}" + + if user.friends.exists? + @log.debug "user has friends - sending friend updates" # create the friend_update message friend_update_msg = @message_factory.friend_update(user.id, online) # send the friend_update to each friend that has active connections user.friends.each do |friend| - @log.debug "sending friend update message to #{context}" + @log.debug "sending friend update message to #{friend}" # put it on the topic exchange for users @users_exchange.publish(friend_update_msg.to_s, :routing_key => "user.#{friend.id}") @@ -503,8 +506,11 @@ module JamWebsockets context = @clients[client] @log.debug "updating timestamp for user #{context}" connection = Connection.find_by_user_id_and_client_id(context.user.user_id, context.client.client_id) - connection.updated_at = DateTime.now - connection.save + + unless connection.nil? + connection.updated_at = DateTime.now + connection.save + end end def handle_join_music_session(join_music_session, client) @@ -539,7 +545,7 @@ module JamWebsockets # send 'new client' message to other members in the session handle_session_directed(session_id, - @message_factory.user_joined_jam_session(context.user.id, context.user.name), + @message_factory.user_joined_music_session(context.user.id, context.user.name), client) end diff --git a/lib/jam_websockets/server.rb b/lib/jam_websockets/server.rb index 3b7f0c9c0..dcc5a37e3 100644 --- a/lib/jam_websockets/server.rb +++ b/lib/jam_websockets/server.rb @@ -17,17 +17,17 @@ module JamWebsockets @log.info "starting server #{host}:#{port}" - @router.start + @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}" + @log.info "new client #{ws}" @router.new_client(ws) end } diff --git a/spec/jam_websockets/router_spec.rb b/spec/jam_websockets/router_spec.rb index 6736ca0e7..af05ad0b2 100644 --- a/spec/jam_websockets/router_spec.rb +++ b/spec/jam_websockets/router_spec.rb @@ -2,35 +2,35 @@ require 'spec_helper' require 'thread' LoginClient = Class.new do - attr_accessor :onmsgblock, :onopenblock, :encode_json, :client_id + attr_accessor :onmsgblock, :onopenblock, :encode_json, :client_id - def initiaize() + def initialize() - end + end - def onopen(&block) - @onopenblock = block - end + def onopen(&block) + @onopenblock = block + end - def onmessage(&block) - @onmsgblock = block - end + def onmessage(&block) + @onmsgblock = block + end - def close(&block) - @oncloseblock = block - end + def close(&block) + @oncloseblock = block + end - def close_websocket() - - end + def close_websocket() + + end - def send(msg) - puts msg - end + def send(msg) + puts msg + end - def get_peername - return "\x00\x02\x93\v\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00" # 37643, "localhost" - end + def get_peername + return "\x00\x02\x93\v\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00" # 37643, "localhost" + end end @@ -38,42 +38,43 @@ end # does a login and returns client def login(router, user, password) - message_factory = MessageFactory.new - client = LoginClient.new + message_factory = MessageFactory.new + client = LoginClient.new - login_ack = message_factory.login_ack("127.0.0.1") + login_ack = message_factory.login_ack("127.0.0.1") - router.should_receive(:send_to_client).with(client, login_ack) - client.should_receive(:onclose) - client.should_receive(:onerror) + router.should_receive(:send_to_client).with(client, login_ack) + client.should_receive(:onclose) + client.should_receive(:onerror) client.should_receive(:request).and_return({ "query" => { "pb" => "true" } }) - client.should_receive(:get_peername).and_return("\x00\x02\x93\v\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00") + client.should_receive(:get_peername).and_return("\x00\x02\x93\v\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00") - @router.new_client(client) - client.onopenblock.call + @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(user.email, password) + # create a login message, and pass it into the router via onmsgblock.call + login = message_factory.login_with_user_pass(user.email, password) - # first log in - client.onmsgblock.call login.to_s + # first log in + client.onmsgblock.call login.to_s - # then join music session - return client + # then join music session + return client end + def login_music_session(router, client, music_session) - message_factory = MessageFactory.new - login_music_session = message_factory.login_music_session(music_session.id) - login_ack = message_factory.login_music_session_ack(false, nil); - router.should_receive(:send_to_client).with(client, login_ack) - client.onmsgblock.call login_music_session.to_s + message_factory = MessageFactory.new + login_music_session = message_factory.login_music_session(music_session.id) + login_ack = message_factory.login_music_session_ack(false, nil); + router.should_receive(:send_to_client).with(client, login_ack) + client.onmsgblock.call login_music_session.to_s end describe Router do - message_factory = MessageFactory.new + message_factory = MessageFactory.new before do @@ -107,24 +108,24 @@ describe Router do 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) + 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) - @router.user_context_lookup.length.should == 0 + @router.user_context_lookup.length.should == 0 - @router.add_user(context) - - @router.user_context_lookup.length.should == 1 + @router.add_user(context) + + @router.user_context_lookup.length.should == 1 - @router.remove_user(context) + @router.remove_user(context) - @router.user_context_lookup.length.should == 0 - end - end + @router.user_context_lookup.length.should == 0 + end + end describe "login" do @@ -133,7 +134,7 @@ describe Router do attr_accessor :onmsgblock, :onopenblock, :encode_json, :client_id - def initiaize() + def initialize() end @@ -145,8 +146,8 @@ describe Router do @onmsgblock = block end - def close_websocket() - end + def close_websocket() + end def close() end @@ -178,9 +179,10 @@ describe Router do :password => "foobar", :password_confirmation => "foobar") @user.save - client1 = login(@router, @user, "foobar") - end + puts "USER ID = #{@user.id}" + client1 = login(@router, @user, "foobar") + end it "should allow music_session_join of valid user", :mq => true do @@ -195,35 +197,34 @@ describe Router do # 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") - login_music_session(@router, client1, music_session) + # create client 1, log him in, and log him in to music session + client1 = login(@router, user1, "foobar") + login_music_session(@router, client1, music_session) end - it "should allow two valid subscribers to communicate with session-directed messages", :mq => true do + it "should allow two valid subscribers to communicate with session-directed messages", :mq => true do - EventMachine.run do - user1 = FactoryGirl.create(:user) # in the music session - user2 = FactoryGirl.create(:user) # in the music session + EventMachine.run do + user1 = FactoryGirl.create(:user) # in the music session + user2 = FactoryGirl.create(:user) # 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) - music_session_member2 = FactoryGirl.create(:music_session_client, :user => user2, :music_session => music_session) + music_session_member1 = FactoryGirl.create(:music_session_client, :user => user1, :music_session => music_session) + music_session_member2 = FactoryGirl.create(:music_session_client, :user => user2, :music_session => music_session) - # make a music_session and define two members + # 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") - login_music_session(@router, client1, music_session) + # create client 1, log him in, and log him in to music session + client1 = login(@router, user1, "foobar") + login_music_session(@router, client1, music_session) - client2 = login(@router, user2, "foobar") - login_music_session(@router, client2, music_session) - EM.stop - end + client2 = login(@router, user2, "foobar") + login_music_session(@router, client2, music_session) + EM.stop + end end - end end