Sha256: c643370347181ebbb029e1799225a80d2507ed37f600962c88dfc9b4d1ce6f09

Contents?: true

Size: 1.53 KB

Versions: 7

Compression:

Stored size: 1.53 KB

Contents

require File.expand_path('../spec_helper', __FILE__)
require 'tempfile'
require 'fcntl'

describe Cool.io::AsyncWatcher, :env => :win do

  it "does not signal on spurious wakeups" do
    aw = Cool.io::AsyncWatcher.new
    tmp = Tempfile.new('coolio_async_watcher_test')
    nr_fork = 2 # must be at least two for spurious wakeups

    # We have aetter chance of failing if this overflows the pipe buffer
    # which POSIX requires >= 512 bytes, Linux 2.6 uses 4096 bytes
    nr_signal = 4096 * 4

    append = File.open(tmp.path, "ab")
    append.sync = true
    rd, wr = ::IO.pipe

    aw.on_signal { append.syswrite("#$$\n") }
    children = nr_fork.times.map do
      fork do
        trap(:TERM) { exit!(0) }
        rloop = Cool.io::Loop.default
        aw.attach(rloop)
        wr.write '.' # signal to master that we're ready
        rloop.run
        exit!(1) # should not get here
      end
    end

    # ensure children are ready
    nr_fork.times { rd.sysread(1).should == '.' }

    # send our signals
    nr_signal.times { aw.signal }

    # wait for the pipe buffer to be consumed by the children
    sleep 1 while tmp.stat.ctime >= (Time.now - 4)

    children.each do |pid|
      Process.kill(:TERM, pid)
      _, status = Process.waitpid2(pid)
      status.exitstatus.should == 0
    end

    # we should've written a line for every signal we sent
    lines = tmp.readlines
    lines.size.should == nr_signal

    # theoretically a bad kernel scheduler could give us fewer...
    lines.sort.uniq.size.should == nr_fork

    tmp.close!
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
cool.io-1.2.3-x86-mingw32 spec/async_watcher_spec.rb
cool.io-1.2.3 spec/async_watcher_spec.rb
cool.io-1.2.2 spec/async_watcher_spec.rb
cool.io-1.2.1 spec/async_watcher_spec.rb
cool.io-1.2.0-x86-mswin32-60 spec/async_watcher_spec.rb
cool.io-1.2.0-x86-mingw32 spec/async_watcher_spec.rb
cool.io-1.2.0 spec/async_watcher_spec.rb