vary ports across socket repl tests (#1539)

* use different port for socket repl tests

- update socket-command to socket-command-on-port, which
 takes a port and then returns the socket-command function
- change port numbers across tests

* undo little formatting change

* grr... newlines
This commit is contained in:
Bob 2023-04-18 03:47:40 -04:00 committed by GitHub
parent a945ea8146
commit 19bfd172fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,30 +15,31 @@
(set! *warn-on-reflection* true) (set! *warn-on-reflection* true)
(defn socket-command [expr expected & [log?]] (defn socket-command-on-port [^long port]
(with-open [socket (java.net.Socket. "127.0.0.1" 1666) (fn [expr expected & [log?]]
reader (io/reader socket) (with-open [socket (java.net.Socket. "127.0.0.1" port)
sw (java.io.StringWriter.) reader (io/reader socket)
writer (io/writer socket)] sw (java.io.StringWriter.)
(binding [*out* writer] writer (io/writer socket)]
(println (str expr "\n"))) (binding [*out* writer]
(loop [] (println (str expr "\n")))
(when-let [l (try (.readLine ^java.io.BufferedReader reader) (loop []
(catch java.net.SocketException _ nil))] (when-let [l (try (.readLine ^java.io.BufferedReader reader)
(when log? (catch java.net.SocketException _ nil))]
(println "===" l)) (when log?
(binding [*out* sw] (println "===" l))
(println l)) (binding [*out* sw]
(let [s (str sw)] (println l))
;; (prn :s s :expected expected (str/includes? s expected)) (let [s (str sw)]
(if (if (fn? expected) ;; (prn :s s :expected expected (str/includes? s expected))
(expected s) (if (if (fn? expected)
(str/includes? s expected)) (expected s)
(is true) (str/includes? s expected))
(recur))))) (is true)
(binding [*out* writer] (recur)))))
(println ":repl/quit\n")) (binding [*out* writer]
:success)) (println ":repl/quit\n"))
:success)))
(def server-process (volatile! nil)) (def server-process (volatile! nil))
@ -46,87 +47,90 @@
(deftest ^:flaky socket-repl-test (deftest ^:flaky socket-repl-test
(when exec? (when exec?
(try (let [socket-command (socket-command-on-port 1666)]
(if tu/jvm? (try
(let [ctx (init {:namespaces main/namespaces
:features #{:bb}})]
(ctx-store/reset-ctx! ctx)
(start-repl! "0.0.0.0:1666" ctx))
(do (vreset! server-process
(p/process ["./bb" "socket-repl" "localhost:1666"]))
(w/wait-for-port "localhost" 1666)))
(Thread/sleep 50)
(is (socket-command "(+ 1 2 3)" "user=> 6"))
(testing "&env"
(socket-command "(defmacro bindings [] (mapv #(list 'quote %) (keys &env)))" "bindings")
(socket-command "(defn bar [x y z] (bindings))" "bar")
(is (socket-command "(bar 1 2 3)" "[x y z]")))
(testing "reader conditionals"
(is (socket-command "#?(:bb 1337 :clj 8888)" "1337")))
(testing "*1, *2, *3, *e"
(is (socket-command "1\n*1" "1")))
(testing "*ns*"
(is (socket-command "(ns foo.bar) (ns-name *ns*)" "foo.bar")))
(testing "set dyn vars"
(is (socket-command "[(set! *warn-on-reflection* true) (set! *unchecked-math* true)]" "[true true]")))
(finally
(if tu/jvm? (if tu/jvm?
(do (stop-repl!) (let [ctx (init {:namespaces main/namespaces
(ctx-store/reset-ctx! nil) :features #{:bb}})]
(Thread/sleep 100)) (ctx-store/reset-ctx! ctx)
(p/destroy-tree @server-process)))))) (start-repl! "0.0.0.0:1666" ctx))
(do (vreset! server-process
(p/process ["./bb" "socket-repl" "localhost:1666"]))
(w/wait-for-port "localhost" 1666)))
(Thread/sleep 50)
(is (socket-command "(+ 1 2 3)" "user=> 6"))
(testing "&env"
(socket-command "(defmacro bindings [] (mapv #(list 'quote %) (keys &env)))" "bindings")
(socket-command "(defn bar [x y z] (bindings))" "bar")
(is (socket-command "(bar 1 2 3)" "[x y z]")))
(testing "reader conditionals"
(is (socket-command "#?(:bb 1337 :clj 8888)" "1337")))
(testing "*1, *2, *3, *e"
(is (socket-command "1\n*1" "1")))
(testing "*ns*"
(is (socket-command "(ns foo.bar) (ns-name *ns*)" "foo.bar")))
(testing "set dyn vars"
(is (socket-command "[(set! *warn-on-reflection* true) (set! *unchecked-math* true)]" "[true true]")))
(finally
(if tu/jvm?
(do (stop-repl!)
(ctx-store/reset-ctx! nil)
(Thread/sleep 100))
(p/destroy-tree @server-process)))))))
(deftest ^:flaky socket-repl-opts-test (deftest ^:flaky socket-repl-opts-test
(when exec? (when exec?
(try (let [socket-command (socket-command-on-port 1667)]
(if tu/jvm? (try
(let [ctx (init {:bindings {'*command-line-args* (if tu/jvm?
["a" "b" "c"]} (let [ctx (init {:bindings {'*command-line-args*
:env (atom {}) ["a" "b" "c"]}
:namespaces {'clojure.core.server clojure-core-server-namespace} :env (atom {})
:features #{:bb}})] :namespaces {'clojure.core.server clojure-core-server-namespace}
(ctx-store/reset-ctx! ctx) :features #{:bb}})]
(start-repl! "{:address \"localhost\" :accept clojure.core.server/repl :port 1666}" (ctx-store/reset-ctx! ctx)
ctx)) (start-repl! "{:address \"localhost\" :accept clojure.core.server/repl :port 1667}"
(do (vreset! server-process ctx))
(p/process ["./bb" "--socket-repl" "{:address \"localhost\" :accept clojure.core.server/repl :port 1666}"])) (do (vreset! server-process
(w/wait-for-port "localhost" 1666))) (p/process ["./bb" "--socket-repl" "{:address \"localhost\" :accept clojure.core.server/repl :port 1667}"]))
(Thread/sleep 50) (w/wait-for-port "localhost" 1667)))
(is (socket-command "(+ 1 2 3)" "user=> 6")) (Thread/sleep 50)
(finally (is (socket-command "(+ 1 2 3)" "user=> 6"))
(if tu/jvm? (finally
(do (stop-repl!) (if tu/jvm?
(ctx-store/reset-ctx! nil) (do (stop-repl!)
(Thread/sleep 100)) (ctx-store/reset-ctx! nil)
(p/destroy-tree @server-process)))))) (Thread/sleep 100))
(p/destroy-tree @server-process)))))))
(deftest ^:flaky socket-prepl-test (deftest ^:flaky socket-prepl-test
(when exec? (when exec?
(try (let [socket-command (socket-command-on-port 1668)]
(if tu/jvm? (try
(let [ctx (init {:bindings {'*command-line-args* (if tu/jvm?
["a" "b" "c"]} (let [ctx (init {:bindings {'*command-line-args*
:env (atom {}) ["a" "b" "c"]}
:namespaces {'clojure.core.server clojure-core-server-namespace} :env (atom {})
:features #{:bb}})] :namespaces {'clojure.core.server clojure-core-server-namespace}
(ctx-store/reset-ctx! ctx) :features #{:bb}})]
(start-repl! "{:address \"localhost\" :accept clojure.core.server/io-prepl :port 1666}" (ctx-store/reset-ctx! ctx)
ctx)) (start-repl! "{:address \"localhost\" :accept clojure.core.server/io-prepl :port 1668}"
(do (vreset! server-process ctx))
(p/process ["./bb" "--socket-repl" "{:address \"localhost\" :accept clojure.core.server/io-prepl :port 1666}"])) (do (vreset! server-process
(w/wait-for-port "localhost" 1666))) (p/process ["./bb" "--socket-repl" "{:address \"localhost\" :accept clojure.core.server/io-prepl :port 1668}"]))
(Thread/sleep 50) (w/wait-for-port "localhost" 1668)))
(is (socket-command "(+ 1 2 3)" (fn [s] (Thread/sleep 50)
(let [m (edn/read-string s)] (is (socket-command "(+ 1 2 3)" (fn [s]
(and (= "6" (:val m)) (let [m (edn/read-string s)]
(= "user" (:ns m)) (and (= "6" (:val m))
(= "(+ 1 2 3)" (:form m))))))) (= "user" (:ns m))
(finally (= "(+ 1 2 3)" (:form m)))))))
(if tu/jvm? (finally
(do (stop-repl!) (if tu/jvm?
(ctx-store/reset-ctx! nil) (do (stop-repl!)
(Thread/sleep 100)) (ctx-store/reset-ctx! nil)
(p/destroy-tree @server-process)))))) (Thread/sleep 100))
(p/destroy-tree @server-process)))))))
;;;; Scratch ;;;; Scratch
@ -141,5 +145,5 @@
'*command-line-args* '*command-line-args*
["a" "b" "c"]} ["a" "b" "c"]}
:env (atom {})}) :env (atom {})})
(socket-command "(+ 1 2 3)" "6") ((socket-command-on-port 1666) "(+ 1 2 3)" "6")
) )