about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-03-14 16:38:58 -0700
committerEric Wong <normalperson@yhbt.net>2011-03-14 16:38:58 -0700
commita9bfa55f9a1d1c96d73367d3bb46f7b9e81fbbf9 (patch)
treea825ae5fb517d023e079147cb357a42b4a46718c
parent724f82bb5feaf1df40b02b8f353e94496f060647 (diff)
This matches behavior of the TCP version.
-rw-r--r--lib/raindrops/linux.rb18
-rw-r--r--test/test_linux.rb20
2 files changed, 31 insertions, 7 deletions
diff --git a/lib/raindrops/linux.rb b/lib/raindrops/linux.rb
index 889c661..55887f0 100644
--- a/lib/raindrops/linux.rb
+++ b/lib/raindrops/linux.rb
@@ -34,15 +34,19 @@ module Raindrops::Linux
   # counterpart due to the latter being able to use inet_diag via netlink.
   # This parses /proc/net/unix as there is no other (known) way
   # to expose Unix domain socket statistics over netlink.
-  def unix_listener_stats(paths)
+  def unix_listener_stats(paths = nil)
     rv = Hash.new { |h,k| h[k.freeze] = Raindrops::ListenStats.new(0, 0) }
-    paths = paths.map do |path|
-      path = path.dup
-      path.force_encoding(Encoding::BINARY) if defined?(Encoding)
-      rv[path]
-      Regexp.escape(path)
+    if nil == paths
+      paths = [ '[^\n]+' ]
+    else
+      paths = paths.map do |path|
+        path = path.dup
+        path.force_encoding(Encoding::BINARY) if defined?(Encoding)
+        rv[path]
+        Regexp.escape(path)
+      end
     end
-    paths = / 00000000 \d+ (\d+)\s+\d+ (#{paths.join('|')})$/n
+    paths = /^\w+: \d+ \d+ 00000000 \d+ (\d+)\s+\d+ (#{paths.join('|')})$/n
 
     # no point in pread since we can't stat for size on this file
     File.read(*PROC_NET_UNIX_ARGS).scan(paths) do |s|
diff --git a/test/test_linux.rb b/test/test_linux.rb
index d3c8da7..45dc2e0 100644
--- a/test/test_linux.rb
+++ b/test/test_linux.rb
@@ -39,6 +39,26 @@ class TestLinux < Test::Unit::TestCase
     assert_equal 1, stats[tmp.path].queued
   end
 
+  def test_unix_all
+    tmp = Tempfile.new("\xde\xad\xbe\xef") # valid path, really :)
+    File.unlink(tmp.path)
+    us = UNIXServer.new(tmp.path)
+    uc0 = UNIXSocket.new(tmp.path)
+    stats = unix_listener_stats
+    assert_equal 0, stats[tmp.path].active
+    assert_equal 1, stats[tmp.path].queued
+
+    uc1 = UNIXSocket.new(tmp.path)
+    stats = unix_listener_stats
+    assert_equal 0, stats[tmp.path].active
+    assert_equal 2, stats[tmp.path].queued
+
+    ua0 = us.accept
+    stats = unix_listener_stats
+    assert_equal 1, stats[tmp.path].active
+    assert_equal 1, stats[tmp.path].queued
+  end
+
   def test_tcp
     s = TCPServer.new(TEST_ADDR, 0)
     port = s.addr[1]