Skip to content

Commit

Permalink
channel for untagged server response: add optional error data (#28)
Browse files Browse the repository at this point in the history
Github Issue: #28

Add optional error data to inspect the status of the running server.
  • Loading branch information
y10k committed Nov 21, 2019
1 parent 3c6d5d5 commit 771881e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
32 changes: 28 additions & 4 deletions lib/rims/channel.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# -*- coding: utf-8 -*-

module RIMS
class ServerResponseChannelError < Error
end

class ServerResponseChannelAttachError < ServerResponseChannelError
end

class ServerResponseChannelDetachError < ServerResponseChannelError
end

class ServerResponseChannelPublishError < ServerResponseChannelError
end

class ServerResponseChannel
def initialize
@mutex = Thread::Mutex.new
Expand All @@ -16,7 +28,9 @@ def make_pub_sub_pair(mbox_id)
def attach(sub)
@mutex.synchronize{
@channel[sub.mbox_id] ||= {}
(@channel[sub.mbox_id].key? sub.pub_sub_pair_key) and raise ArgumentError, 'conflicted subscriber.'
if (@channel[sub.mbox_id].key? sub.pub_sub_pair_key) then
raise ServerResponseChannelAttachError.new('conflicted subscriber.', channel: self, subscriber: sub)
end
@channel[sub.mbox_id][sub.pub_sub_pair_key] = sub
}

Expand All @@ -30,8 +44,13 @@ def attach(sub)
# - ServerResponseSubscriber#detach
def detach(sub)
@mutex.synchronize{
((@channel.key? sub.mbox_id) && (@channel[sub.mbox_id].key? sub.pub_sub_pair_key)) or raise ArgumentError, 'unregistered pub-sub pair.'
(@channel[sub.mbox_id][sub.pub_sub_pair_key] == sub) or raise 'internal error: mismatched subscriber.'
unless ((@channel.key? sub.mbox_id) && (@channel[sub.mbox_id].key? sub.pub_sub_pair_key)) then
raise ServerResponseChannelDetachError.new('unregistered pub-sub pair.', channel: self, subscriber: sub)
end

unless (@channel[sub.mbox_id][sub.pub_sub_pair_key] == sub) then
raise ServerResponseChannelDetachError.new('internal error: mismatched subscriber.', channel: self, subscribe: sub)
end

@channel[sub.mbox_id].delete(sub.pub_sub_pair_key)
if (@channel[sub.mbox_id].empty?) then
Expand Down Expand Up @@ -74,7 +93,12 @@ def pub_sub_pair_key
end

def publish(response_message)
@channel or raise 'detached publisher.'
unless (@channel) then
raise ServerResponseChannelPublishError.new('detached publisher.',
publisher: self,
pub_sub_pair_key: pub_sub_pair_key,
message: response_message)
end
@channel.publish(@mbox_id, pub_sub_pair_key, response_message)
nil
end
Expand Down
3 changes: 2 additions & 1 deletion test/test_channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def test_pub_sub_detach
pub1.publish('msg1')
pub2.publish('msg2')

error = assert_raise(RuntimeError) { pub3.publish('msg3') }
error = assert_raise(RIMS::ServerResponseChannelPublishError) { pub3.publish('msg3') }
pp error, error.optional_data if $DEBUG
assert_match(/detached/, error.message)

assert_equal(%w[ msg2 ], sub1.enum_for(:fetch).to_a)
Expand Down

0 comments on commit 771881e

Please sign in to comment.