|
| 1 | +(ns status-im.test.utils.clocks |
| 2 | + (:require [cljs.test :refer-macros [deftest is testing]] |
| 3 | + [status-im.utils.clocks :as clocks])) |
| 4 | + |
| 5 | +;; Messages are shown on a per-chat basis, ordered by the message clock-value. |
| 6 | +;; See status-im-utils.clocks namespace for details. |
| 7 | + |
| 8 | +;; We are not a monolith. |
| 9 | +(def a (atom {:identity "a"})) |
| 10 | +(def b (atom {:identity "b"})) |
| 11 | +(def c (atom {:identity "c"})) |
| 12 | + |
| 13 | +;; The network is unreliable. |
| 14 | +(defn random-broadcast! [chat-id message] |
| 15 | + (when (> (rand-int 10) 5) (recv! a chat-id message)) |
| 16 | + (when (> (rand-int 10) 5) (recv! b chat-id message)) |
| 17 | + (when (> (rand-int 10) 5) (recv! c chat-id message))) |
| 18 | + |
| 19 | +(defn get-last-clock-value |
| 20 | + [db chat-id] |
| 21 | + (if-let [messages (-> @db :chats chat-id :messages)] |
| 22 | + (-> (sort-by :clock-value > messages) |
| 23 | + first |
| 24 | + :clock-value) |
| 25 | + 0)) |
| 26 | + |
| 27 | +(defn save! [db chat-id message] |
| 28 | + (swap! db |
| 29 | + (fn [state] |
| 30 | + (let [messages (-> state :chats chat-id :messages)] |
| 31 | + (assoc-in state [:chats chat-id :messages] |
| 32 | + (conj messages message)))))) |
| 33 | + |
| 34 | +(defn send! [db chat-id message] |
| 35 | + (let [clock-value (get-last-clock-value db chat-id) |
| 36 | + prepared-message (assoc message :clock-value (clocks/send clock-value))] |
| 37 | + (save! db chat-id prepared-message) |
| 38 | + (random-broadcast! chat-id prepared-message))) |
| 39 | + |
| 40 | +(defn recv! [db chat-id {:keys [clock-value] :as message}] |
| 41 | + (let [local-clock (get-last-clock-value db chat-id) |
| 42 | + new-clock (clocks/receive clock-value local-clock)] |
| 43 | + (when-not (= (:from message) (:identity @db)) |
| 44 | + (save! db chat-id (assoc message :clock-value new-clock))))) |
| 45 | + |
| 46 | +(defn thread [db chat-id] |
| 47 | + (let [messages (-> @db :chats chat-id :messages)] |
| 48 | + (sort-by :clock-value < messages))) |
| 49 | + |
| 50 | +(defn format-message [{:keys [from text]}] |
| 51 | + (str from ": " text ", ")) |
| 52 | + |
| 53 | +(defn format-thread [thread] |
| 54 | + (apply str (map format-message thread))) |
| 55 | + |
| 56 | +;; Invariant we want to maintain. |
| 57 | +(defn ordered-increasing-text? [thread] |
| 58 | + (let [xs (map :text thread)] |
| 59 | + (or (empty? xs) (apply < xs)))) |
| 60 | + |
| 61 | +(defn simulate! [] |
| 62 | + (send! a :foo {:from "a" :text "1"}) |
| 63 | + (send! a :foo {:from "a" :text "2"}) |
| 64 | + |
| 65 | + (send! a :bar {:from "a" :text "1"}) |
| 66 | + |
| 67 | + (send! b :foo {:from "b" :text "3"}) |
| 68 | + (send! c :foo {:from "c" :text "4"}) |
| 69 | + (send! a :foo {:from "a" :text "5"}) |
| 70 | + |
| 71 | + (send! c :bar {:from "c" :text "7"})) |
| 72 | + |
| 73 | +(deftest clocks |
| 74 | + (testing "Message order preserved" |
| 75 | + (simulate!) |
| 76 | + (is (ordered-increasing-text? (thread a :foo))) |
| 77 | + (is (ordered-increasing-text? (thread b :foo))) |
| 78 | + (is (ordered-increasing-text? (thread c :foo))) |
| 79 | + (is (ordered-increasing-text? (thread a :bar)))) |
| 80 | + |
| 81 | + (testing "Bad thread recognized as such" |
| 82 | + (let [bad-thread '({:from "a", :text "1", :clock-value 1} |
| 83 | + {:from "c", :text "4", :clock-value 1} |
| 84 | + {:from "a", :text "2", :clock-value 2} |
| 85 | + {:from "a", :text "5", :clock-value 8})] |
| 86 | + (is (not (ordered-increasing-text? bad-thread)))))) |
| 87 | + |
| 88 | + ;; Debugging |
| 89 | +;;(println "******************************************") |
| 90 | +;;(println "A's POV :foo" (format-thread (thread a :foo))) |
| 91 | +;;(println "B's POV :foo" (format-thread (thread b :foo))) |
| 92 | +;;(println "C's POV :foo" (format-thread (thread c :foo))) |
| 93 | +;;(println "******************************************") |
0 commit comments