Commits

David Krentzlin committed 3e71f80

added better test-utils and started to work on tests that are easier and more reliable

  • Participants
  • Parent commits 4a2bc2b

Comments (0)

Files changed (3)

File tests/run2.scm

+;; 
+;; %%HEADER%%
+;; 
+
+(use test)
+(load "spec-helper")
+(use sendfile)
+
+(with-running-server
+ 
+ (let ((kb-buffer (generate-buffer (kibibytes 1)))
+       (mb-buffer (generate-buffer (mibibytes 1))))
+ 
+   (test-group "sendfile main interface"
+               (call-with-temporary-file/checksum
+                mb-buffer
+                (lambda (temp-file-path content-checksum)
+                  (let ((file-input (file-open temp-file-path (bitwise-ior open/rdonly open/binary))))
+                    (test content-checksum
+                          (call-with-connection-to-server
+                           (lambda (server-in server-out)
+                             (display (mibibytes 1) server-out)
+                             (newline server-out)
+                             (sendfile file-input server-out)
+                             (flush-output server-out)
+                             (read-line server-in)))))))))
+
+  (test-group "regression")
+
+ )
+

File tests/test-helper.scm

+;; 
+;; %%HEADER%%
+;;
+(use simple-sha1 tcp-server srfi-69 posix srfi-4)
+
+(define (notify fmt . args)
+  (apply printf fmt args)
+  (flush-output))
+
+(define (file-checksum path)
+  (sha1sum path))
+
+(define (buffer-checksum buffer)
+  (string->sha1sum buffer))
+
+(define (server-port)
+  (let ((p (get-environment-variable "SENDFILE_TEST_PORT")))
+    (or (and p (string->number p)) 5555)))
+
+(define (server)
+  (let ((listener (tcp-listen (server-port))))
+    (let loop ()
+      (receive (input output) (tcp-accept listener)
+        (handle-request input output)
+        (close-input-port input)
+        (close-output-port output))
+      (loop))))
+
+;; 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
+
+;; Use it like so:
+;; (call-with-connection-to-server (lambda (i o) (display "4" o) (newline o) (display "aaaa" o) (read-line i)))
+(define (handle-request input output)
+  (handle-exceptions exn
+      (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)))
+          (display (buffer-checksum content) output)
+          (newline output)
+          (flush-output output))))))
+
+(define (start-server #!key (fork #t))
+  (if fork (fork-server) (server)))
+
+(define (fork-server)
+  (let ((pid (process-fork server)))
+    (unless (wait-for-server 3)
+      (notify "could not start server!!!")
+      (exit 0))
+    (flush-output)
+    (sleep 4)
+    pid))
+
+(define (wait-for-server times)
+  (if (zero? times)
+      #f
+      (begin (sleep 1) (or (can-connect?) (wait-for-server (sub1 times))))))
+
+(define (can-connect?)
+  (handle-exceptions exn #f
+    (receive (in out)
+        (tcp-connect "localhost" (server-port))
+      (close-input-port in)
+      (close-output-port out)
+      #t)))
+
+(define (stop-server pid)
+  (process-signal pid))
+
+(define (call-with-running-server thunk)
+  (let ((pid (start-server)))
+    (thunk)
+    (stop-server pid)))
+
+(define-syntax with-running-server
+  (syntax-rules ()
+    ((_ code more-code ...)
+     (call-with-running-server
+      (lambda () code more-code ...)))))
+
+;; access the running server
+(define (call-with-connection-to-server proc)
+  (parameterize ((tcp-read-timeout 30000))
+    (receive (input output) (tcp-connect "localhost" (server-port))
+      (let ((result (proc input output)))
+        (close-input-port input)
+        (close-output-port output)
+        result))))
+
+(define (stream-file path size)
+  (call-with-connection-to-server
+   (lambda (input output)
+     (display size output)
+     (newline output)
+     (call-with-input-file path
+       (lambda (input-file)
+         (sendfile input-file output)))
+     (read-line input))))
+
+;generate a string of bytes bytes
+(define (with-buffer bytes proc)
+  (proc (make-string bytes #\a)))
+
+;; generate files
+(define (call-with-temporary-file content proc)
+  (let ((path (create-temporary-file)))
+    (with-output-to-file path (lambda () (display content)))
+    (let ((result (proc path)))
+      (delete-file path)
+      result)))
+
+(define (call-with-temporary-file/checksum content proc)
+  (call-with-temporary-file content
+    (lambda (tempfile-path)
+      (proc tempfile-path (buffer-checksum content)))))
+
+
+
+
+(define (mibibytes amount)
+  (* amount (kibibytes 1024)))
+
+(define (kibibytes amount)
+  (* amount 1024))
+
+(define (generate-buffer bytes)
+  (with-buffer bytes identity))

File tests/test-server.scm

+(load "spec-helper")
+
+(start-server fork: #f)