Skip to content

Commit 45e1880

Browse files
committed
[new] [#422] Add client-side util to simulate a broken connection
1 parent e5aede3 commit 45e1880

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

src/taoensso/sente.cljc

+39-2
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@
10051005
(-chsk-connect! [chsk])
10061006
(-chsk-disconnect! [chsk reason])
10071007
(-chsk-reconnect! [chsk reason])
1008-
(-chsk-break-connection! [chsk])
1008+
(-chsk-break-connection! [chsk opts])
10091009
(-chsk-send! [chsk ev opts]))
10101010

10111011
(defn chsk-connect! [chsk] (-chsk-connect! chsk))
@@ -1014,7 +1014,22 @@
10141014
"Cycles connection, useful for reauthenticating after login/logout, etc."
10151015
[chsk] (-chsk-reconnect! chsk :requested-reconnect))
10161016

1017-
(def ^:deprecated chsk-destroy! "Deprecated" chsk-disconnect!)
1017+
(defn chsk-break-connection!
1018+
"Breaks channel socket's underlying connection without doing a clean
1019+
disconnect as in `chsk-disconnect!`. Useful for simulating broken
1020+
connections in testing, etc.
1021+
1022+
Options:
1023+
1024+
`:close-ws?` - (Default: true)
1025+
Allow WebSocket's `on-close` event to fire?
1026+
Set to falsey to ~simulate a broken socket that doesn't realise
1027+
it's broken."
1028+
1029+
([chsk] (-chsk-break-connection! chsk nil))
1030+
([chsk {:keys [close-ws?] :as opts
1031+
:or {close-ws? true}}]
1032+
(-chsk-break-connection! chsk opts)))
10181033

10191034
(defn chsk-send!
10201035
"Sends `[ev-id ev-?data :as event]`, returns true on apparent success."
@@ -1329,6 +1344,21 @@
13291344
(-chsk-disconnect! chsk reason)
13301345
(-chsk-connect! chsk))
13311346

1347+
(-chsk-break-connection! [chsk opts]
1348+
(let [{:keys [close-ws? ws-code]
1349+
:or {ws-code 3000}} opts]
1350+
1351+
(when-let [[s _sid]
1352+
(if-not close-ws?
1353+
;; Suppress socket's `on-close` handler by breaking
1354+
;; (own-socket?) socket ownership test
1355+
(reset-in! socket_ nil)
1356+
(do @socket_))]
1357+
1358+
#?(:clj (.close ^WebSocketClient s ws-code "CLOSE_ABNORMAL")
1359+
:cljs (.close s ws-code "CLOSE_ABNORMAL")))
1360+
nil))
1361+
13321362
(-chsk-send! [chsk ev opts]
13331363
(let [{?timeout-ms :timeout-ms ?cb :cb :keys [flush?]} opts
13341364
_ (assert-send-args ev ?timeout-ms ?cb)
@@ -1573,6 +1603,9 @@
15731603
(-chsk-disconnect! chsk reason)
15741604
(-chsk-connect! chsk))
15751605

1606+
(-chsk-break-connection! [chsk _opts]
1607+
(when-let [x @curr-xhr_] (.abort x)) nil)
1608+
15761609
(-chsk-send! [chsk ev opts]
15771610
(let [{?timeout-ms :timeout-ms ?cb :cb :keys [flush?]} opts
15781611
_ (assert-send-args ev ?timeout-ms ?cb)
@@ -1736,6 +1769,10 @@
17361769
(-chsk-disconnect! impl reason)
17371770
(-chsk-connect! chsk)))
17381771

1772+
(-chsk-break-connection! [chsk opts]
1773+
(when-let [impl @impl_]
1774+
(-chsk-break-connection! impl opts)))
1775+
17391776
(-chsk-send! [chsk ev opts]
17401777
(if-let [impl @impl_]
17411778
(-chsk-send! impl ev opts)

0 commit comments

Comments
 (0)