             [hatnik.versions :as ver]
             [com.stuartsierra.component :as component]
             [hatnik.test-utils :refer :all :exclude [config]]
-            [taoensso.timbre :as timbre]))
+            [taoensso.timbre :as timbre])
+  (:import [java.util.concurrent Semaphore TimeUnit]))
 
 ;;;
 ;;; Integration test that verifies that worker job is executed periodically.

(defn mock-latest-release-fn
  "Create latest-release version which returns
  new version every time it called."
  []
  (let [versions (atom {"lib-1" 0
                        "lib-2" 1
                        "lib-3" 2
                        "lib-4" 3})]
    (fn [library]
      (timbre/debug library)
      (let [version (-> versions
                        (swap! update-in [library] inc)
                        (get library))]
        (str version ".0")))))

(defn create-function-mock
  "Creates mock funciton which saves all arguments it called with into
  'arguments' atom vector. Also delivers 'true' to the 'finished' promise
  once it called times-to-call times."
  [arguments times-to-call finished]
  (let [counter (atom times-to-call)]
    (fn [& args]
      (when (zero? @counter)
        (throw (ex-info (str "Function expected to be called only "
                             times-to-call " times.")
                        {})))
      (swap! arguments conj (vec args))
      (when (zero? (swap! counter dec))
        (deliver finished true)))))

(defn create-actions []
  (binding [clj-http.core/*cookie-store* (clj-http.cookies/cookie-store)]

                                        :utils utils})
                   component/start)]
    (try
      ; Wait for the first update by worker and update libraries.
      (acquire email-semaphore 2)
      (acquire gi-semaphore 1)
      (reset! versions {"lib-1" 3
                        "lib-2" 4
                        "lib-3" 5
                        "lib-4" 6})

      ; Wait for the second update by worker and verify send-email
      ; and create-github-issue mocks.
      (acquire email-semaphore 2)
      (acquire gi-semaphore 1)
      (assert-emails-args @email-args)
      (assert-github-issue-args @gi-args)

