Permalink
Browse files

create clients that disconnect

  • Loading branch information...
joshbuddy committed Feb 3, 2012
1 parent 8da098c commit dc73bbb9c59125e38588e81ccdf84897b15541a8
Showing with 30 additions and 1 deletion.
  1. +14 −1 lib/protoplasm/client/blocking_client.rb
  2. +16 −0 test/protoplasm_test.rb
@@ -10,6 +10,19 @@ def self.for_types(types)
cls
end
+ def close
+ _socket.close if _socket && !_socket.closed?
+ end
+
+ def self.client(*args)
+ client = new(*args)
+ begin
+ yield client
+ ensure
+ client.close
+ end
+ end
+
private
def host_port
raise "Must be implemented by the client class"
@@ -24,7 +37,7 @@ def _socket
s.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)
s
end
- yield @socket
+ yield @socket if block_given?
rescue Errno::EPIPE, Errno::ECONNRESET
count += 1
if count > 3
View
@@ -43,6 +43,22 @@
end
end
+ it "should allow a self closing client" do
+ with_proto_server(ProtoplasmTest::EMServer) do |port|
+ ProtoplasmTest::Client.client('127.0.0.1', port) do |client|
+ client.ping
+ assert_equal "LOWER", client.upcase('lower')
+ assert_equal "UPPER", client.upcase('upper')
+ nums = []
+ client.evens(10) do |resp|
+ nums << resp
+ end
+ assert_equal [2, 4, 6, 8], nums
+ client.ping
+ assert_equal "LOWER", client.upcase('lower')
+ end
+ end
+ end
it "should do constant lookups" do
assert_equal :PING, ProtoplasmTest::Types::Command::Type.lookup(1)
assert_equal :UPCASE, ProtoplasmTest::Types::Command::Type.lookup(2)

0 comments on commit dc73bbb

Please sign in to comment.