Skip to content

Commit

Permalink
feat: add with-pool macro
Browse files Browse the repository at this point in the history
  • Loading branch information
k13gomez committed Jan 7, 2024
1 parent 9d47b7d commit a678366
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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?`
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<groupId>com.github.k13labs</groupId>
<artifactId>futurama</artifactId>
<name>futurama</name>
<version>0.3.9-SNAPSHOT</version>
<version>0.3.9</version>
<scm>
<tag>0.3.9-SNAPSHOT</tag>
<tag>0.3.9</tag>
<url>https://github.com/k13labs/futurama</url>
<connection>scm:git:git://github.com/k13labs/futurama.git</connection>
<developerConnection>scm:git:ssh://git@github.com/k13labs/futurama.git</developerConnection>
Expand Down
6 changes: 6 additions & 0 deletions src/futurama/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
18 changes: 15 additions & 3 deletions test/futurama/core_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns futurama.core-test
(:require [clojure.test :refer [deftest testing is]]
[futurama.core :refer [!<!! !<! !<!* async?
async async-> async->>
[futurama.core :refer [!<!! !<! !<!* with-pool
async async-> async->> async?
async-for async-map async-reduce
async-some async-every?
async-prewalk async-postwalk
Expand All @@ -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)
Expand All @@ -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"
(!<!!
(with-pool @test-pool
(async
(is (= 100
(!<! (CompletableFuture/completedFuture 100)))))))))

(deftest thread-first-macro-tests
(testing "can thread first async->"
(is (= 1500
Expand Down

0 comments on commit a678366

Please sign in to comment.