Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change pubsub method to pubsub_client #38

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Basically just bind to `:message` and `:pmessage` events:

# Create two connections, one will be used for subscribing
redis = EM::Hiredis.connect
pubsub = redis.pubsub
pubsub = redis.pubsub_client

pubsub.subscribe('bar.0').callback { puts "Subscribed" }
pubsub.psubscribe('bar.*')
Expand All @@ -99,11 +99,11 @@ If you pass a block to `subscribe` or `psubscribe`, the passed block will be cal
redis = EM::Hiredis.connect

puts "Subscribing"
redis.pubsub.subscribe("foo") { |msg|
redis.pubsub_client.subscribe("foo") { |msg|
p [:sub1, msg]
}

redis.pubsub.psubscribe("f*") { |channel, msg|
redis.pubsub_client.psubscribe("f*") { |channel, msg|
p [:sub2, msg]
}

Expand All @@ -113,7 +113,7 @@ If you pass a block to `subscribe` or `psubscribe`, the passed block will be cal

EM.add_timer(5) {
puts "Unsubscribing sub1"
redis.pubsub.unsubscribe("foo")
redis.pubsub_client.unsubscribe("foo")
}

It's possible to subscribe to the same channel multiple time and just unsubscribe a single callback using `unsubscribe_proc` or `punsubscribe_proc`.
Expand Down
2 changes: 1 addition & 1 deletion lib/em-hiredis/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def info_commandstats(&blk)
# Gives access to a richer interface for pubsub subscriptions on a
# separate redis connection
#
def pubsub
def pubsub_client
@pubsub ||= begin
PubsubClient.new(@host, @port, @password, @db).connect
end
Expand Down
76 changes: 38 additions & 38 deletions spec/pubsub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
describe "subscribing" do
it "should return deferrable which succeeds with subscribe call result" do
connect do |redis|
df = redis.pubsub.subscribe("channel") { }
df = redis.pubsub_client.subscribe("channel") { }
df.should be_kind_of(EventMachine::DefaultDeferrable)
df.callback { |subscription_count|
# Subscribe response from redis - indicates that subscription has
Expand All @@ -18,7 +18,7 @@

it "should run the passed block when message received" do
connect do |redis|
redis.pubsub.subscribe("channel") { |message|
redis.pubsub_client.subscribe("channel") { |message|
message.should == 'hello'
done
}.callback {
Expand All @@ -33,7 +33,7 @@
message.should == 'hello'
done
}
redis.pubsub.subscribe("channel", proc).callback {
redis.pubsub_client.subscribe("channel", proc).callback {
redis.publish('channel', 'hello')
}
end
Expand All @@ -48,9 +48,9 @@
message.should == 'hello'
done
}
redis.pubsub.subscribe("channel", proc1)
redis.pubsub.subscribe("channel", proc2).callback {
redis.pubsub.unsubscribe_proc("channel", proc1)
redis.pubsub_client.subscribe("channel", proc1)
redis.pubsub_client.subscribe("channel", proc2).callback {
redis.pubsub_client.unsubscribe_proc("channel", proc1)
redis.publish("channel", "hello")
}
end
Expand All @@ -59,11 +59,11 @@
it "should unsubscribe from redis on last proc unsubscription" do
connect do |redis|
proc = Proc.new { |message| }
redis.pubsub.subscribe("channel", proc).callback { |subs_count|
redis.pubsub_client.subscribe("channel", proc).callback { |subs_count|
subs_count.should == 1
redis.pubsub.unsubscribe_proc("channel", proc).callback {
redis.pubsub_client.unsubscribe_proc("channel", proc).callback {
# Slightly awkward way to check that unsubscribe happened:
redis.pubsub.subscribe('channel2').callback { |count|
redis.pubsub_client.subscribe('channel2').callback { |count|
# If count is 1 this implies that channel unsubscribed
count.should == 1
done
Expand All @@ -76,14 +76,14 @@
it "should allow unsubscribing from redis channel, including all callbacks, and return deferrable for redis unsubscribe" do
connect do |redis|
# Raw pubsub event
redis.pubsub.on('message') { |channel, message| fail }
redis.pubsub_client.on('message') { |channel, message| fail }
# Block subscription
redis.pubsub.subscribe("channel") { |m| fail } # block
redis.pubsub_client.subscribe("channel") { |m| fail } # block
# Proc example
df = redis.pubsub.subscribe("channel", Proc.new { |m| fail })
df = redis.pubsub_client.subscribe("channel", Proc.new { |m| fail })

df.callback {
redis.pubsub.unsubscribe("channel").callback { |remaining_subs|
redis.pubsub_client.unsubscribe("channel").callback { |remaining_subs|
remaining_subs.should == 0
redis.publish("channel", "hello") {
done
Expand All @@ -98,7 +98,7 @@
channel = "channel"
callback_count = 0
connect do |redis|
redis.pubsub.on(:subscribe) { |channel, subscription_count|
redis.pubsub_client.on(:subscribe) { |channel, subscription_count|
# 2. Get subscribe callback
callback_count += 1
channel.should == channel
Expand All @@ -108,7 +108,7 @@
redis.publish(channel, 'foo')
}

redis.pubsub.on(:message) { |channel, message|
redis.pubsub_client.on(:message) { |channel, message|
# 4. Get message callback
callback_count += 1
channel.should == channel
Expand All @@ -119,18 +119,18 @@
}

# 1. Subscribe to channel
redis.pubsub.subscribe(channel)
redis.pubsub_client.subscribe(channel)
end
end

it "should resubscribe to all channels on reconnect" do
callback_count = 0
connect do |redis|
# 1. Subscribe to channels
redis.pubsub.subscribe('channel1') {
redis.pubsub_client.subscribe('channel1') {
callback_count += 1
}
redis.pubsub.subscribe('channel2') {
redis.pubsub_client.subscribe('channel2') {
callback_count += 1
EM.next_tick {
# 4. Success if both messages have been received
Expand All @@ -140,7 +140,7 @@
}.callback { |subscription_count|
subscription_count.should == 2
# 2. Subscriptions complete. Now force disconnect
redis.pubsub.instance_variable_get(:@connection).close_connection
redis.pubsub_client.instance_variable_get(:@connection).close_connection

EM.add_timer(0.1) {
# 3. After giving time to reconnect publish to both channels
Expand All @@ -158,7 +158,7 @@
describe "psubscribing" do
it "should return deferrable which succeeds with psubscribe call result" do
connect do |redis|
df = redis.pubsub.psubscribe("channel") { }
df = redis.pubsub_client.psubscribe("channel") { }
df.should be_kind_of(EventMachine::DefaultDeferrable)
df.callback { |subscription_count|
# Subscribe response from redis - indicates that subscription has
Expand All @@ -172,7 +172,7 @@

it "should run the passed block when message received" do
connect do |redis|
redis.pubsub.psubscribe("channel:*") { |channel, message|
redis.pubsub_client.psubscribe("channel:*") { |channel, message|
channel.should == 'channel:foo'
message.should == 'hello'
done
Expand All @@ -189,7 +189,7 @@
message.should == 'hello'
done
}
redis.pubsub.psubscribe("channel:*", proc).callback {
redis.pubsub_client.psubscribe("channel:*", proc).callback {
redis.publish('channel:foo', 'hello')
}
end
Expand All @@ -205,9 +205,9 @@
message.should == 'hello'
done
}
redis.pubsub.psubscribe("channel:*", proc1)
redis.pubsub.psubscribe("channel:*", proc2).callback {
redis.pubsub.punsubscribe_proc("channel:*", proc1)
redis.pubsub_client.psubscribe("channel:*", proc1)
redis.pubsub_client.psubscribe("channel:*", proc2).callback {
redis.pubsub_client.punsubscribe_proc("channel:*", proc1)
redis.publish("channel:foo", "hello")
}
end
Expand All @@ -216,11 +216,11 @@
it "should punsubscribe from redis on last proc punsubscription" do
connect do |redis|
proc = Proc.new { |message| }
redis.pubsub.psubscribe("channel:*", proc).callback { |subs_count|
redis.pubsub_client.psubscribe("channel:*", proc).callback { |subs_count|
subs_count.should == 1
redis.pubsub.punsubscribe_proc("channel:*", proc).callback {
redis.pubsub_client.punsubscribe_proc("channel:*", proc).callback {
# Slightly awkward way to check that unsubscribe happened:
redis.pubsub.psubscribe('channel2').callback { |count|
redis.pubsub_client.psubscribe('channel2').callback { |count|
# If count is 1 this implies that channel unsubscribed
count.should == 1
done
Expand All @@ -233,14 +233,14 @@
it "should allow punsubscribing from redis channel, including all callbacks, and return deferrable for redis punsubscribe" do
connect do |redis|
# Raw pubsub event
redis.pubsub.on('pmessage') { |pattern, channel, message| fail }
redis.pubsub_client.on('pmessage') { |pattern, channel, message| fail }
# Block subscription
redis.pubsub.psubscribe("channel") { |c, m| fail } # block
redis.pubsub_client.psubscribe("channel") { |c, m| fail } # block
# Proc example
df = redis.pubsub.psubscribe("channel", Proc.new { |c, m| fail })
df = redis.pubsub_client.psubscribe("channel", Proc.new { |c, m| fail })

df.callback {
redis.pubsub.punsubscribe("channel").callback { |remaining_subs|
redis.pubsub_client.punsubscribe("channel").callback { |remaining_subs|
remaining_subs.should == 0
redis.publish("channel", "hello") {
done
Expand All @@ -254,7 +254,7 @@
it "should expose raw pattern pubsub events from redis" do
callback_count = 0
connect do |redis|
redis.pubsub.on(:psubscribe) { |pattern, subscription_count|
redis.pubsub_client.on(:psubscribe) { |pattern, subscription_count|
# 2. Get subscribe callback
callback_count += 1
pattern.should == "channel:*"
Expand All @@ -264,7 +264,7 @@
redis.publish('channel:foo', 'foo')
}

redis.pubsub.on(:pmessage) { |pattern, channel, message|
redis.pubsub_client.on(:pmessage) { |pattern, channel, message|
# 4. Get message callback
callback_count += 1
pattern.should == 'channel:*'
Expand All @@ -276,20 +276,20 @@
}

# 1. Subscribe to channel
redis.pubsub.psubscribe('channel:*')
redis.pubsub_client.psubscribe('channel:*')
end
end

it "should resubscribe to all pattern subscriptions on reconnect" do
callback_count = 0
connect do |redis|
# 1. Subscribe to channels
redis.pubsub.psubscribe('foo:*') { |channel, message|
redis.pubsub_client.psubscribe('foo:*') { |channel, message|
channel.should == 'foo:a'
message.should == 'hello foo'
callback_count += 1
}
redis.pubsub.psubscribe('bar:*') { |channel, message|
redis.pubsub_client.psubscribe('bar:*') { |channel, message|
channel.should == 'bar:b'
message.should == 'hello bar'
callback_count += 1
Expand All @@ -301,7 +301,7 @@
}.callback { |subscription_count|
subscription_count.should == 2
# 2. Subscriptions complete. Now force disconnect
redis.pubsub.instance_variable_get(:@connection).close_connection
redis.pubsub_client.instance_variable_get(:@connection).close_connection

EM.add_timer(0.1) {
# 3. After giving time to reconnect publish to both channels
Expand Down