PageRenderTime 53ms CodeModel.GetById 31ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/bundle/jruby/2.1/gems/redis-3.0.7/test/distributed_publish_subscribe_test.rb

https://github.com/delowong/logstash
Ruby | 92 lines | 70 code | 20 blank | 2 comment | 7 complexity | 9ca6a6b7492c43419272479cd165f711 MD5 | raw file
 1# encoding: UTF-8
 2
 3require File.expand_path("helper", File.dirname(__FILE__))
 4
 5class TestDistributedPublishSubscribe < Test::Unit::TestCase
 6
 7  include Helper::Distributed
 8
 9  def test_subscribe_and_unsubscribe
10    assert_raise Redis::Distributed::CannotDistribute do
11      r.subscribe("foo", "bar") { }
12    end
13
14    assert_raise Redis::Distributed::CannotDistribute do
15      r.subscribe("{qux}foo", "bar") { }
16    end
17  end
18
19  def test_subscribe_and_unsubscribe_with_tags
20    @subscribed = false
21    @unsubscribed = false
22
23    wire = Wire.new do
24      r.subscribe("foo") do |on|
25        on.subscribe do |channel, total|
26          @subscribed = true
27          @t1 = total
28        end
29
30        on.message do |channel, message|
31          if message == "s1"
32            r.unsubscribe
33            @message = message
34          end
35        end
36
37        on.unsubscribe do |channel, total|
38          @unsubscribed = true
39          @t2 = total
40        end
41      end
42    end
43
44    # Wait until the subscription is active before publishing
45    Wire.pass while !@subscribed
46
47    Redis::Distributed.new(NODES).publish("foo", "s1")
48
49    wire.join
50
51    assert @subscribed
52    assert_equal 1, @t1
53    assert @unsubscribed
54    assert_equal 0, @t2
55    assert_equal "s1", @message
56  end
57
58  def test_subscribe_within_subscribe
59    @channels = []
60
61    wire = Wire.new do
62      r.subscribe("foo") do |on|
63        on.subscribe do |channel, total|
64          @channels << channel
65
66          r.subscribe("bar") if channel == "foo"
67          r.unsubscribe if channel == "bar"
68        end
69      end
70    end
71
72    wire.join
73
74    assert_equal ["foo", "bar"], @channels
75  end
76
77  def test_other_commands_within_a_subscribe
78    assert_raise Redis::CommandError do
79      r.subscribe("foo") do |on|
80        on.subscribe do |channel, total|
81          r.set("bar", "s2")
82        end
83      end
84    end
85  end
86
87  def test_subscribe_without_a_block
88    assert_raise LocalJumpError do
89      r.subscribe("foo")
90    end
91  end
92end