Linux::TCP_Info: implement #get! instance method
authorEric Wong <normalperson@yhbt.net>
Thu, 11 Apr 2013 23:13:13 +0000 (11 23:13 +0000)
committerEric Wong <normalperson@yhbt.net>
Thu, 11 Apr 2013 23:13:13 +0000 (11 23:13 +0000)
This allows reusing existing Linux::TCP_Info objects to avoid
generating garbage.

ext/raindrops/linux_tcp_info.c
test/test_linux_tcp_info.rb

index ce1c2d3..dcdb153 100644 (file)
@@ -131,6 +131,7 @@ void Init_raindrops_linux_tcp_info(void)
        cTCP_Info = rb_define_class_under(cRaindrops, "TCP_Info", rb_cObject);
        rb_define_alloc_func(cTCP_Info, alloc);
        rb_define_private_method(cTCP_Info, "initialize", init, 1);
+       rb_define_method(cTCP_Info, "get!", init, 1);
 
 #define TCPI_DEFINE_METHOD(x) \
        rb_define_method(cTCP_Info, #x, tcp_info_##x, 0)
index 3b4a245..c947211 100644 (file)
@@ -12,6 +12,7 @@ class TestLinuxTCP_Info < Test::Unit::TestCase
   # Linux kernel commit 5ee3afba88f5a79d0bff07ddd87af45919259f91
   TCP_INFO_useful_listenq = `uname -r`.strip >= '2.6.24'
 
+
   def test_tcp_server
     s = TCPServer.new(TEST_ADDR, 0)
     rv = Raindrops::TCP_Info.new s
@@ -23,6 +24,11 @@ class TestLinuxTCP_Info < Test::Unit::TestCase
     a = s.accept
     tmp = Raindrops::TCP_Info.new s
     assert_equal 0, tmp.unacked
+    before = tmp.object_id
+
+    tmp.get!(s)
+    assert_equal before, tmp.object_id
+
     ensure
       c.close if c
       a.close if a
@@ -35,6 +41,7 @@ class TestLinuxTCP_Info < Test::Unit::TestCase
     tcp_info_methods = tmp.methods - Object.new.methods
     assert tcp_info_methods.size >= 32
     tcp_info_methods.each do |m|
+      next if m.to_sym == :get!
       val = tmp.__send__ m
       assert_kind_of Integer, val
       assert val >= 0