i really have no clue why this works nwow

This commit is contained in:
maxwell 2010-07-07 16:41:42 -07:00
parent 2db3366ef4
commit bf0afeede0
3 changed files with 38 additions and 31 deletions

View file

@ -32,6 +32,7 @@ group :test do
gem 'autotest' gem 'autotest'
gem 'factory_girl_rails' gem 'factory_girl_rails'
gem 'database_cleaner' gem 'database_cleaner'
gem 'em-spec', :git => 'http://github.com/danielsdeleo/em-spec.git'
end end
group :development do group :development do

View file

@ -1,5 +1,6 @@
require File.dirname(__FILE__) + '/../spec_helper' require File.dirname(__FILE__) + '/../spec_helper'
EventMachine.run{
#require 'em-spec/rspec'
describe SocketController do describe SocketController do
before do before do
Factory.create(:user) Factory.create(:user)
@ -11,22 +12,18 @@ describe SocketController do
end end
it 'should unstub the websocket' do it 'should unstub the websocket' do
EventMachine.next_tick {
WebSocket.initialize_channel WebSocket.initialize_channel
WebSocket.push_to_clients("what").should_not == "stub" WebSocket.push_to_clients("what").should_not == "stub"
WebSocket.unsubscribe(1).should_not == "stub" WebSocket.unsubscribe(1).should_not == "stub"
WebSocket.subscribe.should_not == "stub" WebSocket.subscribe.should_not == "stub"
}
end end
it 'should add a new subscriber to the websocket channel' do it 'should add a new subscriber to the websocket channel' do
EventMachine.next_tick {
puts "balls"
WebSocket.initialize_channel WebSocket.initialize_channel
puts "foobar"
@controller.new_subscriber.should == 1 @controller.new_subscriber.should == 1
}
end end
end end
EventMachine.stop
}

View file

@ -1,6 +1,5 @@
require File.dirname(__FILE__) + '/../spec_helper' require File.dirname(__FILE__) + '/../spec_helper'
EventMachine.run{
describe MessageHandler do describe MessageHandler do
before do before do
@handler = MessageHandler.new @handler = MessageHandler.new
@ -10,11 +9,11 @@ describe MessageHandler do
describe 'GET messages' do describe 'GET messages' do
describe 'creating a GET query' do describe 'creating a GET query' do
it 'should be able to add a GET query to the queue with required destinations' do it 'should be able to add a GET query to the queue with required destinations' do
EM.next_tick{ EventMachine.run{
@handler.add_get_request(@message_urls) @handler.add_get_request(@message_urls)
@handler.size.should == @message_urls.size @handler.size.should == @message_urls.size
EventMachine.stop
} }
end end
@ -22,29 +21,32 @@ describe MessageHandler do
describe 'processing a GET query' do describe 'processing a GET query' do
it 'should remove sucessful http requests from the queue' do it 'should remove sucessful http requests from the queue' do
EM.next_tick{
request = FakeHttpRequest.new(:success) request = FakeHttpRequest.new(:success)
request.should_receive(:get).and_return(request) request.should_receive(:get).and_return(request)
EventMachine::HttpRequest.stub!(:new).and_return(request) EventMachine::HttpRequest.stub!(:new).and_return(request)
EventMachine.run {
@handler.add_get_request("http://www.google.com/") @handler.add_get_request("http://www.google.com/")
@handler.size.should == 1 @handler.size.should == 1
@handler.process @handler.process
@handler.size.should == 0 @handler.size.should == 0
EventMachine.stop
} }
end end
it 'should only retry a bad request three times ' do it 'should only retry a bad request three times ' do
EM.next_tick{
request = FakeHttpRequest.new(:failure) request = FakeHttpRequest.new(:failure)
request.should_receive(:get).exactly(MessageHandler::NUM_TRIES).times.and_return(request) request.should_receive(:get).exactly(MessageHandler::NUM_TRIES).times.and_return(request)
EventMachine::HttpRequest.stub!(:new).and_return(request) EventMachine::HttpRequest.stub!(:new).and_return(request)
@handler.add_get_request("http://www.google.com/") EventMachine.run {
@handler.add_get_request("http://asdfsdajfsdfbasdj.com/") @handler.add_get_request("http://asdfsdajfsdfbasdj.com/")
@handler.size.should == 1 @handler.size.should == 1
@handler.process @handler.process
@handler.size.should == 0 @handler.size.should == 0
EventMachine.stop
} }
end end
end end
@ -54,31 +56,37 @@ describe MessageHandler do
it 'should be able to add a post message to the queue' do it 'should be able to add a post message to the queue' do
EM.next_tick{ EventMachine.run {
@handler.size.should ==0 @handler.size.should ==0
@handler.add_post_request(@message_urls.first, @message_body) @handler.add_post_request(@message_urls.first, @message_body)
@handler.size.should == 1 @handler.size.should == 1
EventMachine.stop
} }
end end
it 'should be able to insert many posts into the queue' do it 'should be able to insert many posts into the queue' do
EM.next_tick{ EventMachine.run {
@handler.size.should == 0 @handler.size.should == 0
@handler.add_post_request(@message_urls, @message_body) @handler.add_post_request(@message_urls, @message_body)
@handler.size.should == @message_urls.size @handler.size.should == @message_urls.size
EventMachine.stop
} }
end end
it 'should post a single message to a given URL' do it 'should post a single message to a given URL' do
EM.next_tick{
request = FakeHttpRequest.new(:success) request = FakeHttpRequest.new(:success)
request.should_receive(:post).and_return(request) request.should_receive(:post).and_return(request)
EventMachine::HttpRequest.stub!(:new).and_return(request) EventMachine::HttpRequest.stub!(:new).and_return(request)
EventMachine.run{
@handler.add_post_request(@message_urls.first, @message_body) @handler.add_post_request(@message_urls.first, @message_body)
@handler.size.should == 1 @handler.size.should == 1
@handler.process @handler.process
@handler.size.should == 0 @handler.size.should == 0
EventMachine.stop
} }
end end
end end
@ -86,35 +94,38 @@ describe MessageHandler do
describe "Mixed Queries" do describe "Mixed Queries" do
it 'should process both POST and GET requests in the same queue' do it 'should process both POST and GET requests in the same queue' do
EM.next_tick{
request = FakeHttpRequest.new(:success) request = FakeHttpRequest.new(:success)
request.should_receive(:get).exactly(3).times.and_return(request) request.should_receive(:get).exactly(3).times.and_return(request)
request.should_receive(:post).exactly(3).times.and_return(request) request.should_receive(:post).exactly(3).times.and_return(request)
EventMachine::HttpRequest.stub!(:new).and_return(request) EventMachine::HttpRequest.stub!(:new).and_return(request)
EventMachine.run{
@handler.add_post_request(@message_urls,@message_body) @handler.add_post_request(@message_urls,@message_body)
@handler.size.should == 3 @handler.size.should == 3
@handler.add_get_request(@message_urls) @handler.add_get_request(@message_urls)
@handler.size.should == 6 @handler.size.should == 6
@handler.process @handler.process
#timer = EventMachine::Timer.new(1) do timer = EventMachine::Timer.new(1) do
@handler.size.should == 0 @handler.size.should == 0
#end EventMachine.stop
end
} }
end end
it 'should be able to have seperate POST and GET have different callbacks' do it 'should be able to have seperate POST and GET have different callbacks' do
EM.next_tick{
request = FakeHttpRequest.new(:success) request = FakeHttpRequest.new(:success)
request.should_receive(:get).exactly(1).times.and_return(request) request.should_receive(:get).exactly(1).times.and_return(request)
request.should_receive(:post).exactly(1).times.and_return(request) request.should_receive(:post).exactly(1).times.and_return(request)
@handler.should_receive(:send_to_seed).once @handler.should_receive(:send_to_seed).once
EventMachine::HttpRequest.stub!(:new).and_return(request) EventMachine::HttpRequest.stub!(:new).and_return(request)
EventMachine.run{
@handler.add_post_request(@message_urls.first,@message_body) @handler.add_post_request(@message_urls.first,@message_body)
@handler.add_get_request(@message_urls.first) @handler.add_get_request(@message_urls.first)
@handler.process @handler.process
EventMachine.stop
} }
end end
@ -125,7 +136,6 @@ class FakeHttpRequest
def initialize(callback_wanted) def initialize(callback_wanted)
@callback = callback_wanted @callback = callback_wanted
end end
def response def response
"NOTE YOU ARE IN FAKE HTTP" "NOTE YOU ARE IN FAKE HTTP"
end end
@ -139,5 +149,4 @@ class FakeHttpRequest
b.call if @callback == :failure b.call if @callback == :failure
end end
end end
EventMachine.stop
}