mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	* ext/-test-/wait_for_single_fd: New. for testing
rb_wait_for_single_fd() internal function. The patch was written by Eric Wong. [ruby-core:35991] * test/-ext-/wait_for_single_fd/test_wait_for_single_fd.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31428 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
		
							parent
							
								
									8a98c57ff0
								
							
						
					
					
						commit
						2a989121db
					
				
					 5 changed files with 73 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -1,3 +1,11 @@
 | 
			
		|||
Wed May  4 18:46:39 2011  KOSAKI Motohiro  <kosaki.motohiro@gmail.com>
 | 
			
		||||
 | 
			
		||||
	* ext/-test-/wait_for_single_fd: New. for testing
 | 
			
		||||
	  rb_wait_for_single_fd() internal function.
 | 
			
		||||
	  The patch was written by Eric Wong. [ruby-core:35991]
 | 
			
		||||
 | 
			
		||||
	* test/-ext-/wait_for_single_fd/test_wait_for_single_fd.rb: ditto.
 | 
			
		||||
 | 
			
		||||
Wed May  4 12:46:25 2011  KOSAKI Motohiro  <kosaki.motohiro@gmail.com>
 | 
			
		||||
 | 
			
		||||
	* thread.c (rb_wait_for_single_fd): Added POLLNVAL check.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										2
									
								
								ext/-test-/wait_for_single_fd/depend
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								ext/-test-/wait_for_single_fd/depend
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,2 @@
 | 
			
		|||
wait_for_single_fd.o: $(top_srcdir)/thread.c \
 | 
			
		||||
  $(hdrdir)/ruby/ruby.h $(hdrdir)/ruby/io.h
 | 
			
		||||
							
								
								
									
										1
									
								
								ext/-test-/wait_for_single_fd/extconf.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								ext/-test-/wait_for_single_fd/extconf.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
create_makefile("-test-/wait_for_single_fd/wait_for_single_fd")
 | 
			
		||||
							
								
								
									
										30
									
								
								ext/-test-/wait_for_single_fd/wait_for_single_fd.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								ext/-test-/wait_for_single_fd/wait_for_single_fd.c
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,30 @@
 | 
			
		|||
#include "ruby/ruby.h"
 | 
			
		||||
#include "ruby/io.h"
 | 
			
		||||
 | 
			
		||||
static VALUE
 | 
			
		||||
wait_for_single_fd(VALUE ign, VALUE fd, VALUE events, VALUE timeout)
 | 
			
		||||
{
 | 
			
		||||
    struct timeval tv;
 | 
			
		||||
    struct timeval *tvp = NULL;
 | 
			
		||||
    int rc;
 | 
			
		||||
 | 
			
		||||
    if (!NIL_P(timeout)) {
 | 
			
		||||
	tv = rb_time_timeval(timeout);
 | 
			
		||||
	tvp = &tv;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    rc = rb_wait_for_single_fd(NUM2INT(fd), NUM2INT(events), tvp);
 | 
			
		||||
    if (rc == -1)
 | 
			
		||||
	rb_sys_fail("rb_wait_for_single_fd");
 | 
			
		||||
    return INT2NUM(rc);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
Init_wait_for_single_fd(void)
 | 
			
		||||
{
 | 
			
		||||
    rb_define_const(rb_cObject, "RB_WAITFD_IN", INT2NUM(RB_WAITFD_IN));
 | 
			
		||||
    rb_define_const(rb_cObject, "RB_WAITFD_OUT", INT2NUM(RB_WAITFD_OUT));
 | 
			
		||||
    rb_define_const(rb_cObject, "RB_WAITFD_PRI", INT2NUM(RB_WAITFD_PRI));
 | 
			
		||||
    rb_define_singleton_method(rb_cIO, "wait_for_single_fd",
 | 
			
		||||
                               wait_for_single_fd, 3);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										32
									
								
								test/-ext-/wait_for_single_fd/test_wait_for_single_fd.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								test/-ext-/wait_for_single_fd/test_wait_for_single_fd.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,32 @@
 | 
			
		|||
require 'test/unit'
 | 
			
		||||
 | 
			
		||||
class TestWaitForSingleFD < Test::Unit::TestCase
 | 
			
		||||
  require '-test-/wait_for_single_fd/wait_for_single_fd'
 | 
			
		||||
 | 
			
		||||
  def with_pipe
 | 
			
		||||
    r, w = IO.pipe
 | 
			
		||||
    begin
 | 
			
		||||
      yield r, w
 | 
			
		||||
    ensure
 | 
			
		||||
      r.close unless r.closed?
 | 
			
		||||
      w.close unless w.closed?
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_wait_for_valid_fd
 | 
			
		||||
    with_pipe do |r,w|
 | 
			
		||||
      rc = IO.wait_for_single_fd(w.fileno, RB_WAITFD_OUT, nil)
 | 
			
		||||
      assert_equal RB_WAITFD_OUT, rc
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_wait_for_invalid_fd
 | 
			
		||||
    with_pipe do |r,w|
 | 
			
		||||
      wfd = w.fileno
 | 
			
		||||
      w.close
 | 
			
		||||
      assert_raises(Errno::EBADF) do
 | 
			
		||||
        IO.wait_for_single_fd(wfd, RB_WAITFD_OUT, nil)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue