fix: restart stale sync websocket

This commit is contained in:
Tienson Qin
2026-05-03 15:08:15 +08:00
parent df38c26dbf
commit 70042a46df
2 changed files with 61 additions and 3 deletions

View File

@@ -273,10 +273,8 @@
[client repo graph-id]
(when (and client (= repo (:repo client)) (= graph-id (:graph-id client)))
(let [ws (:ws client)
ws-state (some-> (:ws-state client) deref)
ws-ready-state (when ws (ready-state ws))]
(or (= :open ws-state)
(contains? #{0 1} ws-ready-state)))))
(contains? #{0 1} ws-ready-state))))
(defn- connect!
[repo client url token]

View File

@@ -0,0 +1,60 @@
(ns frontend.worker.sync.restart-test
(:require [cljs.test :refer [async deftest is]]
[frontend.worker.platform :as platform]
[frontend.worker.shared-service :as shared-service]
[frontend.worker.state :as worker-state]
[frontend.worker.sync :as sync]
[frontend.worker.sync.util :as sync-util]
[promesa.core :as p]))
(deftest start-reconnects-closed-ws-with-stale-open-state-test
(async done
(let [repo "stale-repo"
graph-id "graph-1"
prev-client @worker-state/*db-sync-client
prev-config @worker-state/*db-sync-config
prev-platform (try
(platform/current)
(catch :default _ nil))
connect-calls (atom 0)
stale-ws #js {:readyState 3
:close (fn [] nil)}
stale-client {:repo repo
:graph-id graph-id
:ws stale-ws
:ws-state (atom :open)
:online-users (atom [])
:reconnect (atom {:attempt 0 :timer nil})
:stale-kill-timer (atom nil)}]
(reset! worker-state/*db-sync-config {:ws-url "wss://sync.example.test/sync/%s"})
(reset! worker-state/*db-sync-client stale-client)
(platform/set-platform!
{:env {:runtime :node}
:storage {}
:kv {}
:broadcast {}
:websocket {:connect (fn [_url]
(swap! connect-calls inc)
#js {:readyState 0
:close (fn [] nil)})}
:crypto {}
:timers {}
:sqlite {}})
(-> (p/with-redefs [sync-util/get-graph-id (fn [_repo] graph-id)
sync/<resolve-ws-token (fn [] (p/resolved "token"))
shared-service/broadcast-to-clients! (fn [& _] nil)]
(sync/start! repo))
(p/then
(fn [_]
(is (= 1 @connect-calls)
"start! should reconnect when the cached websocket is already closed")))
(p/catch
(fn [error]
(is false (str "unexpected error: " error))))
(p/finally
(fn []
(reset! worker-state/*db-sync-client prev-client)
(reset! worker-state/*db-sync-config prev-config)
(when prev-platform
(platform/set-platform! prev-platform))
(done)))))))