sleepy_penguin.git  about / heads / tags
Linux I/O events for Ruby
blob 3e44283d0cb1a52ce16aa7ba5171cadd2061ca4f 3424 bytes (raw)
$ git show pu:test/test_inotify.rb	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
 
require_relative 'helper'
require 'fcntl'
require 'tempfile'
require 'set'

class TestInotify < Test::Unit::TestCase
  include SleepyPenguin
  attr_reader :ino

  def teardown
    ObjectSpace.each_object(Inotify) { |io| io.close unless io.closed? }
    ObjectSpace.each_object(Tempfile) { |io| io.close unless io.closed? }
  end

  def test_new
    @ino = Inotify.new
    assert_kind_of(IO, ino)
    check_cloexec(ino)
  end

  def test_constants
    (Inotify.constants - IO.constants).each do |const|
      case const.to_sym
      when :Event, :Enumerator
      else
        nr = Inotify.const_get(const)
        assert nr <= 0xffffffff, "#{const}=#{nr}"
      end
    end
  end

  def test_new_nonblock
    ino = Inotify.new Inotify::NONBLOCK
    flags = ino.fcntl(Fcntl::F_GETFL) & Fcntl::O_NONBLOCK
    assert_equal(Fcntl::O_NONBLOCK, flags)
  end

  def test_new_cloeexec
    ino = Inotify.new Inotify::CLOEXEC
    flags = ino.fcntl(Fcntl::F_GETFD) & Fcntl::FD_CLOEXEC
    assert_equal(Fcntl::FD_CLOEXEC, flags)
  end

  def test_add_take
    ino = Inotify.new Inotify::CLOEXEC
    tmp1 = Tempfile.new 'take'
    tmp2 = Tempfile.new 'take'
    wd = ino.add_watch File.dirname(tmp1.path), Inotify::MOVE
    assert_kind_of Integer, wd
    File.rename tmp1.path, tmp2.path
    event = ino.take
    assert_equal wd, event.wd
    assert_kind_of Inotify::Event, event
    assert_equal File.basename(tmp1.path), event.name
    others = ino.instance_variable_get(:@inotify_tmp)
    assert_kind_of Array, others
    assert_equal 1, others.size
    assert_equal File.basename(tmp2.path), others[0].name
    assert_equal [ :MOVED_FROM ], event.events
    assert_equal [ :MOVED_TO ], others[0].events
    assert_equal wd, others[0].wd
    second_id = others[0].object_id
    assert_equal second_id, ino.take.object_id
    assert_nil ino.take(true)
  end

  def test_add_take_symbols
    ino = Inotify.new :CLOEXEC
    tmp1 = Tempfile.new 'take'
    tmp2 = Tempfile.new 'take'
    wd = ino.add_watch File.dirname(tmp1.path), :MOVE
    assert_kind_of Integer, wd
    File.rename tmp1.path, tmp2.path
    event = ino.take
    assert_equal wd, event.wd
    assert_kind_of Inotify::Event, event
    assert_equal File.basename(tmp1.path), event.name
    others = ino.instance_variable_get(:@inotify_tmp)
    assert_kind_of Array, others
    assert_equal 1, others.size
    assert_equal File.basename(tmp2.path), others[0].name
    assert_equal [ :MOVED_FROM ], event.events
    assert_equal [ :MOVED_TO ], others[0].events
    assert_equal wd, others[0].wd
    second_id = others[0].object_id
    assert_equal second_id, ino.take.object_id
    assert_nil ino.take(true)
  end

  def test_each
    ino = Inotify.new :CLOEXEC
    tmp1 = Tempfile.new 'take'
    wd = ino.add_watch tmp1.path, :OPEN
    assert_kind_of Integer, wd
    nr = 5
    o = File.open(tmp1.path)
    ino.each do |event|
      assert_equal [:OPEN], event.events
      break if (nr -= 1) == 0
      o = File.open(tmp1.path)
    end
    assert_equal 0, nr
  end

  def test_rm_watch
    ino = Inotify.new Inotify::CLOEXEC
    tmp = Tempfile.new 'a'
    wd = ino.add_watch tmp.path, Inotify::ALL_EVENTS
    assert_kind_of Integer, wd
    tmp.syswrite '.'
    event = ino.take
    assert_equal wd, event.wd
    assert_kind_of Inotify::Event, event
    assert_equal [:MODIFY], event.events
    assert_equal 0, ino.rm_watch(wd)
  end
end if defined?(SleepyPenguin::Inotify)

git clone https://yhbt.net/sleepy_penguin.git