From a678366fb326e2371b2e12145794a865d226ed6f Mon Sep 17 00:00:00 2001 From: Jose Gomez Date: Sun, 7 Jan 2024 10:19:32 -0600 Subject: [PATCH] feat: add with-pool macro --- CHANGELOG.md | 3 +++ pom.xml | 4 ++-- src/futurama/core.clj | 6 ++++++ test/futurama/core_test.clj | 18 +++++++++++++++--- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90ab230..0797a46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ This is a history of changes to k13labs/futurama +# 0.3.9 +* Add `with-pool` macro + # 0.3.8 * Add support for `Future` and `IDeref`. * Rename and refactor internal `satisfies?` to `instance-satisfies?` and `class-satisfies?` diff --git a/pom.xml b/pom.xml index a799b71..1bbcd69 100644 --- a/pom.xml +++ b/pom.xml @@ -5,9 +5,9 @@ com.github.k13labs futurama futurama - 0.3.9-SNAPSHOT + 0.3.9 - 0.3.9-SNAPSHOT + 0.3.9 https://github.com/k13labs/futurama scm:git:git://github.com/k13labs/futurama.git scm:git:ssh://git@github.com/k13labs/futurama.git diff --git a/src/futurama/core.clj b/src/futurama/core.clj index b69012a..1807b5e 100644 --- a/src/futurama/core.clj +++ b/src/futurama/core.clj @@ -19,6 +19,12 @@ (def ^:dynamic *thread-pool* (ForkJoinPool/commonPool)) +(defmacro with-pool + "Utility macro which binds *thread-pool* to the supplied pool and then evaluates the `body`." + [pool & body] + `(binding [*thread-pool* ~pool] + ~@body)) + (defn dispatch "dispatch the function by submitting it to the `*thread-pool*`" ^Future [^Runnable f] diff --git a/test/futurama/core_test.clj b/test/futurama/core_test.clj index 8949a6a..9539273 100644 --- a/test/futurama/core_test.clj +++ b/test/futurama/core_test.clj @@ -1,7 +1,7 @@ (ns futurama.core-test (:require [clojure.test :refer [deftest testing is]] - [futurama.core :refer [! async->> + [futurama.core :refer [! async->> async? async-for async-map async-reduce async-some async-every? async-prewalk async-postwalk @@ -10,7 +10,7 @@ [criterium.core :refer [report-result quick-benchmark with-progress-reporting]]) - (:import [java.util.concurrent CompletableFuture ExecutionException] + (:import [java.util.concurrent Executors CompletableFuture ExecutionException] [clojure.lang ExceptionInfo])) (def ^:dynamic *test-val1* nil) @@ -27,6 +27,18 @@ [a b] (* (+ a 100) b)) +(def test-pool + (delay + (Executors/newSingleThreadExecutor))) + +(deftest with-pool-macro-test + (testing "with-pool evals body" + (!" (is (= 1500