diff options
author | Eric Wong <normalperson@yhbt.net> | 2011-11-02 02:40:03 +0000 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2011-11-02 02:40:03 +0000 |
commit | 46355ddfecb4e210b4dd3b6f91dde330e8d509ff (patch) | |
tree | bf99c42aa42eb9ba1c5fa1cb2dbe6764ed7b261f | |
parent | af2d9c971cee5a6896fa3da44adff72d5f2ae44a (diff) | |
download | mogilefs-client-46355ddfecb4e210b4dd3b6f91dde330e8d509ff.tar.gz |
No more mucking with existing Ruby classes \o/
-rw-r--r-- | lib/mogilefs/network.rb | 2 | ||||
-rw-r--r-- | lib/mogilefs/util.rb | 55 | ||||
-rw-r--r-- | test/test_util.rb | 59 |
3 files changed, 1 insertions, 115 deletions
diff --git a/lib/mogilefs/network.rb b/lib/mogilefs/network.rb index cf7f366..95647d2 100644 --- a/lib/mogilefs/network.rb +++ b/lib/mogilefs/network.rb @@ -11,7 +11,7 @@ module MogileFS::Network # first, we asynchronously connect to all of them uris.each do |uri| - sock = Socket.mogilefs_new_nonblock(uri.host, uri.port) rescue next + sock = MogileFS::Socket.start(uri.host, uri.port) rescue next uri_socks[sock] = uri end diff --git a/lib/mogilefs/util.rb b/lib/mogilefs/util.rb index 1fb5ffb..8f315aa 100644 --- a/lib/mogilefs/util.rb +++ b/lib/mogilefs/util.rb @@ -111,59 +111,4 @@ class Socket true end end - - class << self - - # Creates a new (TCP) Socket and initiates (but does not wait for) the - # connection - def mogilefs_new_nonblock(host, port) - sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0) - if defined?(Socket::TCP_NODELAY) - sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) - end - sock.mogilefs_init(host, port) - sock - end - - # Like TCPSocket.new(host, port), but with an explicit timeout - # (and we don't care for local address/port we're binding to). - # This raises MogileFS::Timeout if timeout expires - def mogilefs_new(host, port, timeout = 5.0) - sock = mogilefs_new_nonblock(host, port) or return sock - - while timeout > 0 - t0 = Time.now - r = IO.select(nil, [sock], nil, timeout) - return sock if r && r[1] && sock.mogilefs_init(host, port) - timeout -= (Time.now - t0) - end - - sock.close rescue nil - raise MogileFS::Timeout, 'socket write timeout' - end - - include MogileFS::Util - - # Makes a request on a new TCP Socket and returns with a readble socket - # within the given timeout. - # This raises MogileFS::Timeout if timeout expires - def mogilefs_new_request(host, port, request, timeout = 5.0) - t0 = Time.now - sock = mogilefs_new(host, port, timeout) - syswrite_full(sock, request, timeout) - timeout -= (Time.now - t0) - if timeout < 0 - sock.close rescue nil - raise MogileFS::Timeout, 'socket read timeout' - end - r = IO.select([sock], nil, nil, timeout) - return sock if r && r[0] - - sock.close rescue nil - raise MogileFS::Timeout, 'socket read timeout' - end - - end - end - diff --git a/test/test_util.rb b/test/test_util.rb deleted file mode 100644 index 416970b..0000000 --- a/test/test_util.rb +++ /dev/null @@ -1,59 +0,0 @@ -# -*- encoding: binary -*- -require './test/setup' - -class TestMogileFS__Util < Test::Unit::TestCase - include MogileFS::Util - - def test_mogilefs_write - rd, wr = IO.pipe - - svr = Proc.new do |serv, port| - client, client_addr = serv.accept - client.sync = true - nr = 0 - loop do - begin - nr += client.readpartial(16384).length - rescue EOFError - break - end - end - wr.syswrite("#{nr}\n") - client.close rescue nil - end - t = TempServer.new(svr) - s = Socket.mogilefs_new('127.0.0.1', t.port) - tmp = s.getsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF) - sndbuf_bytes = tmp.unpack('i')[0] - big_string = ' ' * (sndbuf_bytes * 10) - - sent = s.send(big_string, 0) - assert(sent < big_string.length) - - syswrite_full(s, big_string) - s.close rescue nil - IO.select([rd]) - assert_equal((sent + big_string.length), rd.sysread(4096).to_i) - ensure - t.destroy! - end - - def test_write_timeout - svr = Proc.new do |serv, port| - client, client_addr = serv.accept - client.sync = true - readed = client.readpartial(16384) - sleep - end - t = TempServer.new(svr) - s = Socket.mogilefs_new('127.0.0.1', t.port) - tmp = s.getsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF) - sndbuf_bytes = tmp.unpack('i')[0] - big_string = ' ' * (sndbuf_bytes * 10) - - assert_raises(MogileFS::Timeout) { syswrite_full(s, big_string, 0.1) } - s.close rescue nil - ensure - t.destroy! - end -end |