Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix a typo in the unit test for Net::Ping::External and change the implementation for Windows #33

Merged
merged 4 commits into from

2 participants

Andrea Bernardo Ciddio Daniel Berger
Andrea Bernardo Ciddio

I had a typo in the test code for timeouts in Net::Ping::External; that was making the test pass even if the timeout wasn't enforced. I reimplemented it using the '-w' option of the Windows version of ping (hoping to avoid thread/process management entirely), and it seems to work now.

bcandrea added some commits
Andrea Bernardo Ciddio bcandrea Merge pull request #1 from djberg96/master
Merge back changes from djberg96
c034875
Andrea Bernardo Ciddio bcandrea Fix a typo in the Net::Ping::External test
This typo had the effect of setting the expected
ping duration to 5.5s instead of 1.5s. Since in Windows
the defaul ping timeout is 4s, the test always passed.
ef80ced
Andrea Bernardo Ciddio bcandrea Avoid implementing timeout on Windows
The -w option of ping on Windows has the same effect
of an external timeout.
9fe37ec
Andrea Bernardo Ciddio bcandrea Merge branch 'feature/fix-win32-external-ping' into develop c805658
Daniel Berger
Owner

Come to think of it, is there any reason not to just use -W on *nix platforms?

Andrea Bernardo Ciddio

Well, just a few minor annoyances. First, the timeout option might be different in hpux/solaris (I have no clue, to be honest). More importantly, it seems that things like

ping -W 0.3 -c 1 10.0.0.0

work correctly on OSX, but not on Linux (non-integer wait times are simply ignored). It might not be an issue, but in some cases I really wanted to have the option of dropping pings after a very short time - that was actually the reason why I started to test timeouts in the first place :)

Daniel Berger
Owner

Hm, this code wasn't really meant to be much more than a thin wrapper around your system's timeout command, so I worry about overcomplicating the code when we could just use -W. Are timeouts less than 1 second really useful in practice?

I'll double check Solaris and BSD to see if they have an option.

Anyway, in the meantime, I'll merge this and we can adapt as needed.

Daniel Berger djberg96 merged commit a4ae2bd into from
Andrea Bernardo Ciddio

Actually, the semantics of the options is different even between OSX and linux:

OSX:

-t timeout
             Specify a timeout, in seconds, before ping exits regardless of 
             how many packets have been received.
[...]
-W waittime
             Time in milliseconds to wait for a reply for each packet sent.  
             If a reply arrives later, the packet is not printed as
             replied, but considered as replied when calculating statistics.

Linux:

-t  ttl ping only.  Set the IP Time to Live.
-w  deadline
              Specify a timeout, in seconds, before ping exits regardless of 
              how many packets have been sent or  received.  In  this
              case  ping  does  not  stop after count packet are sent, it waits 
              either for deadline expire or until count probes are
              answered or for some error notification from network.
Andrea Bernardo Ciddio

In practice 1 sec is OK, it really doesn't matter (I was just a bit worried about being hit by subtle differences between OSes). Thanks!

Daniel Berger
Owner

Ok, maybe it's best to keep your approach in the name of uniformity. That's what the original Timeout block did.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 27, 2014
  1. Andrea Bernardo Ciddio

    Merge pull request #1 from djberg96/master

    bcandrea authored
    Merge back changes from djberg96
  2. Andrea Bernardo Ciddio

    Fix a typo in the Net::Ping::External test

    bcandrea authored
    This typo had the effect of setting the expected
    ping duration to 5.5s instead of 1.5s. Since in Windows
    the defaul ping timeout is 4s, the test always passed.
  3. Andrea Bernardo Ciddio

    Avoid implementing timeout on Windows

    bcandrea authored
    The -w option of ping on Windows has the same effect
    of an external timeout.
  4. Andrea Bernardo Ciddio
This page is out of date. Refresh to see the latest.
Showing with 26 additions and 8 deletions.
  1. +25 −7 lib/net/ping/external.rb
  2. +1 −1  test/test_net_ping_external.rb
32 lib/net/ping/external.rb
View
@@ -32,7 +32,7 @@ def ping(host = @host)
when /hpux/i
pcmd += [host, '-n1']
when /win32|windows|msdos|mswin|cygwin|mingw/i
- pcmd += ['-n', '1', host]
+ pcmd += ['-n', '1', '-w', (1000 * @timeout).to_i.to_s, host]
else
pcmd += [host]
end
@@ -40,7 +40,11 @@ def ping(host = @host)
start_time = Time.now
begin
- exit_code, err = run_with_timeout(*pcmd)
+ if File::ALT_SEPARATOR
+ exit_code, err = run_no_timeout(*pcmd)
+ else
+ exit_code, err = run_with_timeout(*pcmd)
+ end
case exit_code
when 0
bool = true # Success, at least one response.
@@ -87,11 +91,7 @@ def run_with_timeout(*command)
while (Time.now - start) < timeout and thread.alive?
begin
- if File::ALT_SEPARATOR
- err = stderr.read(ERR_MSG_SIZE)
- else
- err = stderr.read_nonblock(ERR_MSG_SIZE)
- end
+ err = stderr.read_nonblock(ERR_MSG_SIZE)
rescue IO::WaitReadable
IO.select([stderr], nil, nil, @timeout)
next
@@ -122,6 +122,24 @@ def run_with_timeout(*command)
return [exit_code, err]
end
+ # Runs a command without a timeout. The returned value
+ # is a list of the form
+ #
+ # [exit_code, err]
+ #
+ def run_no_timeout(*command)
+ out, err, exit_code = Open3.capture3(*command)
+ if exit_code != 0 and (err.nil? or err.empty?)
+ out.each_line do |line|
+ if line =~ /(timed out|could not find host)/i
+ err = line
+ break
+ end
+ end
+ end
+ return [exit_code, err]
+ end
+
alias ping? ping
alias pingecho ping
end
2  test/test_net_ping_external.rb
View
@@ -128,7 +128,7 @@ def setup
start_time = Time.now
@unreachable.ping
elapsed = Time.now - start_time
- assert_true(elapsed < @bad.timeout + tolerance)
+ assert_true(elapsed < @unreachable.timeout + tolerance)
end
def teardown
Something went wrong with that request. Please try again.