-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.clj
More file actions
435 lines (375 loc) · 19.4 KB
/
api.clj
File metadata and controls
435 lines (375 loc) · 19.4 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
(ns sys.test.api
(:require
[clojure.test :refer [deftest testing is] :as t]
[malli.core :as m]
[malli.error :as me]
[sys.api :as sys]
[sys.internals :as i]))
(defmethod t/assert-expr 'valid? [msg form]
;; form is (valid? schema value)
(let [[_ schema value] form]
`(let [schema# ~schema
value# ~value
explain# (m/explain schema# value#)
ok?# (nil? (:errors explain#))]
(t/do-report
{:type (if ok?# :pass :fail)
:message ~msg
:expected schema#
:actual (if ok?# value# (me/humanize explain#))})
ok?#)))
(defn valid? [& _]) ;; stubbing out above to satisfy clj-kondo
(defn clear!
[]
(println "CLEAR!")
(doseq [system-id (keys @sys/*systems)]
(sys/stop! system-id))
(reset! sys/*systems {}))
(deftest e2e
(testing "set!"
(testing
"(internal) results in valid systems"
(clear!)
(sys/set! ::test #{#:sys.component
{:id :component-1
:expects #{}
:provides #{:a}
:start (fn [_]
{:a 1})}
#:sys.component
{:id :component-2
:expects {:a :int}
:provides {:b :int}
:start (fn [{:keys [a]}]
{:b (+ a 1)})}})
(is (valid? i/SystemObject @(get @sys/*systems ::test))))
(testing "throws exception when component definitions are invalid"
(is (thrown-with-msg?
java.lang.AssertionError
#"m/validate"
(sys/set! ::test #{{}}))))
(testing "throws exception when dependencies are not met"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"not provided: #\{:b\}"
(sys/set! ::test #{{:sys.component/id :component-1
:sys.component/provides #{:a}}
{:sys.component/id :component-2
:sys.component/expects #{:b}}}))))
(testing "throws exception when there are circular dependencies"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Cycle Detected"
(sys/set! ::test #{{:sys.component/id :component-1
:sys.component/expects #{:b}
:sys.component/provides #{:a}}
{:sys.component/id :component-2
:sys.component/expects #{:a}
:sys.component/provides #{:b}}}))))
(testing "throws exception when multiple components provide same key"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Multiple components provide the same key: \{:a .*\}"
(sys/set! ::test #{{:sys.component/id :component-1
:sys.component/provides #{:a}}
{:sys.component/id :component-2
:sys.component/provides {:a :int}}})))))
(testing "start!"
(let [starts (atom [])
start-args (atom {})
get-contexts (atom {})]
(clear!)
(sys/set! ::test #{{:sys.component/id :component-1
:sys.component/expects #{}
:sys.component/provides #{:a}
:sys.component/start (fn [args]
(swap! starts conj :component-1)
(swap! start-args assoc :component-1 args)
(swap! get-contexts assoc :component-1 (sys/context ::test))
{:a 1})}
{:sys.component/id :component-2
:sys.component/expects #{:a}
:sys.component/provides #{:b}
:sys.component/start (fn [{:keys [a] :as args}]
(swap! starts conj :component-2)
(swap! start-args assoc :component-2 args)
(swap! get-contexts assoc :component-2 (sys/context ::test))
{:b (+ a 1)
;; return an extra key that is not provided
;; (and thus not added to context)
:x 1})}
{:sys.component/id :component-3
:sys.component/expects #{:b}
:sys.component/start (fn [{:keys [_b] :as args}]
(swap! starts conj :component-3)
(swap! start-args assoc :component-3 args)
(swap! get-contexts assoc :component-3 (sys/context ::test))
;; component that provides nothing has the
;; output of its start function ignored
[nil])}})
(sys/start! ::test)
(testing "calls start on all components, in dependency order"
(is (= [:component-1 :component-2 :component-3] @starts)))
(testing "(internal) systems remains valid"
(is (valid? i/SystemObject @(get @sys/*systems ::test))))
(testing "each component receives the values it expects (and only the ones it expects)"
(is (= {:component-1 {}
:component-2 {:a 1}
:component-3 {:b 2}}
@start-args)))
(testing "context is updated after each start"
(is (= {:component-1 {}
:component-2 {:a 1}
:component-3 {:a 1 :b 2}}
@get-contexts)))
(testing "resulting system has all provided keys (and only provided keys)"
(is (= 1 (sys/get ::test :a)))
(is (= 2 (sys/get ::test :b)))
(is (= {:a 1 :b 2} (sys/context ::test))))
(testing "repeating start on started system does not call start functions again"
(sys/start! ::test)
(is (= [:component-1 :component-2 :component-3] @starts))
(testing "(internal) systems remains valid"
(is (valid? i/SystemObject @(get @sys/*systems ::test))))))
(testing "when a component does not provide what it declared (set), results in system with exception"
(clear!)
(sys/set! ::test #{{:sys.component/id :component-1
:sys.component/provides #{:a}
:sys.component/start (fn [_]
{:b 1})}})
(sys/start! ::test)
(is (= (-> sys/*systems
deref
::test
deref
::i/exception
ex-message)
"Component with id :component-1 did not provide values as declared: {:a [\"missing required key\"]}"))
(testing "(internal) systems remains valid"
(is (valid? i/SystemObject @(get @sys/*systems ::test)))))
(testing "when a component does not provide what it declared (malli spec), results in system with exception"
(clear!)
(sys/set! ::test #{{:sys.component/id :component-1
:sys.component/provides {:a [:vector :int]}
:sys.component/start (fn [_]
{:a 1})}})
(sys/start! ::test)
(is (= (-> sys/*systems
deref
::test
deref
::i/exception
ex-message)
"Component with id :component-1 did not provide values as declared: {:a [\"invalid type\"]}"))
(testing "(internal) systems remains valid"
(is (valid? i/SystemObject @(get @sys/*systems ::test)))))
(testing "when a component fails to start, does not start remaining components"
(let [starts (atom [])
stops (atom [])
component-1 {:sys.component/id :component-1
:sys.component/expects #{}
:sys.component/provides #{:a}
:sys.component/start (fn [_]
(swap! starts conj :component-1)
{:a 1})
:sys.component/stop (fn [_]
(swap! stops conj :component-1))}
component-2-broken {:sys.component/id :component-2
:sys.component/expects #{:a}
:sys.component/provides #{:b}
:sys.component/start (fn [{:keys [a]}]
(swap! starts conj :component-2)
(throw (ex-info "component failed to start" {}))
{:b (+ a 1)})
:sys.component/stop (fn [_]
(swap! stops conj :component-2))}
component-2-fixed {:sys.component/id :component-2
:sys.component/expects #{:a}
:sys.component/provides #{:b}
:sys.component/start (fn [{:keys [a]}]
(swap! starts conj :component-2)
{:b (+ a 1)})
:sys.component/stop (fn [_]
(swap! stops conj :component-2))}
component-3 {:sys.component/id :component-3
:sys.component/expects #{:b}
:sys.component/provides #{:c}
:sys.component/start (fn [{:keys [b]}]
(swap! starts conj :component-3)
{:c (+ b 1)})
:sys.component/stop (fn [_]
(swap! stops conj :component-3))}]
(clear!)
(sys/set! ::test [component-1 component-2-broken component-3])
(sys/start! ::test)
(is (= [:component-1 :component-2] @starts))
(testing "(internal) systems remains valid"
(is (valid? i/SystemObject @(get @sys/*systems ::test))))
(testing "resulting system has all defined keys"
(is (= 1 (sys/get ::test :a)))
(is (= {:a 1} (sys/context ::test))))
(testing "calling set! to replace a broken system..."
(sys/set! ::test [component-1 component-2-fixed component-3])
(testing "stops components"
(is (= [:component-1] @stops)))
(testing "starts again"
(is (= [:component-1 :component-2 ;; before fixing
:component-1 :component-2 :component-3] @starts)))
(testing "resulting system has all defined keys"
(is (= 1 (sys/get ::test :a)))
(is (= {:a 1 :b 2 :c 3} (sys/context ::test))))
(testing "(internal) systems remains valid"
(is (valid? i/SystemObject @(get @sys/*systems ::test))))))))
(testing "stop!"
(let [stops (atom [])
stop-args (atom {})
get-contexts (atom {})]
(clear!)
(sys/set! ::test #{{:sys.component/id :component-1
:sys.component/expects #{}
:sys.component/provides #{:a}
:sys.component/start (fn [_]
{:a 1})
:sys.component/stop (fn [args]
(swap! stops conj :component-1)
(swap! stop-args assoc :component-1 args)
(swap! get-contexts assoc :component-1 (sys/context ::test)))}
{:sys.component/id :component-2
:sys.component/expects #{:a}
:sys.component/provides #{:b}
;; testing not having a stop
:sys.component/start (fn [{:keys [a]}]
{:b (+ a 1)})}
{:sys.component/id :component-3
:sys.component/expects #{:b}
:sys.component/provides #{:c}
:sys.component/start (fn [{:keys [b]}]
{:c (+ b 1)})
:sys.component/stop (fn [args]
(swap! stops conj :component-3)
(swap! stop-args assoc :component-3 args)
(swap! get-contexts assoc :component-3 (sys/context ::test)))}})
(sys/start! ::test)
(sys/stop! ::test)
(testing "stops components in reverse order"
(is (= [:component-3 :component-1] ;; component-2 doesn't have a stop fn
@stops)))
(testing "(internal) systems remains valid"
(is (valid? i/SystemObject @(get @sys/*systems ::test))))
(testing "each component receives what it provided"
(is (= {:component-1 {:a 1}
:component-3 {:c 3}}
@stop-args)))
(testing "context is updated after each stop"
(is (= {:component-1 {:a 1}
:component-3 {:a 1 :b 2 :c 3}}
@get-contexts)))
(testing "removes provided values from context"
(is (= {} (sys/context ::test)))))
(testing "removes provided keys from context even when component has no stop fn"
(clear!)
(sys/set! ::test #{{:sys.component/id :component-1
:sys.component/provides #{:a}
:sys.component/start (fn [_] {:a 1})}})
(sys/start! ::test)
(is (= {:a 1} (sys/context ::test)))
(sys/stop! ::test)
(is (= {} (sys/context ::test))))
(testing "when given broken system only stops active components"
(let [stops (atom [])]
(clear!)
(sys/set! ::test #{{:sys.component/id :component-1
:sys.component/expects #{}
:sys.component/provides #{:a}
:sys.component/start (fn [_]
{:a 1})
:sys.component/stop (fn [_]
(swap! stops conj :component-1))}
{:sys.component/id :component-2
:sys.component/expects #{:a}
:sys.component/provides #{:b}
:sys.component/start (fn [{:keys [a]}]
{:b (+ a 1)})
:sys.component/stop (fn [_]
(swap! stops conj :component-2))}
{:sys.component/id :component-3
:sys.component/expects #{:b}
:sys.component/provides #{:c}
:sys.component/start (fn [{:keys [b]}]
(throw (ex-info "component failed to start" {}))
{:c (+ b 1)})
:sys.component/stop (fn [_]
(swap! stops conj :component-3))}})
(sys/start! ::test)
(sys/stop! ::test)
(testing "stops components in reverse order"
(is (= [:component-2 :component-1]
@stops)))
(testing "(internal) systems remains valid"
(is (valid? i/SystemObject @(get @sys/*systems ::test)))))))
(testing "nested systems"
(clear!)
(sys/set! ::child #{#:sys.component
{:id :child-1
:expects #{}
:provides #{:a}
:start (fn [_]
{:a 1})}
#:sys.component
{:id :child-2
:expects {:a :int}
:provides {:b :int}
:start (fn [{:keys [a]}]
{:b (+ a 1)})}})
(sys/set! ::parent #{#:sys.component
{:id :parent-1
:expects #{}
:provides #{:a}
:start (fn [_]
(sys/start! ::child)
{:a 1})
:stop (fn [_]
(sys/stop! ::child))}
#:sys.component
{:id :parent-2
:expects {:a :int}
:provides {:b :int}
:start (fn [{:keys [a]}]
{:b (+ a 1)})}})
(sys/start! ::parent)
(testing "starting parent also starts child"
(is (= {:a 1 :b 2} (sys/context ::child))))
(testing "parent context is correct"
(is (= {:a 1 :b 2} (sys/context ::parent))))
(testing "(internal) systems remains valid after start"
(is (valid? i/SystemObject @(get @sys/*systems ::parent))))
(testing "set! on running parent stops child and restarts it"
(sys/set! ::parent #{#:sys.component
{:id :parent-1
:expects #{}
:provides #{:a}
:start (fn [_]
(sys/start! ::child)
{:a 10})
:stop (fn [_]
(sys/stop! ::child))}
#:sys.component
{:id :parent-2
:expects {:a :int}
:provides {:b :int}
:start (fn [{:keys [a]}]
{:b (+ a 1)})}})
(testing "child is restarted"
(is (= {:a 1 :b 2} (sys/context ::child))))
(testing "parent context reflects new components"
(is (= {:a 10 :b 11} (sys/context ::parent))))
(testing "(internal) systems remains valid after set!"
(is (valid? i/SystemObject @(get @sys/*systems ::parent)))))
(sys/stop! ::parent)
(testing "stopping parent also stops child"
(is (= {} (sys/context ::child))))
(testing "parent context is cleared after stop"
(is (= {} (sys/context ::parent))))
(testing "(internal) systems remains valid after stop"
(is (valid? i/SystemObject @(get @sys/*systems ::parent))))))
(clojure.test/run-tests)