From 11e403deb32aed32840294ab73acb8440adb4007 Mon Sep 17 00:00:00 2001 From: Seth Call Date: Tue, 21 Aug 2012 22:08:01 -0500 Subject: [PATCH] * no longer using 'pg' gem for ActiveRecord, because it's not tested for jruby; also making all tests pass --- .rvmrc | 2 +- Gemfile | 6 ++++-- config/database.yml | 6 ++++-- lib/jam_websockets/client_context.rb | 6 +++--- lib/jam_websockets/router.rb | 12 +++++------- spec/factories.rb | 2 +- spec/jam_websockets/router_spec.rb | 13 +++++++++---- spec/spec_db.rb | 17 ++++++++++++++++- spec/spec_helper.rb | 26 ++++++++++++++------------ 9 files changed, 57 insertions(+), 33 deletions(-) diff --git a/.rvmrc b/.rvmrc index ad94dd625..a5e31abc5 100644 --- a/.rvmrc +++ b/.rvmrc @@ -1,2 +1,2 @@ -rvm use jruby-1.7.0.preview1@websockets --create +rvm use jruby-head@websockets --create #rvm use ruby-1.9.3@websockets --create diff --git a/Gemfile b/Gemfile index 598a7113f..ac3361db9 100644 --- a/Gemfile +++ b/Gemfile @@ -11,17 +11,19 @@ gem 'pg_migrate', '0.1.4' gem 'jam_db', :path => '~/workspace/jam-db/target/ruby_package' gem 'jam_ruby', :path => '~/workspace/jam-ruby' gem 'jampb', :path => '~/workspace/jam-pb/target/ruby/jampb' -gem 'em-websocket', :path=> '~/workspace/em-websocket' +gem 'em-websocket' # :path=> '~/workspace/em-websocket' gem 'hot_bunnies', '1.3.8' gem 'activerecord', '3.2.7' gem 'logging' #gem 'em-http-request' +gem 'activerecord-jdbc-adapter' +gem 'activerecord-jdbcpostgresql-adapter' group :test do gem 'cucumber' gem 'rspec' gem 'factory_girl' - gem 'spork', '0.9.0' + #gem 'spork', '0.9.0' gem 'database_cleaner', '0.7.0' gem 'guard', '>= 0.10.0' gem 'guard-rspec', '>= 0.7.3' diff --git a/config/database.yml b/config/database.yml index 303d4b907..5a6edeacd 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,8 +1,10 @@ test: - adapter: postgresql + adapter: jdbcpostgresql database: jam_websockets_test + host: localhost + port: 5432 pool: 3 username: postgres password: postgres timeout: 2000 - encoding: unicode \ No newline at end of file + encoding: unicode diff --git a/lib/jam_websockets/client_context.rb b/lib/jam_websockets/client_context.rb index 43eac7c2c..ddcf6e5cf 100644 --- a/lib/jam_websockets/client_context.rb +++ b/lib/jam_websockets/client_context.rb @@ -1,10 +1,10 @@ module JamWebsockets class ClientContext - attr_accessor :user_id, :user_queue, :session_topic, :subscription + attr_accessor :user, :user_queue, :session_topic, :subscription - def initialize(user_id, user_queue, subscription) - @user_id = user_id + def initialize(user, user_queue, subscription) + @user = user @user_queue = user_queue @subscription = subscription @session_topic = nil diff --git a/lib/jam_websockets/router.rb b/lib/jam_websockets/router.rb index 574e0241e..f88686da1 100644 --- a/lib/jam_websockets/router.rb +++ b/lib/jam_websockets/router.rb @@ -98,7 +98,7 @@ module JamWebsockets begin self.route(pb_msg, client) rescue => e - @log.debug "ending client session due to error: #{e.to_s}" + @log.debug "ending client session due to error: #{e.to_s} #{e.backtrace}" begin # wrap the message up and send it down @@ -162,7 +162,7 @@ module JamWebsockets elsif @message_factory.session_directed? client_msg - session = client_msg.target[MessageFactory::SESSION_PREFIX_TARGET.length..-1] + session = client_msg.target[MessageFactory::SESSION_TARGET_PREFIX.length..-1] handle_session_directed(session, client_msg, client) elsif @message_factory.user_directed? client_msg @@ -268,7 +268,7 @@ module JamWebsockets context.session_topic = subscription # respond with LOGIN_JAM_SESSION_ACK to let client know it was successful - client.send(@message_factory.login_jam_session_ack(false, nil) + client.send(@message_factory.login_jam_session_ack(false, nil)) # send 'new client' message @@ -276,7 +276,7 @@ module JamWebsockets end - def handle_leave_jam_session(leae_jam_session, client) + def handle_leave_jam_session(leave_jam_session, client) context = @clients[client] @@ -324,7 +324,7 @@ module JamWebsockets raise 'not allowed to join the specified session' end - return jam_sesson + return jam_session end def handle_session_directed(session, client_msg, client) @@ -346,5 +346,3 @@ module JamWebsockets end end -end - diff --git a/spec/factories.rb b/spec/factories.rb index 856d9e1bf..37aad9841 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -17,4 +17,4 @@ FactoryGirl.define do factory :jam_session_member, :class => JamRuby::JamSessionMember do end -end \ No newline at end of file +end diff --git a/spec/jam_websockets/router_spec.rb b/spec/jam_websockets/router_spec.rb index 45e584d13..2d6733a80 100644 --- a/spec/jam_websockets/router_spec.rb +++ b/spec/jam_websockets/router_spec.rb @@ -86,7 +86,7 @@ describe Router do TestClient = Class.new do - attr_accessor :onmsgblock, :onopenblock + attr_accessor :onmsgblock, :onopenblock, :oncloseblock def initiaize() @@ -100,6 +100,9 @@ describe Router do @onmsgblock = block end + def close(&block) + @oncloseblock = block + end end client = TestClient.new @@ -153,6 +156,10 @@ describe Router do def onmessage(&block) @onmsgblock = block end + + def close(&block) + @oncloseblock = block + end end client = TestClient.new @@ -169,7 +176,7 @@ describe Router do 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") + login = message_factory.login_with_user_pass(user1.email, "foobar") # first log in client.onmsgblock.call login.to_s @@ -180,7 +187,5 @@ describe Router do end end - - end diff --git a/spec/spec_db.rb b/spec/spec_db.rb index dabd970cb..7ac708075 100644 --- a/spec/spec_db.rb +++ b/spec/spec_db.rb @@ -2,7 +2,22 @@ class SpecDb TEST_DB_NAME="jam_websockets_test" - def self.recreate_database + def self.recreate_database(db_config) + recreate_database_jdbc(db_config) + end + + def self.recreate_database_jdbc(db_config) + original = db_config["database"] + db_config["database"] = "postgres" + ActiveRecord::Base.establish_connection(db_config) + ActiveRecord::Base.connection.execute("DROP DATABASE IF EXISTS #{TEST_DB_NAME}") + ActiveRecord::Base.connection.execute("CREATE DATABASE #{TEST_DB_NAME}") + JamDb::Migrator.new.migrate(:dbname => TEST_DB_NAME) + db_config["database"] = original + end + + def self.recreate_database_pg + conn = PG::Connection.open("dbname=postgres") conn.exec("DROP DATABASE IF EXISTS #{TEST_DB_NAME}") conn.exec("CREATE DATABASE #{TEST_DB_NAME}") diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d0d3d0f4d..d1c8af6a1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -21,15 +21,16 @@ end Logging.logger.root.appenders = Logging.appenders.stdout # recreate test database and migrate it -SpecDb::recreate_database -# initialize ActiveRecord's db connection -ActiveRecord::Base.establish_connection(YAML::load(File.open('config/database.yml'))["test"]) +db_config = YAML::load(File.open('config/database.yml'))["test"] +SpecDb::recreate_database(db_config) +# initialize ActiveRecord's db connection +ActiveRecord::Base.establish_connection(db_config) require 'jam_ruby' require 'jampb' require 'rubygems' -require 'spork' +#require 'spork' require 'database_cleaner' require 'factory_girl' require 'factories' @@ -43,7 +44,7 @@ include Jampb #require 'spork/ext/ruby-debug' -Spork.prefork do +#Spork.prefork do # Loading more in this block will cause your tests to run faster. However, # if you change any configuration or code from libraries loaded here, you'll # need to restart spork for it take effect. @@ -64,16 +65,17 @@ Spork.prefork do config.filter_run :focus config.before(:suite) do - DatabaseCleaner.strategy = :transaction - DatabaseCleaner.clean_with(:truncation) + # DatabaseCleaner.strategy = :transaction + # DatabaseCleaner.clean_with(:truncation) end config.before(:each) do - DatabaseCleaner.start + # DatabaseCleaner.start end config.after(:each) do - DatabaseCleaner.clean + ActiveRecord::Base.connection.execute('select truncate_tables()') + # DatabaseCleaner.clean end # If you're not using ActiveRecord, or you'd prefer not to run each of your @@ -87,10 +89,10 @@ Spork.prefork do # --seed 1234 config.order = 'random' end -end +#end -Spork.each_run do +#Spork.each_run do # This code will be run each time you run your specs. -end +#end