Skip to content

Commit

Permalink
fixed spec and added :auto_reconnect option handling
Browse files Browse the repository at this point in the history
  • Loading branch information
colinsurprenant committed Apr 5, 2011
1 parent 932ddf7 commit a499642
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
40 changes: 23 additions & 17 deletions lib/twitter/json_stream.rb
Expand Up @@ -20,21 +20,22 @@ class JSONStream < EventMachine::Connection
RETRIES_MAX = 10

DEFAULT_OPTIONS = {
:method => 'GET',
:path => '/',
:content_type => "application/x-www-form-urlencoded",
:content => '',
:path => '/1/statuses/filter.json',
:host => 'stream.twitter.com',
:port => 80,
:ssl => false,
:user_agent => 'TwitterStream',
:timeout => 0,
:proxy => ENV['HTTP_PROXY'],
:auth => nil,
:oauth => {},
:filters => [],
:params => {},
:method => 'GET',
:path => '/',
:content_type => "application/x-www-form-urlencoded",
:content => '',
:path => '/1/statuses/filter.json',
:host => 'stream.twitter.com',
:port => 80,
:ssl => false,
:user_agent => 'TwitterStream',
:timeout => 0,
:proxy => ENV['HTTP_PROXY'],
:auth => nil,
:oauth => {},
:filters => [],
:params => {},
:auto_reconnect => true,
}

attr_accessor :code
Expand Down Expand Up @@ -88,6 +89,10 @@ def on_reconnect &block
def on_max_reconnects &block
@max_reconnects_callback = block
end

def on_close &block
@close_callback = block
end

def stop
@gracefully_closed = true
Expand All @@ -102,7 +107,8 @@ def immediate_reconnect

def unbind
receive_line(@buffer.flush) unless @buffer.empty?
schedule_reconnect unless @gracefully_closed
schedule_reconnect if @options[:auto_reconnect] && !@gracefully_closed
@close_callback.call if @close_callback
end

def receive_data data
Expand Down Expand Up @@ -154,7 +160,7 @@ def reconnect_timeout
@immediate_reconnect = false
return 0
end

if (@code == 0) # network failure
if @nf_last_reconnect
@nf_last_reconnect += NF_RECONNECT_ADD
Expand Down
25 changes: 21 additions & 4 deletions spec/twitter/json_stream_spec.rb
Expand Up @@ -99,6 +99,12 @@ def receive_data data
end
end

it "should not reconnect on network failure" do
connect_stream(:auto_reconnect => false) do
stream.should_receive(:reconnect).never
end
end

it "should reconnect with 0.25 at base" do
connect_stream do
stream.should_receive(:reconnect_after).with(0.25)
Expand Down Expand Up @@ -145,13 +151,18 @@ def receive_data data
end
end

it "should not reconnect on inactivity" do
connect_stream(:stop_in => 1.5, :auto_reconnect => false) do
stream.should_receive(:reconnect).never
end
end

it_should_behave_like "network failure"
end

context "on server unavailable" do

attr_reader :stream

# This is to make it so the network failure specs which call connect_stream
# can be reused. This way calls to connect_stream won't actually create a
# server to listen in.
Expand All @@ -167,8 +178,8 @@ def connect_stream_without_server(opts={},&block)
context "on application failure" do
attr_reader :stream
before :each do
$data_to_send = 'HTTP/1.1 401 Unauthorized\r\nWWW-Authenticate: Basic realm="Firehose"\r\n\r\n1'
$close_connection = true
$data_to_send = "HTTP/1.1 401 Unauthorized\r\nWWW-Authenticate: Basic realm=\"Firehose\"\r\n\r\n"
$close_connection = false
end

it "should reconnect on application failure 10 at base" do
Expand All @@ -177,6 +188,12 @@ def connect_stream_without_server(opts={},&block)
end
end

it "should not reconnect on application failure 10 at base" do
connect_stream(:auto_reconnect => false) do
stream.should_receive(:reconnect_after).never
end
end

it "should reconnect with exponential timeout" do
connect_stream do
stream.af_last_reconnect = 160
Expand Down

0 comments on commit a499642

Please sign in to comment.