|
| 1 | +(ns intemporal.shutdown-test |
| 2 | + (:require [clojure.test :as t :refer [deftest is testing]] |
| 3 | + [intemporal.store :as store] |
| 4 | + [intemporal.workflow :as w] |
| 5 | + [intemporal.test-utils :as tu] |
| 6 | + [matcher-combinators.test :refer [match?]]) |
| 7 | + (:require [intemporal.macros :refer [stub-protocol defn-workflow]] |
| 8 | + [intemporal.test-utils :refer [with-result]])) |
| 9 | + |
| 10 | +(t/use-fixtures :once tu/with-trace-logging) |
| 11 | + |
| 12 | +(defprotocol MyActivities |
| 13 | + (foo [this a])) |
| 14 | + |
| 15 | +(defrecord MyActivitiesImpl [] |
| 16 | + MyActivities |
| 17 | + (foo [this a] |
| 18 | + (Thread/sleep 1000) :foo)) |
| 19 | + |
| 20 | +(defn-workflow my-workflow [k] |
| 21 | + (let [stub (stub-protocol MyActivities {}) |
| 22 | + prr (foo stub :pr)] |
| 23 | + prr)) |
| 24 | + |
| 25 | +;;;; test proper |
| 26 | + |
| 27 | +(deftest executor-shutdown-test |
| 28 | + (testing "failure: task validation fails" |
| 29 | + (let [mstore (store/make-store {}) |
| 30 | + executor (w/start-poller! mstore {:protocols {`MyActivities (->MyActivitiesImpl)} |
| 31 | + :polling-ms 10})] |
| 32 | + |
| 33 | + (testing "shutdown of ongoing workflow" |
| 34 | + ;; give it some time so the poller can pick it up but just once |
| 35 | + (future |
| 36 | + (Thread/sleep 500) |
| 37 | + (w/shutdown executor 0)) |
| 38 | + |
| 39 | + (with-result [res (w/with-env {:store mstore} |
| 40 | + (my-workflow :ok))] |
| 41 | + |
| 42 | + (is (instance? Exception res)) |
| 43 | + (is (= "Operation timed out." (ex-message res))) |
| 44 | + |
| 45 | + (testing "Workflow is not in failed state" |
| 46 | + (tu/print-tables mstore) |
| 47 | + |
| 48 | + (testing "workflow task" |
| 49 | + (let [[w1] (store/list-tasks mstore)] |
| 50 | + (is (match? {:type :workflow :sym 'intemporal.shutdown-test/my-workflow- :state :pending} w1)) |
| 51 | + |
| 52 | + (testing "workflow events" |
| 53 | + (let [[e1 e2 e3] (store/list-events mstore)] |
| 54 | + (is (match? {:type :intemporal.workflow/invoke :sym 'intemporal.shutdown-test/my-workflow-} e1)) |
| 55 | + (is (match? {:type :intemporal.protocol/invoke :sym 'intemporal.shutdown-test/foo} e2)) |
| 56 | + (is (nil? e3)))))) |
| 57 | + |
| 58 | + (testing "workflow resumes" |
| 59 | + (let [executor (w/start-poller! mstore {:protocols {`MyActivities (->MyActivitiesImpl)} |
| 60 | + :polling-ms 10})] |
| 61 | + (store/reenqueue-pending-tasks mstore (constantly nil)) |
| 62 | + (with-result [_ (Thread/sleep 2000)] |
| 63 | + |
| 64 | + (tu/print-tables mstore) |
| 65 | + |
| 66 | + (testing "workflow succeeded" |
| 67 | + (let [[w1] (store/list-tasks mstore)] |
| 68 | + (is (match? {:type :workflow :sym 'intemporal.shutdown-test/my-workflow- :state :success} w1)))) |
| 69 | + |
| 70 | + (w/shutdown executor 0)))))))))) |
0 commit comments