[macruby-changes] [4362] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Thu Jul 15 10:14:08 PDT 2010


Revision: 4362
          http://trac.macosforge.org/projects/ruby/changeset/4362
Author:   pthomson at apple.com
Date:     2010-07-15 10:14:05 -0700 (Thu, 15 Jul 2010)
Log Message:
-----------
Added specs for Socket#sendfile so that Laurent doesn't hit me with his TDD bat.

Modified Paths:
--------------
    MacRuby/trunk/ext/socket/socket.c

Added Paths:
-----------
    MacRuby/trunk/spec/macruby/library/socket/
    MacRuby/trunk/spec/macruby/library/socket/fixtures/
    MacRuby/trunk/spec/macruby/library/socket/fixtures/sample.txt
    MacRuby/trunk/spec/macruby/library/socket/sendfile_spec.rb

Modified: MacRuby/trunk/ext/socket/socket.c
===================================================================
--- MacRuby/trunk/ext/socket/socket.c	2010-07-14 23:32:17 UTC (rev 4361)
+++ MacRuby/trunk/ext/socket/socket.c	2010-07-15 17:14:05 UTC (rev 4362)
@@ -3003,15 +3003,12 @@
     
     file = rb_io_check_io(file);
     
-    rb_io_t *source = ExtractIOStruct(self);
-    rb_io_t *dest = ExtractIOStruct(file);
+    rb_io_t *socket = ExtractIOStruct(self);
+    rb_io_t *source = ExtractIOStruct(file);
     
-    rb_io_assert_readable(source);
-    rb_io_assert_writable(dest);
-    
     off_t to_write = NUM2OFFT(len);
     
-    if (sendfile(source->read_fd, dest->write_fd, NUM2OFFT(offset), &to_write, NULL, 0) == -1) {
+    if (sendfile(source->fd, socket->fd, NUM2OFFT(offset), &to_write, NULL, 0) == -1) {
         if (needs_to_close) {
             rb_io_close(file);
         }
@@ -3567,6 +3564,7 @@
     rb_objc_define_method(rb_cBasicSocket, "recv_nonblock", bsock_recv_nonblock, -1);
     rb_objc_define_method(rb_cBasicSocket, "do_not_reverse_lookup", bsock_do_not_reverse_lookup, 0);
     rb_objc_define_method(rb_cBasicSocket, "do_not_reverse_lookup=", bsock_do_not_reverse_lookup_set, 1);
+    rb_objc_define_method(rb_cBasicSocket, "sendfile", socket_sendfile, 3);
 
     rb_cIPSocket = rb_define_class("IPSocket", rb_cBasicSocket);
     rb_objc_define_method(rb_cIPSocket, "addr", ip_addr, 0);
@@ -3625,7 +3623,6 @@
 
     rb_objc_define_method(rb_cSocket, "recvfrom", sock_recvfrom, -1);
     rb_objc_define_method(rb_cSocket, "recvfrom_nonblock", sock_recvfrom_nonblock, -1);
-    rb_objc_define_method(rb_cSocket, "sendfile", socket_sendfile, 3);
 
     rb_objc_define_method(*(VALUE *)rb_cSocket, "socketpair", sock_s_socketpair, 3);
     rb_objc_define_method(*(VALUE *)rb_cSocket, "pair", sock_s_socketpair, 3);

Added: MacRuby/trunk/spec/macruby/library/socket/fixtures/sample.txt
===================================================================
--- MacRuby/trunk/spec/macruby/library/socket/fixtures/sample.txt	                        (rev 0)
+++ MacRuby/trunk/spec/macruby/library/socket/fixtures/sample.txt	2010-07-15 17:14:05 UTC (rev 4362)
@@ -0,0 +1,4 @@
+Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
+Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
+Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
+Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\ No newline at end of file

Added: MacRuby/trunk/spec/macruby/library/socket/sendfile_spec.rb
===================================================================
--- MacRuby/trunk/spec/macruby/library/socket/sendfile_spec.rb	                        (rev 0)
+++ MacRuby/trunk/spec/macruby/library/socket/sendfile_spec.rb	2010-07-15 17:14:05 UTC (rev 4362)
@@ -0,0 +1,44 @@
+require 'socket'
+require 'dispatch'
+
+describe "Socket" do
+  describe :sendfile do
+    
+    it "should be able to send instances of IO" do
+      port = 2112
+      io = File.open(File.dirname(__FILE__) + '/fixtures/sample.txt')
+      server = TCPServer.open(port)
+      socket = TCPSocket.open('localhost', port)
+      
+      Dispatch::Queue.concurrent.async do
+        server.listen(5)
+        client = server.accept
+        client.sendfile(io, 0, io.stat.size)
+        client.close
+      end
+      
+      socket.readlines.should == io.readlines
+      socket.close
+      server.close
+    end
+    
+    it "should be able to send files specified by a path" do
+      port = 2112
+      path = File.dirname(__FILE__) + '/fixtures/sample.txt'
+      io = File.open(path)
+      server = TCPServer.open(port)
+      socket = TCPSocket.open('localhost', port)
+      
+      Dispatch::Queue.concurrent.async do
+        server.listen(5)
+        client = server.accept
+        client.sendfile(path, 0, io.stat.size)
+        client.close
+      end
+      
+      socket.readlines.should == io.readlines
+      socket.close
+      server.close
+    end
+  end
+end
\ No newline at end of file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100715/89dc7aca/attachment.html>


More information about the macruby-changes mailing list