Created
June 23, 2014 08:43
-
-
Save paulbellamy/54d7d908d7ebce5bd2c0 to your computer and use it in GitHub Desktop.
How to lock up core.async
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(import '[java.util.concurrent CountDownLatch]) | |
(require '[clojure.core.async :as async]) | |
(defn now [] | |
(System/currentTimeMillis)) | |
(defn waiter [i latch results] | |
(async/go | |
(swap! results conj [i :waiting (now)]) | |
(.await latch) ;; Will block the thread | |
(swap! results conj [i :done (now)]))) | |
(def core-async-thread-count (-> (Runtime/getRuntime) (.availableProcessors) (* 2) (+ 42))) | |
(def x (+ core-async-thread-count 1)) | |
(defn- parse-results [results] | |
(->> @results | |
(group-by second) | |
(map (fn [[k v]] [k (count v)])) | |
(into {}))) | |
(defn show-bug [] | |
(let [latch (CountDownLatch. 1) | |
results (atom [])] | |
(dotimes [i x] (waiter i latch results)) | |
(let [parsed (parse-results results)] | |
(if (= x (:waiting parsed)) | |
(println "no lockup!") | |
(println "Expected" x "to be waiting, but there were" parsed))))) | |
(show-bug) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment