David Krentzlin committed 9018496

added tests for content chunking

Comments (0)

Files changed (2)

                            (read-checksum server-in)))))))
-   (test-group "content chunking")   
+   (test-group "content chunking"
+      (let* ((head   (generate-buffer 20 #\a))
+             (middle (generate-buffer 20 #\b))
+             (tail   (generate-buffer 20 #\c))
+             (buffer (string-append head middle tail)))         
+        (define (chunking-test proc) 
+         (call-with-connection-to-server
+          (lambda (server-in server-out)
+            (let ((input (open-input-string buffer)))
+              (write-content-size server-out -1)
+              (proc input server-out)
+              (close-output-port server-out)
+              (read-checksum server-in)))))
+        (define-syntax test-chunking
+          (syntax-rules ()
+            ((_ implementation)
+             (test-group implementation
+               (parameterize ((force-implementation (string->symbol implementation)))          
+                 (test "offsets"
+                       (buffer-checksum tail)
+                       (chunking-test
+                        (lambda (input output)
+                          (sendfile input output offset: 40)))
+                       )
+                 (test "size"
+                       (buffer-checksum head)
+                       (chunking-test
+                        (lambda (input output)
+                          (sendfile input output bytes: 20))))
+                 (test "size and offset"
+                       (buffer-checksum middle)
+                       (chunking-test
+                        (lambda (input output)
+                          (sendfile input output offset: 20 bytes: 20)))))))))
+        (when sendfile-available
+          (test-chunking "sendfile"))
+        (when mmap-available
+          (test-chunking "mmapped"))
+        (test-chunking "read-write")
+        (test-chunking "read-write-port")))
    (test-group "bugs"               
                 (generate-buffer (mebibytes 2))
                 (lambda (temp-file expected-checksum)
-                  (test "send files > 1 mibibyte"
+                  (test "send files > 1 mebibyte"
                         (stream-file temp-file sendfile)))))))


 ;; the handler reads the input and writes back the checksum of
 ;; the data received.
 ;; Important: the implementation expected the bytes to receive to be
-;; the very first line of input
+;; the very first line of input. If this size is -1 then we just read
+;; until we reach eof-object
 ;; Use it like so:
 ;; (call-with-connection-to-server (lambda (i o) (display "4" o) (newline o) (display "aaaa" o) (read-line i)))
       (begin (display "Error" output)
              (display (get-condition-property exn 'exn 'msg) output)
              (newline output))
-    (let* ((bytes-following (read-line input)))
-      (unless (eof-object? bytes-following)
-        (let ((content (read-string (string->number bytes-following) input)))
+    (let* ((header (read-line input)))
+      (unless (eof-object? header)
+        (let* ((bytes-following (string->number header))
+               (content (read-string (if (positive? bytes-following) bytes-following #f) input)))
           (display (buffer-checksum content) output)
           (newline output)
           (flush-output output))))))
   (read-line port))
 ;generate a string of bytes bytes
-(define (call-with-buffer bytes proc)
-  (proc (make-string bytes #\a)))
+(define (call-with-buffer bytes proc #!optional (char #\a))
+  (proc (make-string bytes char)))
 (define (call-with-buffer/checksum buffer-size proc)
 (define (kibibytes amount)
   (* amount 1024))
-(define (generate-buffer bytes)
-  (call-with-buffer bytes identity))
+(define (generate-buffer bytes #!optional (char #\a))
+  (call-with-buffer bytes identity char))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.