Skip to content

Instantly share code, notes, and snippets.

@pbostrom
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save pbostrom/86c261730641edca5744 to your computer and use it in GitHub Desktop.

Select an option

Save pbostrom/86c261730641edca5744 to your computer and use it in GitHub Desktop.
Concurrency bugs with with-redefs and deftest
;; with-redefs
``` clojure
(defn print-it []
(println "don't print this"))
(defn dont-print [x]
(with-redefs [print-it (constantly nil)]
(print-it)))
(defn -main []
(doall (pmap dont-print (range 1000)))
nil)
```
;;$ lein run
;;don't print this
;;don't print this
;;don't print this
;;don't print this
;; deftest/is
(deftest test-counter-parallel
(doall (pmap (fn [x] (is true)) (range 1000))))
(deftest test-counter
(doall (map (fn [x] (is true)) (range 1000))))
;;$ lein test :only pmap-bugs.core-test/test-counter-parallel
;;
;;lein test pmap-bugs.core-test
;;
;;Ran 1 tests containing 194 assertions.
;;0 failures, 0 errors.
;;$ lein test :only pmap-bugs.core-test/test-counter
;;
;;lein test pmap-bugs.core-test
;;
;;Ran 1 tests containing 1000 assertions.
;;0 failures, 0 errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment