Skip to content

Commit

Permalink
check that the passed connection handler is of the proper class (subc…
Browse files Browse the repository at this point in the history
…lass of EM::Connection), and added test for this.

Signed-off-by: Aman Gupta <[email protected]>
  • Loading branch information
jakedouglas authored and tmm1 committed Mar 5, 2009
1 parent 1197a08 commit 9b4ef82
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/eventmachine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ def EventMachine::start_server server, port=nil, handler=nil, *args, &block
end if port

klass = if (handler and handler.is_a?(Class))
raise ArgumentError, 'must provide module or subclass of EventMachine::Connection' unless Connection > handler
handler
else
Class.new( Connection ) {handler and include handler}
Expand Down Expand Up @@ -685,6 +686,7 @@ def EventMachine::connect server, port=nil, handler=nil, *args
end if port

klass = if (handler and handler.is_a?(Class))
raise ArgumentError, 'must provide module or subclass of EventMachine::Connection' unless Connection > handler
handler
else
Class.new( Connection ) {handler and include handler}
Expand Down Expand Up @@ -752,6 +754,7 @@ def EventMachine::connect server, port=nil, handler=nil, *args
# Thanks to Riham Aldakkak (eSpace Technologies) for the initial patch
def EventMachine::attach io, handler=nil, *args
klass = if (handler and handler.is_a?(Class))
raise ArgumentError, 'must provide module or subclass of EventMachine::Connection' unless Connection > handler
handler
else
Class.new( Connection ) {handler and include handler}
Expand Down Expand Up @@ -883,6 +886,7 @@ def EventMachine::connect_unix_domain socketname, *args, &blk
#
def self::open_datagram_socket address, port, handler=nil, *args
klass = if (handler and handler.is_a?(Class))
raise ArgumentError, 'must provide module or subclass of EventMachine::Connection' unless Connection > handler
handler
else
Class.new( Connection ) {handler and include handler}
Expand Down Expand Up @@ -1138,6 +1142,7 @@ def self::set_descriptor_table_size n_descriptors=nil
#
def self::popen cmd, handler=nil, *args
klass = if (handler and handler.is_a?(Class))
raise ArgumentError, 'must provide module or subclass of EventMachine::Connection' unless Connection > handler
handler
else
Class.new( Connection ) {handler and include handler}
Expand Down Expand Up @@ -1173,6 +1178,7 @@ def self::reactor_running?
#
def EventMachine::open_keyboard handler=nil, *args
klass = if (handler and handler.is_a?(Class))
raise ArgumentError, 'must provide module or subclass of EventMachine::Connection' unless Connection > handler
handler
else
Class.new( Connection ) {handler and include handler}
Expand Down Expand Up @@ -1361,6 +1367,7 @@ def EventMachine::set_runtime_error_hook &blk
class << self
def _open_file_for_writing filename, handler=nil
klass = if (handler and handler.is_a?(Class))
raise ArgumentError, 'must provide module or subclass of EventMachine::Connection' unless Connection > handler
handler
else
Class.new( Connection ) {handler and include handler}
Expand Down
37 changes: 37 additions & 0 deletions tests/test_handler_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
$:.unshift "../lib"
require 'eventmachine'
require 'test/unit'

class TestHandlerCheck < Test::Unit::TestCase

class Foo < EM::Connection; end;
module TestModule; end;

def test_with_correct_class
assert_nothing_raised do
EM.run {
EM.connect("127.0.0.1", 80, Foo)
EM.stop_event_loop
}
end
end

def test_with_incorrect_class
assert_raise(ArgumentError) do
EM.run {
EM.connect("127.0.0.1", 80, String)
EM.stop_event_loop
}
end
end

def test_with_module
assert_nothing_raised do
EM.run {
EM.connect("127.0.0.1", 80, TestModule)
EM.stop_event_loop
}
end
end

end

0 comments on commit 9b4ef82

Please sign in to comment.