Skip to content

Commit

Permalink
wip: considering remaining-chimes for #30:
Browse files Browse the repository at this point in the history
not 100% on this API because they're not accessible from within the scheduling fn - but we can't make them accessible easily without changing the arity of the schedule fn
  • Loading branch information
jarohen committed Jul 12, 2020
1 parent 0739e6d commit f8fa945
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
53 changes: 31 additions & 22 deletions src/chime/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
(doto (Thread. r)
(.setName (format "chime-" (swap! !count inc))))))))

(defprotocol ChimeSchedule
(remaining-chimes [chime-schedule]))

(defn chime-at
"Calls `f` with the current time at every time in the `times` sequence.
Expand Down Expand Up @@ -84,36 +87,42 @@
(let [interrupted? (instance? InterruptedException e)]
(when-not interrupted?
(log/warn e "Error running scheduled fn"))
(not interrupted?))))]
(not interrupted?))))
!times (atom (map ->instant times))]
(letfn [(close []
(.shutdownNow pool)
(deliver !latch nil)
(when on-finished
(on-finished)))

(schedule-loop [[time & times]]
(letfn [(task []
(if (try
(f time)
true
(catch Exception e
(try
(exception-handler e)
(catch Throwable e
(log/error e "Error calling Chime exception-handler, stopping schedule"))))
(catch Throwable t
(log/error t (str (class t) " thrown, stopping schedule"))))

(schedule-loop times)
(close)))]

(if time
(.schedule pool ^Runnable task (.between ChronoUnit/MILLIS (now) time) TimeUnit/MILLISECONDS)
(close))))]

(schedule-loop (map ->instant times))
(schedule-loop []
(let [time (first @!times)]
(letfn [(task []
(if (try
(swap! !times rest)
(f time)
true
(catch Exception e
(try
(exception-handler e)
(catch Throwable e
(log/error e "Error calling Chime exception-handler, stopping schedule"))))
(catch Throwable t
(log/error t (str (class t) " thrown, stopping schedule"))))

(schedule-loop)
(close)))]

(if time
(.schedule pool ^Runnable task (.between ChronoUnit/MILLIS (now) time) TimeUnit/MILLISECONDS)
(close)))))]

(schedule-loop)

(reify
ChimeSchedule
(remaining-chimes [_] @!times)

AutoCloseable
(close [_] (close))

Expand Down
14 changes: 14 additions & 0 deletions test/chime/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@

(t/is @proof))))

(t/deftest test-remaining-chimes
(let [times [(.plusMillis (Instant/now) 500)
(.plusMillis (Instant/now) 1000)]
!proof (atom nil)
!latch (promise)
sched (chime/chime-at times
(fn [time]
(deliver !latch time)))]
(t/is (= times (chime/remaining-chimes sched)))
@!latch
(t/is (= (drop 1 times) (chime/remaining-chimes sched)))
@sched
(t/is (empty? (chime/remaining-chimes sched)))))

(t/deftest test-on-finished
(let [proof (atom false)]
(chime/chime-at [(.plusMillis (Instant/now) 500) (.plusMillis (Instant/now) 1000)]
Expand Down

0 comments on commit f8fa945

Please sign in to comment.