Skip to content

Commit 32c3012

Browse files
committed
Refactor the temporal.testing.env
N.B. This is a breaking change for the temporal.testing.env namespace. There are use cases where we need to pass a temporal client in the :ctx, but this was impossible with the current "all in one" design of temporal.testing.env/create. Therefore, we split this up into three distinct operations: create/start/get-client. This now means that we get a chance to call create/get-client before invoking start, yielding the opportunity to build the worker :ctx as we require. Signed-off-by: Greg Haskins <[email protected]>
1 parent bd0f7a7 commit 32c3012

File tree

5 files changed

+40
-19
lines changed

5 files changed

+40
-19
lines changed

doc/testing.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ You can use the provided environment with a Clojure unit testing framework of yo
3131

3232
(deftest my-test
3333
(testing "Verifies that we can invoke our greeter workflow"
34-
(let [{:keys [client] :as env} (e/create {:task-queue task-queue})
35-
(c/create-workflow client greeter-workflow {:task-queue task-queue})]
36-
(c/start workflow {:name "Bob"})
34+
(let [env (e/create)
35+
client (e/get-client env)]
36+
(e/start env {:task-queue task-queue})
37+
(let [workflow (c/create-workflow client greeter-workflow {:task-queue task-queue})]
38+
(c/start workflow {:name "Bob"}))
3739
(is (= @(c/get-result workflow) "Hi, Bob")))))
3840
```
3941

40-
The primary difference between this test and a real-world application is the use of [temporal.testing.env/create](https://cljdoc.org/d/io.github.manetu/temporal-sdk/CURRENT/api/temporal.testing.env#create) function. This method simultaneously creates an instance of the in-memory Temporal service *and* a client already connected to this environment.
42+
The primary difference between this test and a real-world application is the use of [temporal.testing.env/create](https://cljdoc.org/d/io.github.manetu/temporal-sdk/CURRENT/api/temporal.testing.env#create) function. The features in the temporal.testing.env namespace allow you to create an instance of an in-memory Temporal service along with an associated Temporal worker and client connected to this instance.
4143

42-
Typical tests may opt to create the testing environment within a [fixture](https://clojuredocs.org/clojure.test/use-fixtures), but this is left as an exercise to the reader. The testing environment may be cleanly shutdown with [temporal.testing.env/stop](https://cljdoc.org/d/io.github.manetu/temporal-sdk/CURRENT/api/temporal.testing.env#stop).
44+
Typical tests may opt to create the testing environment within a [fixture](https://clojuredocs.org/clojure.test/use-fixtures), but this is left as an exercise to the reader.
45+
46+
The testing environment may be cleanly shutdown with [temporal.testing.env/stop](https://cljdoc.org/d/io.github.manetu/temporal-sdk/CURRENT/api/temporal.testing.env#stop).

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject io.github.manetu/temporal-sdk "0.10.5-SNAPSHOT"
1+
(defproject io.github.manetu/temporal-sdk "0.11.0-SNAPSHOT"
22
:description "A Temporal SDK for Clojure"
33
:url "https://github.com/manetu/temporal-clojure-sdk"
44
:license {:name "Apache License 2.0"

src/temporal/testing/env.clj

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,33 @@
88

99
(defn create
1010
"
11-
Creates a mock environment and an associated client connection for emulating the combination of a worker and a Temporal
12-
backend, suitable for unit testing.
11+
Creates a mock Temporal backend, suitable for unit testing.
12+
13+
A worker may be created with [[start]] and a client may be connected with [[get-client]]
14+
"
15+
[]
16+
(TestWorkflowEnvironment/newInstance))
17+
18+
(defn start
19+
"
20+
Starts a Temporal worker associated with the mock environment created with [[create]].
1321
1422
Arguments:
1523
1624
- `options`: See [[temporal.client.worker/worker-options]]
1725
1826
```clojure
19-
(let [{:keys [client] :as instance} (create {:task-queue ::my-queue :ctx {:some \"context\"}}]
27+
(let [env (create)]
28+
(start env {:task-queue ::my-queue :ctx {:some \"context\"}})
2029
;; create and invoke workflows
21-
(stop instance))
30+
(stop env))
2231
```
2332
"
24-
[{:keys [task-queue] :as options}]
25-
(let [env (TestWorkflowEnvironment/newInstance)
26-
worker (.newWorker env (u/namify task-queue))]
33+
[env {:keys [task-queue] :as options}]
34+
(let [worker (.newWorker env (u/namify task-queue))]
2735
(worker/init worker options)
2836
(.start env)
29-
{:env env :worker worker :client (.getWorkflowClient env)}))
37+
:ok))
3038

3139
(defn stop
3240
"
@@ -37,7 +45,7 @@ see [[synchronized-stop]].
3745
(stop instance)
3846
```
3947
"
40-
[{:keys [^TestWorkflowEnvironment env]}]
48+
[^TestWorkflowEnvironment env]
4149
(.shutdown env))
4250

4351
(defn synchronized-stop
@@ -49,5 +57,10 @@ see [[stop]]
4957
(synchronized-stop instance)
5058
```
5159
"
52-
[{:keys [^TestWorkflowEnvironment env]}]
60+
[^TestWorkflowEnvironment env]
5361
(.close env))
62+
63+
(defn get-client
64+
"Returns a client instance associated with the mock environment created by [[create]]"
65+
[env]
66+
(.getWorkflowClient env))

test/temporal/test/manual_dispatch.clj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@
4949
;; Fixtures
5050
;;-----------------------------------------------------------------------------
5151
(defn create-service []
52-
(let [{:keys [client] :as env} (e/create {:task-queue task-queue
53-
:dispatch {:workflows [explicitly-registered-workflow]}})] ;; note that we intentionally omit explicitly-skipped-workflow
52+
(let [env (e/create)
53+
client (e/get-client env)]
54+
(e/start env {:task-queue task-queue
55+
:dispatch {:workflows [explicitly-registered-workflow]}}) ;; note that we intentionally omit explicitly-skipped-workflow
5456
(swap! state assoc
5557
:env env
5658
:client client)))

test/temporal/test/utils.clj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
;; Fixtures
3030
;;-----------------------------------------------------------------------------
3131
(defn create-service []
32-
(let [{:keys [client] :as env} (e/create {:task-queue task-queue})]
32+
(let [env (e/create)
33+
client (e/get-client env)]
34+
(e/start env {:task-queue task-queue})
3335
(swap! state assoc
3436
:env env
3537
:client client)))

0 commit comments

Comments
 (0)