forked from clojure-emacs/cider-nrepl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store exceptions so sync ops can get stacktraces
The u/err-info function will now automatically store exceptions in simple map-based, single-use, FIFO-expiring data store. This will allow the stacktrace middleware to act on Exceptions that were not bound to *e. See bug 1420 in CIDER: clojure-emacs/cider#1420. See also CIDER PR 1617: clojure-emacs/cider#1617
- Loading branch information
Showing
5 changed files
with
144 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
(ns cider.nrepl.middleware.util.storage) | ||
|
||
(def ^:private store (ref {})) | ||
(def ^:private exp-index (ref '())) | ||
(def ^:private capacity 10) | ||
|
||
(defn add! | ||
"Adds the provided kv pair to a single-retrieval store that also has | ||
queue-capacity expiring behavior; i.e. not only is kv removed from | ||
the store when sucessfully queried, bual also if addition of the | ||
supplied item causes store size to increase over the default | ||
capacity of 10 items, then the oldest kv pair in the store is | ||
expired (removed). Returns the value added to the store." | ||
[k v] | ||
(dosync | ||
(let [untrimmed-exp (conj @exp-index k) | ||
untrimmed-store (assoc @store k v) | ||
trimmable (drop capacity untrimmed-exp) | ||
trimmed-exp (remove (set trimmable) untrimmed-exp) | ||
trimmed-store (apply dissoc untrimmed-store trimmable)] | ||
(ref-set store trimmed-store) | ||
(ref-set exp-index trimmed-exp) | ||
v))) | ||
|
||
(defn query! | ||
"Queries the single-retrieval store. Finds value associated with the | ||
supplied key `k` and then removes the kv pair from storage. If `k` | ||
not present, returns nil and no changes are made." | ||
[k] | ||
(dosync | ||
(let [reply (get @store k)] | ||
(ref-set store (dissoc @store k)) | ||
(ref-set exp-index (remove #(= k %) @exp-index)) | ||
reply))) | ||
|
||
(defn refresh! | ||
"Refreshes the single-retrieval, queue-capacity store. Removes all kv | ||
pairs from the store." | ||
[] | ||
(dosync | ||
(ref-set store {}) | ||
(ref-set exp-index '()))) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
(ns cider.nrepl.middleware.util.storage-test | ||
(:require [cider.nrepl.middleware.util.storage :as c-store] | ||
[clojure.test :refer :all])) | ||
|
||
(use-fixtures :each (fn [f] (c-store/refresh!) (f))) | ||
|
||
(deftest test-single-add | ||
(is (= 1 (c-store/add! :a 1)))) | ||
|
||
(deftest multiple-add | ||
(is (= 1 (c-store/add! :a 1))) | ||
(is (= 2 (c-store/add! :b 2))) | ||
(is (= 3 (c-store/add! :c 3)))) | ||
|
||
(deftest test-empty-query | ||
(is (nil? (c-store/query! :a)))) | ||
|
||
(deftest test-miss-query | ||
(c-store/add! :b 2) | ||
(is (nil? (c-store/query! :a)))) | ||
|
||
(deftest test-hit-query | ||
(c-store/add! :a 1) | ||
(is (= 1 (c-store/query! :a))) | ||
(is (nil? (c-store/query! :a)))) | ||
|
||
(deftest test-over-capacity | ||
(c-store/add! :a 1) | ||
(c-store/add! :b 2) | ||
(c-store/add! :c 3) | ||
(c-store/add! :d 4) | ||
(c-store/add! :e 5) | ||
(c-store/add! :f 6) | ||
(c-store/add! :g 7) | ||
(c-store/add! :h 8) | ||
(c-store/add! :i 9) | ||
(c-store/add! :j 10) | ||
(c-store/add! :k 11) | ||
(is (nil? (c-store/query! :a))) | ||
(is (= 11 (c-store/query! :k)))) | ||
|
||
(deftest test-refresh | ||
(c-store/add! :a 1) | ||
(c-store/add! :b 2) | ||
(c-store/add! :c 3) | ||
(c-store/add! :d 4) | ||
(c-store/add! :e 5) | ||
(c-store/add! :f 6) | ||
(c-store/add! :g 7) | ||
(c-store/add! :h 8) | ||
(c-store/add! :i 9) | ||
(c-store/add! :j 10) | ||
(c-store/add! :k 11) | ||
(c-store/refresh!) | ||
(is (nil? (c-store/query! :a))) | ||
(is (nil? (c-store/query! :e))) | ||
(is (nil? (c-store/query! :k)))) |