Sha256: 3ace98bf4c50197e00955a25a4709e5656e8f6d99456f5b24ee448e4dfd7169e

Contents?: true

Size: 1.89 KB

Versions: 11

Compression:

Stored size: 1.89 KB

Contents

# encoding: UTF-8

require "helper"

class TestDistributedPublishSubscribe < Test::Unit::TestCase

  include Helper::Distributed

  def test_subscribe_and_unsubscribe
    assert_raise Redis::Distributed::CannotDistribute do
      r.subscribe("foo", "bar") { }
    end

    assert_raise Redis::Distributed::CannotDistribute do
      r.subscribe("{qux}foo", "bar") { }
    end
  end

  def test_subscribe_and_unsubscribe_with_tags
    @subscribed = false
    @unsubscribed = false

    wire = Wire.new do
      r.subscribe("foo") do |on|
        on.subscribe do |channel, total|
          @subscribed = true
          @t1 = total
        end

        on.message do |channel, message|
          if message == "s1"
            r.unsubscribe
            @message = message
          end
        end

        on.unsubscribe do |channel, total|
          @unsubscribed = true
          @t2 = total
        end
      end
    end

    # Wait until the subscription is active before publishing
    Wire.pass while !@subscribed

    Redis::Distributed.new(NODES).publish("foo", "s1")

    wire.join

    assert @subscribed
    assert_equal 1, @t1
    assert @unsubscribed
    assert_equal 0, @t2
    assert_equal "s1", @message
  end

  def test_subscribe_within_subscribe
    @channels = []

    wire = Wire.new do
      r.subscribe("foo") do |on|
        on.subscribe do |channel, total|
          @channels << channel

          r.subscribe("bar") if channel == "foo"
          r.unsubscribe if channel == "bar"
        end
      end
    end

    wire.join

    assert_equal ["foo", "bar"], @channels
  end

  def test_other_commands_within_a_subscribe
    assert_raise Redis::CommandError do
      r.subscribe("foo") do |on|
        on.subscribe do |channel, total|
          r.set("bar", "s2")
        end
      end
    end
  end

  def test_subscribe_without_a_block
    assert_raise LocalJumpError do
      r.subscribe("foo")
    end
  end
end

Version data entries

11 entries across 11 versions & 3 rubygems

Version Path
redis-3.0.5 test/distributed_publish_subscribe_test.rb
sidekiq-statsd-0.1.1 vendor/ruby/1.9.1/gems/redis-3.0.4/test/distributed_publish_subscribe_test.rb
sidekiq-statsd-0.1.0 vendor/ruby/1.9.1/gems/redis-3.0.4/test/distributed_publish_subscribe_test.rb
redis-3.0.4 test/distributed_publish_subscribe_test.rb
redis-3.0.3 test/distributed_publish_subscribe_test.rb
remq-0.0.4 vendor/bundle/gems/redis-3.0.2/test/distributed_publish_subscribe_test.rb
remq-0.0.3 vendor/bundle/gems/redis-3.0.2/test/distributed_publish_subscribe_test.rb
redis-3.0.2 test/distributed_publish_subscribe_test.rb
redis-3.0.1 test/distributed_publish_subscribe_test.rb
redis-3.0.0 test/distributed_publish_subscribe_test.rb
redis-3.0.0.rc2 test/distributed_publish_subscribe_test.rb