Permalink
Browse files

further refactoring and moving helper methods into own module

  • Loading branch information...
markburns authored and stevegraham committed Apr 7, 2012
1 parent d0623f2 commit 2bff801a037a71bf057bfa36a13b88456bc5ba94
Showing with 66 additions and 62 deletions.
  1. +1 −60 spec/integration/integration_spec.rb
  2. +65 −2 spec/spec_helper.rb
@@ -6,18 +6,11 @@
require 'em-http-request'
require 'pusher'
require 'thin'
require './spec/spec_helper'
describe 'Integration' do
let(:errback) { Proc.new { fail 'cannot connect to slanger. your box might be too slow. try increasing sleep value in the before block' } }
def new_websocket
uri = "ws://0.0.0.0:8080/app/#{Pusher.key}?client=js&version=1.8.5"
EM::HttpRequest.new(uri).get(:timeout => 0).tap do |ws|
ws.errback &errback
end
end
before(:each) do
# Fork service. Our integration tests MUST block the main thread because we want to wait for i/o to finish.
@server_pid = EM.fork_reactor do
@@ -52,58 +45,6 @@ def new_websocket
end
end
def em_stream
messages = []
em_thread do
websocket = new_websocket
stream(websocket, messages) do |message|
yield websocket, messages
end
end
return messages
end
def em_thread
Thread.new do
EM.run do
yield
end
end.join
end
def stream websocket, messages
websocket.stream do |message|
messages << JSON.parse(message)
yield message
end
end
def auth_from options
id = options[:message]['data']['socket_id']
name = options[:name]
user_id = options[:user_id]
Pusher['presence-channel'].authenticate(id, {user_id: user_id, user_info: {name: name}})
end
def send_subscribe options
auth = auth_from options
options[:user].send({event: 'pusher:subscribe',
data: {channel: 'presence-channel'}.merge(auth)}.to_json)
end
def matcher messages, options
messages.first['event'].should == 'pusher:connection_established' if options[:connection_established]
messages.first['data']['socket_id'].should_not be_nil if options[:id_present]
messages.first['event'].should == options[:first_event] if options[:first_event]
messages.last['event'].should == options[:last_event] if options[:last_event]
messages.last['data'].should == options[:last_data] if options[:last_data]
messages.length.should == options[:count] if options[:count]
end
describe 'regular channels:' do
it 'pushes messages to interested websocket connections' do
View
@@ -1,7 +1,70 @@
require 'mocha'
require 'pusher'
require 'eventmachine'
module EventMachineHelperMethods
def new_websocket
uri = "ws://0.0.0.0:8080/app/#{Pusher.key}?client=js&version=1.8.5"
EM::HttpRequest.new(uri).get(:timeout => 0).tap do |ws|
ws.errback &errback
end
end
def em_stream
messages = []
em_thread do
websocket = new_websocket
stream(websocket, messages) do |message|
yield websocket, messages
end
end
return messages
end
def em_thread
Thread.new do
EM.run do
yield
end
end.join
end
def stream websocket, messages
websocket.stream do |message|
messages << JSON.parse(message)
yield message
end
end
def auth_from options
id = options[:message]['data']['socket_id']
name = options[:name]
user_id = options[:user_id]
Pusher['presence-channel'].authenticate(id, {user_id: user_id, user_info: {name: name}})
end
def send_subscribe options
auth = auth_from options
options[:user].send({event: 'pusher:subscribe',
data: {channel: 'presence-channel'}.merge(auth)}.to_json)
end
def matcher messages, options
messages.first['event'].should == 'pusher:connection_established' if options[:connection_established]
messages.first['data']['socket_id'].should_not be_nil if options[:id_present]
messages.first['event'].should == options[:first_event] if options[:first_event]
messages.last['event'].should == options[:last_event] if options[:last_event]
messages.last['data'].should == options[:last_data] if options[:last_data]
messages.length.should == options[:count] if options[:count]
end
end
RSpec.configure do |config|
config.mock_with :mocha
config.include EventMachineHelperMethods
end

0 comments on commit 2bff801

Please sign in to comment.