/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. require File.expand_path("helper", File.dirname(__FILE__))
  3. class TestDistributedPublishSubscribe < Test::Unit::TestCase
  4. include Helper::Distributed
  5. def test_subscribe_and_unsubscribe
  6. assert_raise Redis::Distributed::CannotDistribute do
  7. r.subscribe("foo", "bar") { }
  8. end
  9. assert_raise Redis::Distributed::CannotDistribute do
  10. r.subscribe("{qux}foo", "bar") { }
  11. end
  12. end
  13. def test_subscribe_and_unsubscribe_with_tags
  14. @subscribed = false
  15. @unsubscribed = false
  16. wire = Wire.new do
  17. r.subscribe("foo") do |on|
  18. on.subscribe do |channel, total|
  19. @subscribed = true
  20. @t1 = total
  21. end
  22. on.message do |channel, message|
  23. if message == "s1"
  24. r.unsubscribe
  25. @message = message
  26. end
  27. end
  28. on.unsubscribe do |channel, total|
  29. @unsubscribed = true
  30. @t2 = total
  31. end
  32. end
  33. end
  34. # Wait until the subscription is active before publishing
  35. Wire.pass while !@subscribed
  36. Redis::Distributed.new(NODES).publish("foo", "s1")
  37. wire.join
  38. assert @subscribed
  39. assert_equal 1, @t1
  40. assert @unsubscribed
  41. assert_equal 0, @t2
  42. assert_equal "s1", @message
  43. end
  44. def test_subscribe_within_subscribe
  45. @channels = []
  46. wire = Wire.new do
  47. r.subscribe("foo") do |on|
  48. on.subscribe do |channel, total|
  49. @channels << channel
  50. r.subscribe("bar") if channel == "foo"
  51. r.unsubscribe if channel == "bar"
  52. end
  53. end
  54. end
  55. wire.join
  56. assert_equal ["foo", "bar"], @channels
  57. end
  58. def test_other_commands_within_a_subscribe
  59. assert_raise Redis::CommandError do
  60. r.subscribe("foo") do |on|
  61. on.subscribe do |channel, total|
  62. r.set("bar", "s2")
  63. end
  64. end
  65. end
  66. end
  67. def test_subscribe_without_a_block
  68. assert_raise LocalJumpError do
  69. r.subscribe("foo")
  70. end
  71. end
  72. end