mirror of
https://github.com/alda-lang/alda.git
synced 2025-12-12 20:36:27 +01:00
rm test scripts
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
#!/usr/bin/env boot
|
||||
|
||||
(set-env! :dependencies '[[org.zeromq/jeromq "0.3.5"]])
|
||||
|
||||
(import '[org.zeromq ZMQ ZContext])
|
||||
|
||||
(defn print-usage
|
||||
[]
|
||||
(println "Example usage:")
|
||||
(println " $ boot scripts/jeromq-sanity-check-server-ctx.boot 12345")
|
||||
(println)
|
||||
(println " # in another terminal:")
|
||||
(println " $ boot scripts/jeromq-sanity-check-client-ctx.boot 12345"))
|
||||
|
||||
(defn hello-world-test
|
||||
[socket]
|
||||
(println "Hello world client/server example:")
|
||||
(dotimes [n 5]
|
||||
(let [req (format "Hello #%s from client" (inc n))]
|
||||
(println "Sending request:" req)
|
||||
(.send socket req))
|
||||
(let [res (.recv socket)]
|
||||
(println "Received response" (String. res)))))
|
||||
|
||||
(defn run-tests
|
||||
[port ctx-desc ctx]
|
||||
(println)
|
||||
(println (format "Testing with %s..." ctx-desc))
|
||||
(println)
|
||||
|
||||
(with-open [socket (doto (.socket ctx ZMQ/REQ)
|
||||
(.connect (format "tcp://*:%s" port)))]
|
||||
(hello-world-test socket))
|
||||
|
||||
(println)
|
||||
(print "Destroying context... ")
|
||||
(flush)
|
||||
(.term ctx)
|
||||
(println "done."))
|
||||
|
||||
(defn -main
|
||||
([]
|
||||
(println "No port specified.")
|
||||
(println)
|
||||
(print-usage)
|
||||
(System/exit 1))
|
||||
([port]
|
||||
(println (format "Using port %s for tests." port))
|
||||
(println
|
||||
(format "(In another terminal, run the server script first on port %s.)"
|
||||
port))
|
||||
|
||||
(run-tests port "Ctx (ZMQ.createContext(1))" (ZMQ/context 1))
|
||||
|
||||
(println)
|
||||
(println "Done.")))
|
||||
@@ -1,56 +0,0 @@
|
||||
#!/usr/bin/env boot
|
||||
|
||||
(set-env! :dependencies '[[org.zeromq/jeromq "0.3.5"]])
|
||||
|
||||
(import '[org.zeromq ZMQ ZContext])
|
||||
|
||||
(defn print-usage
|
||||
[]
|
||||
(println "Example usage:")
|
||||
(println " $ boot scripts/jeromq-sanity-check-server-zcontext.boot 12345")
|
||||
(println)
|
||||
(println " # in another terminal:")
|
||||
(println " $ boot scripts/jeromq-sanity-check-client-zcontext.boot 12345"))
|
||||
|
||||
(defn hello-world-test
|
||||
[socket]
|
||||
(println "Hello world client/server example:")
|
||||
(dotimes [n 5]
|
||||
(let [req (format "Hello #%s from client" (inc n))]
|
||||
(println "Sending request:" req)
|
||||
(.send socket req))
|
||||
(let [res (.recv socket)]
|
||||
(println "Received response" (String. res)))))
|
||||
|
||||
(defn run-tests
|
||||
[port ctx-desc ctx]
|
||||
(println)
|
||||
(println (format "Testing with %s..." ctx-desc))
|
||||
(println)
|
||||
|
||||
(with-open [socket (doto (.createSocket ctx ZMQ/REQ)
|
||||
(.connect (format "tcp://*:%s" port)))]
|
||||
(hello-world-test socket))
|
||||
|
||||
(println)
|
||||
(print "Destroying context... ")
|
||||
(flush)
|
||||
(.destroy ctx)
|
||||
(println "done."))
|
||||
|
||||
(defn -main
|
||||
([]
|
||||
(println "No port specified.")
|
||||
(println)
|
||||
(print-usage)
|
||||
(System/exit 1))
|
||||
([port]
|
||||
(println (format "Using port %s for tests." port))
|
||||
(println
|
||||
(format "(In another terminal, run the server script first on port %s.)"
|
||||
port))
|
||||
|
||||
(run-tests port "ZContext" (ZContext. 1))
|
||||
|
||||
(println)
|
||||
(println "Done.")))
|
||||
@@ -1,56 +0,0 @@
|
||||
#!/usr/bin/env boot
|
||||
|
||||
(set-env! :dependencies '[[org.zeromq/jeromq "0.3.5"]])
|
||||
|
||||
(import '[org.zeromq ZMQ ZContext])
|
||||
|
||||
(defn print-usage
|
||||
[]
|
||||
(println "Example usage:")
|
||||
(println " $ boot scripts/jeromq-sanity-check-server-ctx.boot 12345")
|
||||
(println)
|
||||
(println " # in another terminal:")
|
||||
(println " $ boot scripts/jeromq-sanity-check-client-ctx.boot 12345"))
|
||||
|
||||
(defn hello-world-test
|
||||
[socket]
|
||||
(println "Hello world client/server example:")
|
||||
(dotimes [n 5]
|
||||
(let [req (.recv socket)
|
||||
res (format "Hello #%s from server" (inc n))]
|
||||
(println "Received request:" (String. req))
|
||||
(Thread/sleep 1000) ; simulate doing work
|
||||
(println "Sending response:" res)
|
||||
(.send socket res))))
|
||||
|
||||
(defn run-tests
|
||||
[port ctx-desc ctx]
|
||||
(println)
|
||||
(println (format "Testing with %s..." ctx-desc))
|
||||
(println)
|
||||
|
||||
(with-open [socket (doto (.socket ctx ZMQ/REP)
|
||||
(.bind (format "tcp://*:%s" port)))]
|
||||
(hello-world-test socket))
|
||||
|
||||
(println)
|
||||
(print "Destroying context... ")
|
||||
(flush)
|
||||
(.term ctx)
|
||||
(println "done."))
|
||||
|
||||
(defn -main
|
||||
([]
|
||||
(println "No port specified.")
|
||||
(println)
|
||||
(print-usage)
|
||||
(System/exit 1))
|
||||
([port]
|
||||
(println (format "Using port %s for tests." port))
|
||||
(println
|
||||
(format "(In another terminal, run the client script on port %s.)" port))
|
||||
|
||||
(run-tests port "Ctx (ZMQ.createContext(1))" (ZMQ/context 1))
|
||||
|
||||
(println)
|
||||
(println "Done.")))
|
||||
@@ -1,56 +0,0 @@
|
||||
#!/usr/bin/env boot
|
||||
|
||||
(set-env! :dependencies '[[org.zeromq/jeromq "0.3.5"]])
|
||||
|
||||
(import '[org.zeromq ZMQ ZContext])
|
||||
|
||||
(defn print-usage
|
||||
[]
|
||||
(println "Example usage:")
|
||||
(println " $ boot scripts/jeromq-sanity-check-server-zcontext.boot 12345")
|
||||
(println)
|
||||
(println " # in another terminal:")
|
||||
(println " $ boot scripts/jeromq-sanity-check-client-zcontext.boot 12345"))
|
||||
|
||||
(defn hello-world-test
|
||||
[socket]
|
||||
(println "Hello world client/server example:")
|
||||
(dotimes [n 5]
|
||||
(let [req (.recv socket)
|
||||
res (format "Hello #%s from server" (inc n))]
|
||||
(println "Received request:" (String. req))
|
||||
(Thread/sleep 1000) ; simulate doing work
|
||||
(println "Sending response:" res)
|
||||
(.send socket res))))
|
||||
|
||||
(defn run-tests
|
||||
[port ctx-desc ctx]
|
||||
(println)
|
||||
(println (format "Testing with %s..." ctx-desc))
|
||||
(println)
|
||||
|
||||
(with-open [socket (doto (.createSocket ctx ZMQ/REP)
|
||||
(.bind (format "tcp://*:%s" port)))]
|
||||
(hello-world-test socket))
|
||||
|
||||
(println)
|
||||
(print "Destroying context... ")
|
||||
(flush)
|
||||
(.destroy ctx)
|
||||
(println "done."))
|
||||
|
||||
(defn -main
|
||||
([]
|
||||
(println "No port specified.")
|
||||
(println)
|
||||
(print-usage)
|
||||
(System/exit 1))
|
||||
([port]
|
||||
(println (format "Using port %s for tests." port))
|
||||
(println
|
||||
(format "(In another terminal, run the client script on port %s.)" port))
|
||||
|
||||
(run-tests port "ZContext" (ZContext. 1))
|
||||
|
||||
(println)
|
||||
(println "Done.")))
|
||||
@@ -1,133 +0,0 @@
|
||||
#!/usr/bin/env boot
|
||||
|
||||
(set-env! :dependencies '[[cheshire "5.6.3"]
|
||||
[org.zeromq/jeromq "0.3.5"]])
|
||||
|
||||
(require '[cheshire.core :as json])
|
||||
(import '[org.zeromq ZMQ ZContext ZMsg])
|
||||
|
||||
(def frontend-msgs
|
||||
(let [jsons [{:command "parse" :body "piano: c8 d e f g2" :options {:as "lisp"}}
|
||||
{:command "ping"}
|
||||
{:command "status"}
|
||||
{:command "version"}]]
|
||||
(for [{:keys [command] :as json} jsons]
|
||||
(doto (ZMsg.)
|
||||
(.addString (json/generate-string json))
|
||||
(.addString command)))))
|
||||
|
||||
(def frontend-play-msg
|
||||
(let [{:keys [command] :as json} {:command "play" :body "piano: c8 d e f g2"}]
|
||||
(doto (ZMsg.)
|
||||
(.addString (json/generate-string json))
|
||||
(.addString command))))
|
||||
|
||||
(defn frontend-play-status-msg
|
||||
[worker-address]
|
||||
(let [{:keys [command] :as json} {:command "play-status"}]
|
||||
(doto (ZMsg.)
|
||||
(.addString (json/generate-string json))
|
||||
(.add worker-address)
|
||||
(.addString command))))
|
||||
|
||||
(defn receive-all
|
||||
"Receives one frame, then keeps checking to see if there is more until there
|
||||
are no more frames to receive. Returns the result as a vector of strings."
|
||||
[socket]
|
||||
(loop [frames [(.recv socket)]
|
||||
more? (.hasReceiveMore socket)]
|
||||
(if more?
|
||||
(recur (conj frames (.recv socket))
|
||||
(.hasReceiveMore socket))
|
||||
frames)))
|
||||
|
||||
(defn print-usage
|
||||
[]
|
||||
(println "Example usage:")
|
||||
(println " $ alda -v -p 23232 server")
|
||||
(println " # take note of the frontend and backend ports")
|
||||
(println " # let's say for example the backend port it uses is 56667")
|
||||
(println)
|
||||
(println " # in another terminal:")
|
||||
(println " $ boot scripts/test-server.boot 23232 56667"))
|
||||
|
||||
(defn test-msg
|
||||
[msg socket]
|
||||
(println)
|
||||
(println "Sending msg:" \newline msg)
|
||||
(.send msg socket)
|
||||
(let [response (receive-all socket)]
|
||||
(println "Response received:" \newline (map #(String. %) response))
|
||||
response))
|
||||
|
||||
(defn -main
|
||||
([]
|
||||
(println "No ports specified.")
|
||||
(println)
|
||||
(print-usage)
|
||||
(System/exit 1))
|
||||
([port]
|
||||
(println "Only one port specified.")
|
||||
(println)
|
||||
(print-usage)
|
||||
(System/exit 1))
|
||||
([port1 port2]
|
||||
(let [ctx (ZContext. 1)]
|
||||
(with-open [frontend (doto (.createSocket ctx ZMQ/DEALER)
|
||||
(.connect (format "tcp://*:%s" port1)))
|
||||
backend (doto (.createSocket ctx ZMQ/DEALER)
|
||||
(.connect (format "tcp://*:%s" port2)))]
|
||||
(println
|
||||
(format "Testing server with frontend port %s, backend port %s..."
|
||||
port1 port2))
|
||||
(println)
|
||||
(println "Testing frontend port...")
|
||||
(doseq [msg frontend-msgs]
|
||||
(test-msg msg frontend)
|
||||
(Thread/sleep 500))
|
||||
; doing play msg separately so we can note the worker address...
|
||||
(let [response (test-msg frontend-play-msg frontend)
|
||||
; ...and use it to send the play-status msg
|
||||
msg (frontend-play-status-msg (last response))]
|
||||
(test-msg msg frontend)
|
||||
(Thread/sleep 500))
|
||||
(println)
|
||||
(println "Testing backend port...")
|
||||
(println)
|
||||
|
||||
(comment
|
||||
"In these tests, we are acting like a worker process interacting with
|
||||
the server we are testing.
|
||||
|
||||
The backend receives two different types of messages from workers:
|
||||
- responses to forward to the client that made the request
|
||||
- heartbeats
|
||||
|
||||
It is difficult to test the first type of message, because the server
|
||||
takes it and forwards it to the client, and does not send anything back
|
||||
to the worker to let us know that this happened.
|
||||
|
||||
The frontend tests above actually confirm that this works properly; if
|
||||
the server has at least one worker running, then if you send a message
|
||||
to the frontend and the server sends a response from the worker, then
|
||||
we know that the first type of message works.
|
||||
|
||||
Testing the second type of message here. As a worker, if we send an
|
||||
AVAILABLE heartbeat to the server, it should start sending us HEARTBEAT
|
||||
signals.")
|
||||
|
||||
(let [msg (doto (ZMsg.) (.addString "READY"))]
|
||||
(println "Sending msg:" \newline msg)
|
||||
(.send msg backend)
|
||||
(loop [n 5]
|
||||
(when (pos? n)
|
||||
(let [msg (doto (ZMsg.) (.addString "AVAILABLE"))]
|
||||
(println)
|
||||
(println "Sending msg:" \newline msg)
|
||||
(.send msg backend)
|
||||
(println)
|
||||
(let [msg (receive-all backend)]
|
||||
(println "Received msg:" \newline (map #(String. %) msg))))
|
||||
(recur (dec n)))))))
|
||||
(println)
|
||||
(println "Done.")))
|
||||
@@ -1,104 +0,0 @@
|
||||
#!/usr/bin/env boot
|
||||
|
||||
(set-env! :dependencies '[[cheshire "5.6.3"]
|
||||
[org.zeromq/jeromq "0.3.5"]])
|
||||
|
||||
(require '[cheshire.core :as json])
|
||||
(import '[org.zeromq ZMQ ZContext ZMsg ZMQException])
|
||||
|
||||
(def frontend-msgs
|
||||
(let [jsons [{:command "parse" :body "piano: c8 d e f g2" :options {:as "lisp"}}
|
||||
{:command "play" :body "piano: c8 d e f g2"}]]
|
||||
(for [{:keys [command] :as json} jsons]
|
||||
(doto (ZMsg.)
|
||||
(.add (.getBytes "fakeclientaddress"))
|
||||
(.addString (json/generate-string json))
|
||||
(.addString command)))))
|
||||
|
||||
(def server-signals
|
||||
(for [signal ["HEARTBEAT" "KILL"]]
|
||||
(doto (ZMsg.) (.addString signal))))
|
||||
|
||||
(defn receive-all
|
||||
"Receives one frame, then keeps checking to see if there is more until there
|
||||
are no more frames to receive. Returns the result as a vector of strings."
|
||||
[socket]
|
||||
(loop [frames [(.recvStr socket)]
|
||||
more? (.hasReceiveMore socket)]
|
||||
(if more?
|
||||
(recur (conj frames (.recvStr socket))
|
||||
(.hasReceiveMore socket))
|
||||
frames)))
|
||||
|
||||
(defn print-usage
|
||||
[]
|
||||
(println "Example usage:")
|
||||
(println " $ boot scripts/test-worker.boot 12345")
|
||||
(println)
|
||||
(println " # then start a worker in another terminal:")
|
||||
(println " $ alda -v -p 12345 worker"))
|
||||
|
||||
(defn test-msg
|
||||
[msg socket expect-response?]
|
||||
(println)
|
||||
(println "Sending msg:" \newline msg)
|
||||
(.send msg socket)
|
||||
(when expect-response?
|
||||
(loop [response (receive-all socket)]
|
||||
(if (#{"READY" "AVAILABLE" "BUSY"} (first response))
|
||||
(recur (receive-all socket))
|
||||
(do
|
||||
(println "Response received:" \newline response)
|
||||
response)))))
|
||||
|
||||
(defn expect-signals
|
||||
[socket & signals]
|
||||
; blocks until a message is received from worker
|
||||
(let [msg (receive-all socket)
|
||||
signal (first msg)]
|
||||
(when-not ((set signals) signal)
|
||||
(println "Unexpected message:" msg)
|
||||
(System/exit 1))))
|
||||
|
||||
(defn -main
|
||||
([]
|
||||
(println "No port specified.")
|
||||
(println)
|
||||
(print-usage)
|
||||
(System/exit 1))
|
||||
([port]
|
||||
(let [ctx (ZContext. 1)]
|
||||
(with-open [socket (try
|
||||
(doto (.createSocket ctx ZMQ/DEALER)
|
||||
(.bind (format "tcp://*:%s" port)))
|
||||
(catch ZMQException e
|
||||
(if (= 48 (.getErrorCode e))
|
||||
(do
|
||||
(println
|
||||
(format "Port %s is already in use." port)
|
||||
"Please choose a different port number.")
|
||||
(System/exit 1))
|
||||
(throw e))))]
|
||||
(println
|
||||
(format "Please start an Alda worker process listening on port %s."
|
||||
port))
|
||||
(println)
|
||||
(println "Waiting for READY signal from worker...")
|
||||
(expect-signals socket "READY")
|
||||
(println "READY signal received.")
|
||||
(Thread/sleep 500)
|
||||
(println)
|
||||
(println "Measuring worker's heartbeat...")
|
||||
(dotimes [_ 5]
|
||||
(expect-signals socket "READY" "AVAILABLE" "BUSY")
|
||||
(.send (doto (ZMsg.) (.addString "HEARTBEAT")) socket)
|
||||
(println "th-thump"))
|
||||
(Thread/sleep 500)
|
||||
(doseq [msg (concat frontend-msgs)]
|
||||
(test-msg msg socket true)
|
||||
(Thread/sleep 500))
|
||||
(doseq [msg (concat server-signals)]
|
||||
(test-msg msg socket false)
|
||||
(Thread/sleep 500))))
|
||||
(println)
|
||||
(println "Done.")))
|
||||
Reference in New Issue
Block a user