sleepy_penguin.git  about / heads / tags
Linux I/O events for Ruby
blob 0a7113d6c1232ad01c11be4fb82f025d2593b443 958 bytes (raw)
$ git show pu:test/test_cfr.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
 
# -*- encoding: binary -*-
require_relative 'helper'
require 'tempfile'

class TestCfr < Test::Unit::TestCase
  def test_copy_file_range
    str = 'abcde'
    size = 5
    src = Tempfile.new('ruby_cfr_src')
    dst = Tempfile.new('ruby_cfr_dst')
    assert_equal 5, src.syswrite(str)
    src.sysseek(0)
    begin
      nr = SleepyPenguin.copy_file_range(src, dst, size)
    rescue Errno::EINVAL
      warn 'copy_file_range not supported (requires Linux 4.5+)'
      warn "We have: #{`uname -a`}"
      return
    end
    assert_equal nr, 5
    dst.sysseek(0)
    assert_equal str, dst.sysread(5)

    nr = SleepyPenguin.copy_file_range(src, dst, size, off_in: 1, off_out: 0)
    assert_equal 4, nr
    dst.sysseek(0)
    assert_equal 'bcde', dst.sysread(4)

    nr = SleepyPenguin.copy_file_range(src, dst, size, off_in: 9)
    assert_equal 0, nr, 'no EOFError'
  ensure
    dst.close!
    src.close!
  end
end if SleepyPenguin.respond_to?(:copy_file_range)

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