Back

periodic-seq (clj)

(source)

function

(periodic-seq start duration-or-period)

Examples

chime
(ns chime-test
  (:require
   [chime :refer :all]
   [clojure.core.async :as a :refer [<! go-loop]]
   [clojure.test :refer :all]
   [chime.core :as chime]
   [chime.joda-time]
   [clj-time.core :as t]
   [clj-time.periodic])
  (:import (java.time Instant)
           (java.time.temporal ChronoUnit)))

  (let [proof (atom [])
        ch (chime-ch (->> (chime/periodic-seq (-> (.plusSeconds (chime-test/now) 2)
                                                  (.truncatedTo (ChronoUnit/SECONDS)))
                                              (java.time.Duration/ofSeconds 1))
                          (take 3)))]

(deftest test-cancellation-works-even-in-the-face-of-overrun-past-tasks
  (let [proof (atom [])
        do-stuff (fn [now]
                   ;; some overrunning task:
                   (swap! proof conj now)
                   (Thread/sleep 5000))
        cancel-stuff! (chime-at (rest (chime/periodic-seq (chime-test/now)
                                                          (java.time.Duration/ofSeconds 1)))
                                do-stuff)]
    (Thread/sleep 3000)
    (cancel-stuff!)
    (is (= 1
           (count @proof)))))

(deftest backwards-compatibility-without-past-times-test
  (let [times (-> (t/date-time 1990 1 1)
                  (t/from-time-zone (t/time-zone-for-offset -3))
                  (clj-time.periodic/periodic-seq (t/days 1))
                  (as-> x
                    (take 100 x)))]
    (is (empty? (chime/without-past-times times)))))
chime
(ns chime.core-test
  (:require [chime.core :as chime]
            [clojure.test :as t])
  (:import (java.time Instant Duration)
           (java.time.temporal ChronoUnit)))

(t/deftest test-long-running-jobs
  (let [proof (atom [])
        !latch (promise)
        now (Instant/now)
        times (->> (chime/periodic-seq now (Duration/ofMillis 500))
                   (take 3))
        sched (chime/chime-at times
                            (fn [time]
                              (swap! proof conj [time (Instant/now)])
                              (Thread/sleep 750)))]

    (t/is (not= ::nope (deref sched 4000 ::nope)))
    (t/is (= times (map first @proof)))
    (check-timeliness! (map vector
                            (->> (chime/periodic-seq now (Duration/ofMillis 750))
                                 (take 3))
                            (map second @proof)))))

(t/deftest test-cancelling-overrunning-task
  (let [!proof (atom [])
        !error (atom nil)
        !latch (promise)]
    (with-open [sched (chime/chime-at (chime/periodic-seq (Instant/now) (Duration/ofSeconds 1))
                                      (fn [now]
                                        (swap! !proof conj now)
                                        (Thread/sleep 3000))
                                      {:error-handler (fn [e]
                                                        (reset! !error e))
                                       :on-finished (fn []
                                                      (deliver !latch nil))})]
      (Thread/sleep 2000))