Back

make-writer (clj)

(source)

protocol

(make-writer x opts)
Creates a BufferedWriter. See also IOFactory docs.

Examples

pcalcado/UpCloud
(ns upcloud.t_upload
  (:require [clojure.java.io :as io])
  (:use (midje sweet)
        (upcloud upload))
  (:import [java.io ByteArrayInputStream]))

                 (let [first-bytes (. "These are the first bytes..." getBytes)
                       more-bytes (. "and these are even more bytes!" getBytes)
                       writer-fn (make-writer-fn temp-dir upload-id notifier-fn file-size)]
                   (writer-fn first-bytes)
                   (writer-fn more-bytes)
                   (slurp temp-path) => "These are the first bytes...and these are even more bytes!")))

         (fact "should notify about progress every time it writes a chunk"
               (let [first-chunk (byte-array 1000)
                     file-size 1024
                     headers-size 100
                     file-plus-headers-size (+ file-size headers-size)
                     notifications (ref [])
                     notifier-fn (fn [upload-id current total] (dosync (alter notifications conj [upload-id current total])))]
                 
                 (io/delete-file temp-path true)
                 
                 (let [writer-fn (make-writer-fn temp-dir upload-id notifier-fn file-plus-headers-size)]
                   (await (writer-fn first-chunk))
                   
                   @notifications => [[upload-id 1000 1124]])))

         (fact "should notify about completion once no data is passed to it"
               (let [file-plus-headers-size 1025
                     notifications (ref [])
                     notifier-fn (fn [upload-id current total] (dosync (alter notifications conj [upload-id current total])))]
               
                 (io/delete-file temp-path true)
               
                 (let [writer-fn (make-writer-fn temp-dir upload-id notifier-fn file-plus-headers-size)]
                   (writer-fn)