test/test-filetransfer.rkt
#lang racket
(provide filetransfer-tests)
(require rackunit
         rackunit/gui
         (planet erast/file-utils)
         "../display-measures.rkt"
         "../filetransfer.rkt")

; TESTS

(define (test-listen)
  (letrec ((t (start-listen 1431 (build-path "test-received") "127.0.0.1" 
                            (lambda (phase path progress) 
                              (when (= (remainder progress 10) 0)
                                (test-case
                                 (format "received ~a%" progress)
                                 (check-true (and (path? path) (number? progress) (symbol? phase))))))
                            (lambda (code path msecs bytes) 
                              (test-case 
                               "receiver finalizer proc"
                               (check-true (and (symbol? code) (path? path) (number? msecs) (number? bytes))))
                              (test-case
                               "file equality"
                               (file-equal? test-picture path))
                              (test-case 
                               "received file exists"
                               (check-true (file-exists? path)))
                              (test-case
                               "transfer?"
                               (check-true (filetransfer? t)))
                              (delete-file path)))))
    t))

(define (test-listen2)
  (letrec ((number 0)
           (t (start-listen 1431 (build-path "test-received") "127.0.0.1" 
                            (lambda (phase path progress) 
                              (void))
                            (lambda (code path msecs bytes) 
                              (set! number (add1 number))
                              (test-case 
                               (format "file ~a received, passed file check" number)
                               (file-equal? test-picture path))
                              (delete-file path)
                              (when (= number 10)
                                (finish-transfer t)))
                            )))
    t))

(define (test-send)
  (define t (send-file "127.0.0.1" 1431 test-picture "picture.tif"
             (lambda (phase path progress) 
               (when (= (remainder progress 10) 0)
                 (test-case 
                  (format "sent ~a%" progress)
                  (check-true (and (path? path) (number? progress) (symbol? phase))))))
             (lambda (code path msecs bytes) 
               (test-case
                "sender finalizer proc"
                (check-true (and (symbol? code) (path? path) (number? msecs) (number? bytes))))
               (test-case
                "average speed > 0"
                (check-true (and (> msecs 0) (> (/ bytes msecs) 0)))))))
  (wait-transfer t))

(define (test-send2)
  (for ([number (in-range 1 11)])
    (send-file "127.0.0.1" 1431 test-picture "picture.tif"
               (lambda (phase path progress) 
                 (void))
               (lambda (code path msecs bytes) 
                 (test-case
                  (format "file ~a sent" number)
                  (check-true (and (symbol? code) (path? path) (number? msecs) (number? bytes))))))))


(define test-picture (build-path "testfiles" "picture.tif"))

(define filetransfer-tests
  (test-suite 
   "Filetransfer Library"
   (let ((t (test-listen)))
     (test-case
      "start-listen returns filetransfer"
      (check-true (filetransfer? t)))
     (test-send)
     (test-case 
      "finish-transfer returns void"
      (check-true (void? (finish-transfer t))))
     (test-listen2)
     (test-send2))))

(define (test-all)
  (test/gui filetransfer-tests))