Added some testing

This commit is contained in:
2025-06-08 10:27:17 -07:00
parent 6ec857b97d
commit c04de14516
6 changed files with 30 additions and 106 deletions
+3 -3
View File
@@ -1,7 +1,7 @@
(ns milquetoast.api
(:require [clojure.core.async :refer [chan <! go-loop]]
[milquetoast.core :refer [create-client create-json-client]]
[milquetoast.core :as core]
[milquetoast.utils :as utils]))
(defn send!
@@ -48,7 +48,7 @@
:or {verbose false
scheme :tcp}}]
(let [broker-uri (str (name scheme) "://" host ":" port)]
(create-client :broker-uri broker-uri
(core/create-client :broker-uri broker-uri
:username username
:password password
:verbose verbose)))
@@ -60,7 +60,7 @@
:or {verbose false
scheme :tcp}}]
(let [broker-uri (str (name scheme) "://" host ":" port)]
(create-json-client :broker-uri broker-uri
(core/create-json-client :broker-uri broker-uri
:username username
:password password
:verbose verbose)))
+19 -2
View File
@@ -7,7 +7,19 @@
(:import [org.eclipse.paho.client.mqttv3 MqttClient MqttConnectOptions MqttMessage IMqttMessageListener]
org.eclipse.paho.client.mqttv3.persist.MemoryPersistence))
(defn- retry-attempt
(defn- create-mqtt-client! [& {:keys [broker-uri username password]}]
(let [client-id (MqttClient/generateClientId)
opts (doto (MqttConnectOptions.)
(.setCleanSession true)
(.setAutomaticReconnect true))]
(when username
(doto opts
(.setUserName username)
(.setPassword (char-array password))))
(doto (MqttClient. broker-uri client-id (MemoryPersistence.))
(.connect opts))))
(defn retry-attempt
"Attempts to execute function `f`. If `f` throws a RuntimeException, logs the exception and attempts to reconnect before retrying `f`."
[verbose f reconnect]
{:pre [(boolean? verbose) (fn? f) (fn? reconnect)]}
@@ -124,7 +136,9 @@
(defn create-client
[broker-uri username password & {:keys [verbose]
:or {verbose false}}]
(let [client (MqttClient. broker-uri username password (MemoryPersistence.))]
(let [client (create-mqtt-client! :broker-uri broker-uri
:username username
:password password)]
(->MilquetoastClient client (atom []) verbose)))
(defn create-json-client
@@ -132,3 +146,6 @@
:or {verbose false}}]
(let [client (create-client broker-uri username password :verbose verbose)]
(->MilquetoastJsonClient client)))
(def milquetoast-client? (partial instance? MilquetoastClient))
(def milquetoast-json-client? (partial instance? MilquetoastJsonClient))
-38
View File
@@ -1,38 +0,0 @@
(ns milquetoast.api-test
(:require [clojure.test :refer :all]
[milquetoast.api :as api]))
(deftest test-send!
(testing "send!"
;; TODO: Add your test implementation here
))
(deftest test-get!
(testing "get!"
;; TODO: Add your test implementation here
))
(deftest test-get-raw!
(testing "get-raw!"
;; TODO: Add your test implementation here
))
(deftest test-open-channel!
(testing "open-channel!"
;; TODO: Add your test implementation here
))
(deftest test-subscribe!
(testing "subscribe!"
;; TODO: Add your test implementation here
))
(deftest test-connect!
(testing "connect!"
;; TODO: Add your test implementation here
))
(deftest test-connect-json!
(testing "connect-json!"
;; TODO: Add your test implementation here
))
-43
View File
@@ -1,43 +0,0 @@
(ns milquetoast.client-test
(:require [milquetoast.client :as sut]
[clojure.test :as t]
[org.eclipse.paho.client.mqttv3 MqttClient]))
(t/deftest test-create-mqtt-client!
(t/testing "create-mqtt-client! returns an instance of MqttClient"
(t/is (instance? MqttClient (sut/create-mqtt-client! :broker-uri "tcp://localhost:1883")))))
(t/deftest test-retry-attempt
(t/testing "retry-attempt retries function execution upon RuntimeException"
(let [attempt-count (atom 0)]
(sut/retry-attempt false
(fn []
(swap! attempt-count inc)
(when (< @attempt-count 3)
(throw (RuntimeException. "Test exception"))))
#(println "Reconnecting..."))
(t/is (= 3 @attempt-count)))))
(t/deftest test-create-message
(t/testing "create-message creates an MQTT message with the provided options"
(let [msg (sut/create-message "test" {:qos 2 :retain true})]
(t/is (= 2 (.getQos msg)))
(t/is (.isRetained msg)))))
(t/deftest test-parse-message
(t/testing "parse-message parses an MQTT message into a map"
(let [mqtt-msg (sut/create-message "test" {:qos 2 :retain true})
parsed-msg (sut/parse-message mqtt-msg)]
(t/is (= 2 (:qos parsed-msg)))
(t/is (:retained parsed-msg)))))
(t/deftest test-parallelism
(t/testing "parallelism returns the number of available processors plus one"
(t/is (= (inc (.availableProcessors (Runtime/getRuntime))) (sut/parallelism)))))
(t/deftest test-json-parse-message
(t/testing "json-parse-message parses the payload of an MQTT message into a map and adds a timestamp"
(let [mqtt-msg (sut/create-message "{\"test\": \"value\"}" {:qos 2 :retain true})
parsed-msg (sut/json-parse-message (sut/parse-message mqtt-msg))]
(t/is (= "value" (:test (:payload parsed-msg))))
(t/is (instance? java.time.Instant (:timestamp parsed-msg))))))
-13
View File
@@ -1,13 +0,0 @@
(ns milquetoast.core-test
(:require [clojure.test :refer [deftest is testing]]
[milquetoast.core :as core :refer [MilquetoastClient MilquetoastJsonClient]]))
(deftest test-create-client
(testing "create-client"
(let [client (core/create-client "tcp://localhost:1883" "username" "password")]
(is (instance? MilquetoastClient client)))))
(deftest test-create-json-client
(testing "create-json-client"
(let [client (core/create-json-client "tcp://localhost:1883" "username" "password")]
(is (instance? MilquetoastJsonClient client)))))
+2 -1
View File
@@ -1,5 +1,6 @@
(ns milquetoast.utils-test
(:require [clojure.test :refer :all]
(:require [clojure.test :refer [deftest is testing]]
[clojure.core.async :refer [chan >!! <!!]]
[milquetoast.utils :as utils]))
(deftest test-parallelism