Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

idea: make recursive-gen interruptible #1152

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/malli/generator.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@
dschema (m/deref schema)]
(cond->> (generator dschema (assoc-in options [::rec-gen ref-id] scalar-ref-gen))
(realized? scalar-ref-gen) (gen/recursive-gen
#(generator dschema (assoc-in options [::rec-gen ref-id] %))))))))
#(do #?(:clj (when (Thread/interrupted) (throw (InterruptedException.))))
(generator dschema (assoc-in options [::rec-gen ref-id] %)))))))))

(defn -=>-gen [schema options]
(let [output-generator (generator (:output (m/-function-info schema)) options)]
Expand Down
16 changes: 16 additions & 0 deletions test/malli/generator_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1124,3 +1124,19 @@
(doseq [_ (range 100)
v (mg/sample [:seqable {:min 1} :any])]
(is (seq v))))

#?(:clj
(deftest interrupt-recursive-gen-test
(let [started (promise)
finished (promise)
f (future
(deliver started true)
(try (mg/generate [:schema {:registry {::cons [:maybe [:vector [:tuple pos-int? [:ref ::cons]]]]}}
[:vector [:ref ::cons]]]
{:seed 1
:size 10000})
(catch InterruptedException _ (deliver finished :interrupted))
(finally (deliver finished :not-interrupted))))]
@started
(future-cancel f)
(is (= :interrupted (deref finished 1000 :interrupted))))))
Loading