Skip to content

Commit

Permalink
tests would fail when run in a certain order because the way Slanger:…
Browse files Browse the repository at this point in the history
…:Redis was set up to listen for messages. if the file was first loaded when EM::Hiredis.connect was stubbed the object would not receive messages for the rest of the test suite causing the integration tests to hang forever. the opposite case was that EM::Hiredis.connext was never mocked if the tests were green in particular order. fixed by explicitly deleting connections from Slanger::Redis before and after they are mocked and making pubsub subscription lazy instead of eager.
  • Loading branch information
stevegraham committed Feb 9, 2013
1 parent 0758ca6 commit 80c1b17
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@ env:
services:
- redis-server

script:
- bundle exec rspec spec --tag ~@defer --order random
- bundle exec rspec spec --tag @defer --order random
17 changes: 8 additions & 9 deletions lib/slanger/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Redis
extend Forwardable

def_delegator :publisher, :publish
def_delegators :subscriber, :on, :subscribe
def_delegators :subscriber, :subscribe
def_delegators :regular_connection, :hgetall, :hdel, :hset, :hincrby

private
Expand All @@ -22,20 +22,19 @@ def publisher
end

def subscriber
@subscriber ||= new_connection
@subscriber ||= new_connection.tap do |c|
c.on(:message) do |channel, message|
message = JSON.parse message
c = Channel.from message['channel']
c.dispatch message, channel
end
end
end

def new_connection
EM::Hiredis.connect Slanger::Config.redis_address
end

extend self

# Dispatch messages received from Redis to their destination channel.
on(:message) do |channel, message|
message = JSON.parse message
c = Channel.from message['channel']
c.dispatch message, channel
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def errback
config.formatter = 'documentation'
config.color = true
config.mock_framework = :mocha
config.order = 'random'
config.include SlangerHelperMethods
config.fail_fast = true
config.after(:each) { stop_slanger if @server_pid }
Expand Down
14 changes: 11 additions & 3 deletions spec/unit/channel_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
require 'spec_helper'
require 'slanger'

describe 'Slanger::Webhook', defer: true do
def clear_redis_connections
Slanger::Redis.instance_variables.each do |ivar|
Slanger::Redis.send :remove_instance_variable, ivar
end
end

describe 'Slanger::Channel' do
let(:channel) { Slanger::Channel.create channel_id: 'test' }

before(:all) do
before(:each) do
EM::Hiredis.stubs(:connect).returns stub_everything('redis')
clear_redis_connections
end

after(:all) do
after(:each) do
clear_redis_connections
EM::Hiredis.unstub(:connect)
end

Expand Down

0 comments on commit 80c1b17

Please sign in to comment.