-
Notifications
You must be signed in to change notification settings - Fork 1
/
op.clj
56 lines (45 loc) · 1.5 KB
/
op.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
(ns test.carly.op
"Test operations and related functionality.")
(defprotocol TestOperation
"Protocol for a test operation on a system."
(apply-op
[operation system]
"Apply the operation to the system, returning a result value.")
(check
[operation model result]
"Validate an operation given the model state and the response from the
system being tested. May include `clojure.test/is` assertions, and should
return a boolean value indicating overall success or failure.")
(update-model
[operation model]
"Apply an update to the model based on the operation."))
(defn apply-ops!
"Apply a sequence of operations to a system, returning a vector of pairs of
the operations with their results."
[system ops]
(mapv
(fn [op]
(assoc
op ::result
(try
(apply-op op system)
(catch Throwable t
t))))
ops))
(defn run-ops!
"Applies a sequence of operations in a separate thread. Returns a promise for
the results of the application."
[latch system thread-id ops]
(future @latch (apply-ops! system ops)))
(defn run-threads!
"Run each of the given operation sequences in a separate thread. Returns a
vector of the operation results for each thread."
[system op-seqs]
(let [latch (promise)
threads (map (partial run-ops! latch system) (range) op-seqs)]
(dorun threads)
(deliver latch :start)
; TODO: timeout on deref?
(->> (map deref threads)
(map vector (range))
(into {}))))