require 'test_helper' class BackrubRedisTest < MiniTest::Unit::TestCase def setup @backlog = 20 @config = {} @store = Backrub::Store::Redis.new(@config, @backlog) @redis = @store.redis end def test_backlog backlog = 20.times.map {|n| "a" * (n+1)} channel = "test" count = 5 @redis.expects(:lrange).with(channel, 0, count - 1).returns(backlog[0...count]) received_backlog = [] @store.backlog(channel, count) do |channel, message| received_backlog << message end assert_equal backlog[0...count].reverse, received_backlog end def test_publishing channel = "test" message = "data" @redis.expects(:publish).with(channel, message) @redis.expects(:lpush).with(channel, message) @redis.expects(:ltrim).with(channel, 0, @backlog - 1) @store.publish(channel, message) end def test_subscribing test_messages = [["test", "data"], ["test2", "data2"]] mock_redis = mock() mock_message_handler = mock() mock_message_handler.expects(:message).multiple_yields(*test_messages) mock_redis.expects(:subscribe).with("test", "test2").yields(mock_message_handler) mock_redis.expects(:quit) ::Redis.expects(:new).returns(mock_redis) @store.subscribe("test", "test2") do |channel, message| assert_equal test_messages.shift, [channel, message] end end end