Skip to content

Commit 24334fc

Browse files
committed
Update tests
1 parent 8908f52 commit 24334fc

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

deps.local.edn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{:launchpad/aliases [:dev :test]}

src/lambdaisland/funnel.clj

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109

110110
(defn match-selector? [whoami selector]
111111
(cond
112+
(nil? whoami) false
112113
(true? selector) true
113114
(vector? selector) (= (second selector) (get whoami (first selector)))
114115
(map? selector) (reduce (fn [_ [k v]]
@@ -133,12 +134,12 @@
133134

134135
(defn handle-query [conn selector conns]
135136
(let [msg {:funnel/clients
136-
(map (comp :whoami val)
137-
(filter
138-
(fn [[c m]]
139-
(and (match-selector? (:whoami m) selector)
140-
(not= c conn)))
141-
conns))}]
137+
(keep (comp :whoami val)
138+
(filter
139+
(fn [[c m]]
140+
(and (match-selector? (:whoami m) selector)
141+
(not= c conn)))
142+
conns))}]
142143
(async/>!! (outbox conn) msg)))
143144

144145
(defn handle-message [state ^WebSocket conn raw-msg]

test/lambdaisland/funnel/test_util.clj

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
(ns lambdaisland.funnel.test-util
2-
(:require [clojure.java.io :as io]
3-
[clojure.test :as t]
4-
[lambdaisland.funnel :as funnel]
5-
[matcher-combinators.test]
6-
[matcher-combinators.core :as mc])
7-
(:import (java.net URI)
8-
(java.net ServerSocket)
9-
(org.java_websocket.client WebSocketClient)))
2+
(:require
3+
[clojure.java.io :as io]
4+
[clojure.test :as t]
5+
[lambdaisland.funnel :as funnel]
6+
[matcher-combinators.test]
7+
[matcher-combinators.core :as mc])
8+
(:import
9+
(java.net URI)
10+
(java.net ServerSocket)
11+
(org.java_websocket.client WebSocketClient)))
1012

1113
(def ^:dynamic *port* 0)
1214

test/lambdaisland/funnel_test.clj

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,26 @@
2020
(with-open [s (test-server state)
2121
c (test-client)]
2222
(c {:funnel/whoami {:id 123}})
23-
(will (= [{:whoami {:id 123}}] (vals @state)))
23+
(will (= [{:whoami {:id 123}
24+
:format :transit}] (vals @state)))
2425

2526
(c {:funnel/whoami {:id :abc :hello :world}})
26-
(will (= [{:whoami {:id :abc :hello :world}}] (vals @state)))
27+
(will (= [{:whoami {:id :abc :hello :world}
28+
:format :transit}] (vals @state)))
2729

2830
(with-open [c2 (test-client)]
2931
(c2 {:funnel/whoami {:root "/x/y/z"}})
3032

3133
(will (match? (m/in-any-order [{:whoami {:id :abc
32-
:hello :world}}
33-
{:whoami {:root "/x/y/z"}}])
34+
:hello :world}
35+
:format :transit}
36+
{:whoami {:root "/x/y/z"}
37+
:format :transit}])
3438
(vals @state))))
3539

3640
(testing "closing will clean up the client connection"
37-
(will (= [{:whoami {:id :abc :hello :world}}] (vals @state)))))))
41+
(will (= [{:whoami {:id :abc :hello :world}
42+
:format :transit}] (vals @state)))))))
3843

3944
(deftest subscribe-test
4045
(testing "messages get forwarded to subscriber"
@@ -47,7 +52,9 @@
4752
:funnel/subscribe [:id 2]})
4853
;; Checkpoint to prevent race conditions, we only continue when funnel
4954
;; has registered the subscription.
50-
(will (= [{:whoami {:id 1}, :subscriptions #{[:id 2]}}]
55+
(will (= [{:whoami {:id 1} :subscriptions #{[:id 2]} :format :transit}
56+
{:format :transit}
57+
{:format :transit}]
5158
(vals @state)))
5259

5360
(c2 {:funnel/whoami {:id 2}
@@ -67,9 +74,9 @@
6774
(with-open [s (test-server state)
6875
c (test-client)]
6976
(c {:funnel/subscribe [:foo :bar]})
70-
(will (= [{:subscriptions #{[:foo :bar]}}] (vals @state)))
77+
(will (= [{:subscriptions #{[:foo :bar]} :format :transit}] (vals @state)))
7178
(c {:funnel/unsubscribe [:foo :bar]})
72-
(will (= [{:subscriptions #{}}] (vals @state))))))
79+
(will (= [{:subscriptions #{} :format :transit}] (vals @state))))))
7380

7481
(deftest match-selector-test
7582
(testing "vector"
@@ -78,8 +85,10 @@
7885
(is (not (funnel/match-selector? {:id 123} [:id 456]))))
7986

8087
(testing "true"
81-
(is (funnel/match-selector? {:id 123} true))
82-
(is (funnel/match-selector? nil true)))
88+
(is (funnel/match-selector? {:id 123} true)))
89+
90+
(testing "no whoami"
91+
(is (not (funnel/match-selector? nil true))))
8392

8493
(testing "map"
8594
(is (funnel/match-selector? {:type :x :subtype :a} {:type :x}))
@@ -88,9 +97,12 @@
8897

8998
(deftest destinations-test
9099
(let [state {:ws1 {:whoami {:id :ws1}
91-
:subscriptions #{[:id :ws2]}}
92-
:ws2 {:whoami {:id :ws2}}
93-
:ws3 {:whoami {:id :ws3}}}]
100+
:subscriptions #{[:id :ws2]}
101+
:format :transit}
102+
:ws2 {:whoami {:id :ws2}
103+
:format :transit}
104+
:ws3 {:whoami {:id :ws3}
105+
:format :transit}}]
94106

95107
(is (= [:ws1] (funnel/destinations :ws2 nil state)))
96108
(is (match? (m/in-any-order [:ws1 :ws3])

0 commit comments

Comments
 (0)