sendfile / tests / run.scm

;; %%HEADER%%

(use test)
(load "test-helper")
(use sendfile)

(test-begin "sendfile")


 (let* ((mb-buffer (generate-buffer (mebibytes 1)))
        (mb-checksum (buffer-checksum mb-buffer)))

   (define (stream-mb-buffer)
      (lambda (temp-file _)
        (stream-file temp-file sendfile))))
   (test-group "sendfile main interface"
               (test "sendfile" mb-checksum (stream-mb-buffer)))

   (test-group "forcing implementation"
               (parameterize ((force-implementation 'read-write))
                 (test "read-write" mb-checksum (stream-mb-buffer)))

               (if sendfile-available
                   (parameterize ((force-implementation 'sendfile))
                     (test "sendfile(2)" mb-checksum (stream-mb-buffer))))

               (if mmap-available
                   (parameterize ((force-implementation 'mmapped))
                     (test "mmap(2)" mb-checksum (stream-mb-buffer))))

               (parameterize ((force-implementation 'read-write-port))
                 (test "read-write-port" mb-checksum (stream-mb-buffer))))

   (test-group "read-write variations"
                (generate-buffer (mebibytes 1))
                (lambda (temp-file expected-checksum)
                  (test "ports only"
                         (lambda (server-in server-out)
                           (write-content-size server-out (mebibytes 1))
                           (call-with-input-file temp-file
                             (lambda (file-in)
                               (sendfile file-in server-out)))
                           (close-output-port server-out)
                           (read-checksum server-in)))))))

   (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) 
          (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)
                        (lambda (input output)
                          (sendfile input output offset: 40))))
                 (test "size"
                       (buffer-checksum head)
                        (lambda (input output)
                          (sendfile input output bytes: 20))))
                 (test "size and offset"
                       (buffer-checksum middle)
                        (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"               
               (kibibytes 1)
               (lambda (buffer checksum)
                 (test "custom input port without fd [bug #542]"
                        (lambda (server-in server-out)
                          (write-content-size server-out (kibibytes 1))
                          (sendfile (open-input-string buffer) server-out)
                          (close-output-port server-out)
                          (read-checksum server-in))))))
               (generate-buffer (mebibytes 2))
               (lambda (temp-file expected-checksum)
                 (test "send files > 1 mebibyte"
                       (stream-file temp-file sendfile)))))))

(test-end "sendfile")

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.