mirror of
https://github.com/vale981/stream
synced 2025-03-05 10:01:39 -05:00
testing serialization
This commit is contained in:
parent
9cc0717601
commit
aad0b0c214
5 changed files with 102 additions and 55 deletions
|
@ -6,6 +6,6 @@ script:
|
||||||
- sudo apt install dbus-user-session curl ffmpeg
|
- sudo apt install dbus-user-session curl ffmpeg
|
||||||
- systemctl --user start dbus
|
- systemctl --user start dbus
|
||||||
- export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus
|
- export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus
|
||||||
- lein cloverage --coveralls
|
- lein coverage --coveralls
|
||||||
- curl -F "json_file=@target/coverage/coveralls.json" "https://coveralls.io/api/v1/jobs"
|
- curl -F "json_file=@target/coverage/coveralls.json" "https://coveralls.io/api/v1/jobs"
|
||||||
- lein uberjar
|
- lein uberjar
|
||||||
|
|
|
@ -14,7 +14,8 @@
|
||||||
[com.taoensso/encore "2.122.0"]
|
[com.taoensso/encore "2.122.0"]
|
||||||
[com.outpace/config "0.13.2"]]
|
[com.outpace/config "0.13.2"]]
|
||||||
:main ^:skip-aot stream.core
|
:main ^:skip-aot stream.core
|
||||||
:aliases {"config" ["run" "-m" "outpace.config.generate"]}
|
:aliases {"config" ["run" "-m" "outpace.config.generate"]
|
||||||
|
"coverage" ["with-profile" "test" "cloverage"]}
|
||||||
:profiles {:test {:jvm-opts ["-Dconfig.edn=resources/test/config.edn"]}
|
:profiles {:test {:jvm-opts ["-Dconfig.edn=resources/test/config.edn"]}
|
||||||
:prod {:jvm-opts ["-Dconfig.edn=config.edn"]}
|
:prod {:jvm-opts ["-Dconfig.edn=config.edn"]}
|
||||||
:uberjar {:aot :all}}
|
:uberjar {:aot :all}}
|
||||||
|
|
3
resources/test/config.edn
Normal file
3
resources/test/config.edn
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
stream.commander.api/processdb-directory "./resources/test/processes"
|
||||||
|
}
|
|
@ -28,6 +28,16 @@
|
||||||
(defrecord process
|
(defrecord process
|
||||||
[id process-name unit-name monitor supervisor ffmpeg-config problems])
|
[id process-name unit-name monitor supervisor ffmpeg-config problems])
|
||||||
|
|
||||||
|
(defn process=
|
||||||
|
"Tests `proc-1` and `proc-2` for equality."
|
||||||
|
[proc-1 proc-2]
|
||||||
|
(loop [[key & keys] [:id :process-name :config]]
|
||||||
|
(if key
|
||||||
|
(if (= (get proc-1 key) (get proc-2 key))
|
||||||
|
(recur keys)
|
||||||
|
false)
|
||||||
|
true)))
|
||||||
|
|
||||||
(def ^:private processes (ref {}))
|
(def ^:private processes (ref {}))
|
||||||
(def ^:private dbus-monitor (chan))
|
(def ^:private dbus-monitor (chan))
|
||||||
(def ^:private master-monitor (chan))
|
(def ^:private master-monitor (chan))
|
||||||
|
@ -272,7 +282,7 @@
|
||||||
"Deletes a process from the process map, stops it and deletes the unit file.
|
"Deletes a process from the process map, stops it and deletes the unit file.
|
||||||
Returns `true` on success, `false` otherwise."
|
Returns `true` on success, `false` otherwise."
|
||||||
[proc]
|
[proc]
|
||||||
(debug "Removing process with ID:" (:id proc))
|
(info "Removing process with ID:" (:process-name proc))
|
||||||
(let [{:keys [unit-name monitor]} proc
|
(let [{:keys [unit-name monitor]} proc
|
||||||
[monitor close] monitor]
|
[monitor close] monitor]
|
||||||
(close! (:supervisor proc))
|
(close! (:supervisor proc))
|
||||||
|
@ -294,7 +304,7 @@
|
||||||
(if-let [proc (get-process! id)]
|
(if-let [proc (get-process! id)]
|
||||||
(do (info "Replacing process with ID:" id)
|
(do (info "Replacing process with ID:" id)
|
||||||
(delete-process! proc))
|
(delete-process! proc))
|
||||||
(info "Creating process with ID:" id))
|
(info "Creating process " process-name))
|
||||||
(let [ffmpeg-config (merge default-ffmpeg-config ffmpeg-config)
|
(let [ffmpeg-config (merge default-ffmpeg-config ffmpeg-config)
|
||||||
unit-name (str (sanitize-process-name process-name)
|
unit-name (str (sanitize-process-name process-name)
|
||||||
"-" id)
|
"-" id)
|
||||||
|
@ -307,8 +317,7 @@
|
||||||
process (->process id process-name unit-name monitor supervisor ffmpeg-config #{})]
|
process (->process id process-name unit-name monitor supervisor ffmpeg-config #{})]
|
||||||
(dosync
|
(dosync
|
||||||
(commute processes assoc id process))
|
(commute processes assoc id process))
|
||||||
(go
|
(save-process! process)
|
||||||
(save-process! process))
|
|
||||||
process))
|
process))
|
||||||
([process-name ffmpeg-config]
|
([process-name ffmpeg-config]
|
||||||
(create-process! process-name ffmpeg-config (generate-process-id))))
|
(create-process! process-name ffmpeg-config (generate-process-id))))
|
||||||
|
@ -387,9 +396,10 @@
|
||||||
:version 1}))
|
:version 1}))
|
||||||
|
|
||||||
(defn edn->process!
|
(defn edn->process!
|
||||||
"Creates a process from its edn serialized version."
|
"Creates a process from its `edn` serialized version."
|
||||||
[{:keys [name config id]}]
|
[edn]
|
||||||
(create-process! name config id))
|
(let [{:keys [name config id]} (clojure.edn/read-string edn)]
|
||||||
|
(create-process! name config id)))
|
||||||
|
|
||||||
(defn processdb-filename
|
(defn processdb-filename
|
||||||
"Computes the filename of the serialization file for the given process
|
"Computes the filename of the serialization file for the given process
|
||||||
|
@ -418,23 +428,18 @@
|
||||||
true
|
true
|
||||||
(catch Object _
|
(catch Object _
|
||||||
(error (:throwable &throw-context) "could not write process file")
|
(error (:throwable &throw-context) "could not write process file")
|
||||||
false))))
|
(throw+)))))
|
||||||
|
|
||||||
;; TODO: Global warnings
|
;; TODO: Global warnings
|
||||||
(defn load-process!
|
(defn load-process!
|
||||||
"Loads a process with the `id` from the processdb directory.
|
"Loads a process with the `id` from the processdb directory.
|
||||||
Returns `false` if the db file is not found or can't be read and
|
Returns `nil` if the db file is not found or can't be read and
|
||||||
the loaded process otherwise."
|
the loaded process otherwise."
|
||||||
[id]
|
[id]
|
||||||
(if-let [proc-data
|
(if-let [proc-data
|
||||||
(try+
|
(slurp (processdb-filename id))]
|
||||||
(clojure.edn/read-string
|
|
||||||
(slurp (processdb-filename id)))
|
|
||||||
(catch Object _
|
|
||||||
(error (:throwable &throw-context) "could not read process file")
|
|
||||||
false))]
|
|
||||||
(edn->process! proc-data)
|
(edn->process! proc-data)
|
||||||
false))
|
nil))
|
||||||
|
|
||||||
(defn load-processes!
|
(defn load-processes!
|
||||||
"Loads the serialized processes from the processdb directory by
|
"Loads the serialized processes from the processdb directory by
|
||||||
|
@ -442,30 +447,17 @@
|
||||||
[]
|
[]
|
||||||
(reduce (fn [procs file]
|
(reduce (fn [procs file]
|
||||||
(if-let [id (second (re-matches #"(.*)\.edn" file))]
|
(if-let [id (second (re-matches #"(.*)\.edn" file))]
|
||||||
(if-let [proc (load-process! id)]
|
(try+ (conj procs (load-process! id))
|
||||||
(conj procs proc)
|
(catch Object _
|
||||||
)
|
procs))
|
||||||
procs))
|
procs))
|
||||||
[] (.list (File. processdb-directory))))
|
[] (.list (File. processdb-directory))))
|
||||||
|
|
||||||
|
|
||||||
(defn save-processes!
|
|
||||||
"Saves the process registry to files via [[save-process!]]"
|
|
||||||
[]
|
|
||||||
(info "saving processes")
|
|
||||||
(doseq [[_ proc] @processes]
|
|
||||||
(save-process! proc)))
|
|
||||||
|
|
||||||
(defn delete-processdb-file!
|
(defn delete-processdb-file!
|
||||||
"Deletes the processdb file of a process.
|
"Deletes the processdb file of a process.
|
||||||
Returns `false` if the file is not found."
|
Returns `false` if the file is not found."
|
||||||
[id]
|
[id]
|
||||||
(try+
|
(io/delete-file (processdb-filename id)))
|
||||||
(io/delete-file (processdb-filename id))
|
|
||||||
true
|
|
||||||
(catch Object _
|
|
||||||
(error (:throwable &throw-context) "could not delete process file")
|
|
||||||
false)))
|
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
; Init ;
|
; Init ;
|
||||||
|
|
|
@ -252,4 +252,55 @@
|
||||||
|
|
||||||
(testing "deleting all processes"
|
(testing "deleting all processes"
|
||||||
(api/delete-all-processes!)
|
(api/delete-all-processes!)
|
||||||
(is (= 0 (count @@#'api/processes))))))
|
(is (= 0 (count @@#'api/processes))))
|
||||||
|
|
||||||
|
(testing "serialize and load process"
|
||||||
|
(let [proc (api/create-process! "test" config)
|
||||||
|
id (:id proc)
|
||||||
|
saved (api/save-process! proc)
|
||||||
|
loaded (api/load-process! id)
|
||||||
|
edn (api/process->edn proc)
|
||||||
|
fromedn (api/edn->process! edn)]
|
||||||
|
(is (api/process= proc fromedn))
|
||||||
|
(is saved)
|
||||||
|
(is (api/process= proc loaded))
|
||||||
|
(api/delete-all-processes!)))
|
||||||
|
|
||||||
|
(testing "saving a process twice"
|
||||||
|
(let [proc (api/create-process! "test" config)]
|
||||||
|
(api/save-process! proc)
|
||||||
|
(api/save-process! proc)
|
||||||
|
(api/delete-all-processes!)))
|
||||||
|
|
||||||
|
(testing "loading a nonexistent process"
|
||||||
|
(try+
|
||||||
|
(api/load-process! "nonone")
|
||||||
|
(is false)
|
||||||
|
(catch Object _
|
||||||
|
(is true))))
|
||||||
|
|
||||||
|
(testing "deleting a nonexistent process"
|
||||||
|
(try+
|
||||||
|
(api/delete-processdb-file! "nonone")
|
||||||
|
(is false)
|
||||||
|
(catch Object _
|
||||||
|
(is true))))
|
||||||
|
|
||||||
|
(testing "automatic saving and loading"
|
||||||
|
(let [procs [(api/create-process!
|
||||||
|
"tester" config)
|
||||||
|
(api/create-process!
|
||||||
|
"tester2" config)]]
|
||||||
|
;; lets put in a random broken edn file-seq
|
||||||
|
(spit (api/processdb-filename "distract") "bla")
|
||||||
|
(let [loaded (api/load-processes!)]
|
||||||
|
(is (= (count loaded) 2))
|
||||||
|
(is (reduce (fn [same proc]
|
||||||
|
(if same
|
||||||
|
(some #(api/process= proc %) procs)
|
||||||
|
false)) true
|
||||||
|
(api/load-processes!)))))
|
||||||
|
(is (= 2 (count @@#'api/processes)))
|
||||||
|
(api/delete-all-processes!)
|
||||||
|
(clojure.java.io/delete-file (api/processdb-filename "distract"))
|
||||||
|
(is (= "" (reduce str (.list (clojure.java.io/file api/processdb-directory))))))))
|
||||||
|
|
Loading…
Add table
Reference in a new issue