1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 11:09:54 +00:00

Create test case for localname/peername

This commit is contained in:
llmII 2021-09-07 07:12:43 -05:00
parent 3740eadb7d
commit 077bf5ebae
No known key found for this signature in database
GPG Key ID: E3AD2E259F58A9A0

View File

@ -151,6 +151,48 @@
(:close s))
# Test localname and peername
(repeat 10
(defn check-matching-names [stream &opt direction]
"Checks that the remote agrees with the local about ip/port"
(let [[my-ip my-port] (net/localname stream)
[remote-ip remote-port] (net/peername stream)
to-write (string/join
@[my-ip (string my-port)
remote-ip (string remote-port)]
" ")
buffer @""]
(if (= direction :write)
(do (net/write stream to-write) (net/read stream 1024 buffer))
(do (net/read stream 1024 buffer) (net/write stream to-write)))
(def comparison (string/split " " buffer))
(printf "%s\n\t%s%q\n\t%s%q"
(if (= direction :write) "-->" "<--")
"remote view: "
comparison
"local view: "
[my-ip my-port remote-ip remote-port])
(assert (and (= my-ip (get comparison 2))
(= (string my-port) (get comparison 3))
(= remote-ip (get comparison 0))
(= (string remote-port) (get comparison 1))))))
(defn names-handler
"Simple handler for connections."
[stream]
(defer (:close stream)
(check-matching-names stream)))
(def s (net/server "127.0.0.1" "8000" names-handler))
(assert s "made server 1")
(defn test-names []
(with [conn (net/connect "127.0.0.1" "8000")]
(check-matching-names conn :write)))
(test-names)
(test-names)
(:close s))
# Create pipe
(var pipe-counter 0)