Skip to content

Commit

Permalink
removed old protobuf library in favor of the new one
Browse files Browse the repository at this point in the history
  • Loading branch information
macalimlim committed Jul 2, 2020
1 parent ae952f5 commit ade207d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 44 deletions.
1 change: 0 additions & 1 deletion resources/config.test.ci.edn
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
:statsd {:host "localhost"
:port [8125 :int]
:enabled [false :bool]}
:alpha-features {:protobuf-middleware {:enabled false}}
:sentry {:enabled [false :bool]
:dsn "dummy"
:worker-count [10 :int]
Expand Down
2 changes: 1 addition & 1 deletion resources/config.test.edn
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
:port [8125 :int]
:enabled [false :bool]}
:metrics {:constructor "ziggurat.dropwizard-metrics-wrapper/->DropwizardMetrics"}
:alpha-features {:protobuf-middleware {:enabled false}}
:alpha-features {}
:sentry {:enabled [false :bool]
:dsn "dummy"
:worker-count [10 :int]
Expand Down
1 change: 0 additions & 1 deletion src/ziggurat/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
:worker-count 10
:queue-size 10
:thread-termination-wait-s 1}
:alpha-features {:protobuf-middleware {:enabled false}}
:rabbit-mq-connection {:port 5672
:username "guest"
:password "guest"
Expand Down
16 changes: 6 additions & 10 deletions src/ziggurat/middleware/default.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
(:require [protobuf.impl.flatland.mapdef :as protodef]
[sentry-clj.async :as sentry]
[ziggurat.config :refer [get-in-config ziggurat-config]]
[flatland.protobuf.core :as proto]
[ziggurat.metrics :as metrics]
[ziggurat.sentry :refer [sentry-reporter]]))

Expand All @@ -15,15 +14,12 @@
[message proto-class topic-entity-name]
(if-not (map? message) ;; TODO: we should have proper dispatch logic per message type (not like this)
(try
(let [[proto-define-fn proto-load-fn proto-schema-fn] (if (true? (get-in-config [:alpha-features :protobuf-middleware :enabled]))
[protodef/mapdef protodef/parse protodef/mapdef->schema]
[proto/protodef proto/protobuf-load proto/protobuf-schema])
proto-klass (proto-define-fn proto-class)
loaded-proto (proto-load-fn proto-klass message)
proto-keys (-> proto-klass
proto-schema-fn
:fields
keys)]
(let [proto-klass (protodef/mapdef proto-class)
loaded-proto (protodef/parse proto-klass message)
proto-keys (-> proto-klass
protodef/mapdef->schema
:fields
keys)]
(select-keys loaded-proto proto-keys))
(catch Throwable e
(let [service-name (:app-name (ziggurat-config))
Expand Down
42 changes: 11 additions & 31 deletions test/ziggurat/middleware/default_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns ziggurat.middleware.default-test
(:require [clojure.test :refer [deftest is join-fixtures testing use-fixtures]]
[protobuf.core :as proto]
[ziggurat.config :refer [ziggurat-config]]
[ziggurat.fixtures :as fix]
[ziggurat.metrics :as metrics]
[ziggurat.middleware.default :as mw])
Expand Down Expand Up @@ -78,33 +77,14 @@
(reset! metric-reporter-called? true))]
((mw/protobuf->hash handler-fn nil topic-entity-name) (mw/->RegularMessage nil)))
(is (true? @handler-fn-called?))
(is (true? @metric-reporter-called?)))))

(deftest protobuf->hash-test-alpha-and-deprecated
(testing "Deprecated protobuf deserializer"
(with-redefs [ziggurat-config (fn [] {:alpha-features {:protobuf-middleware {:enabled false}}})]
(common-protobuf->hash-test)
(testing "When alpha feature is disabled use the old deserializer function"
(let [deserialize-message-called? (atom false)
topic-entity-name "test"
message {:id 7
:path "/photos/h2k3j4h9h23"}
proto-class Example$Photo
proto-message (proto/->bytes (proto/create Example$Photo message))]
(with-redefs [mw/deserialize-message (fn [_ _ _] (reset! deserialize-message-called? true))]
((mw/protobuf->hash (constantly nil) proto-class topic-entity-name) (mw/->RegularMessage proto-message))
(is (true? @deserialize-message-called?)))))))
(testing "Alpha protobuf deserializer"
(with-redefs [ziggurat.config/ziggurat-config (fn [] {:alpha-features {:protobuf-middleware {:enabled true}}})]
(common-protobuf->hash-test)
(testing "When alpha feature is enabled use the new deserializer function"
(let [deserialize-message-called? (atom false)
topic-entity-name "test"
message {:id 7
:path "/photos/h2k3j4h9h23"}
proto-class Example$Photo
proto-message (proto/->bytes (proto/create Example$Photo message))]
(with-redefs [mw/deserialize-message (fn [_ _ _] (reset! deserialize-message-called? true))]

((mw/protobuf->hash (constantly nil) proto-class topic-entity-name) (mw/->RegularMessage proto-message))
(is (true? @deserialize-message-called?))))))))
(is (true? @metric-reporter-called?))))
(testing "using the new deserializer function"
(let [deserialize-message-called? (atom false)
topic-entity-name "test"
message {:id 7
:path "/photos/h2k3j4h9h23"}
proto-class Example$Photo
proto-message (proto/->bytes (proto/create Example$Photo message))]
(with-redefs [mw/deserialize-message (fn [_ _ _] (reset! deserialize-message-called? true))]
((mw/protobuf->hash (constantly nil) proto-class topic-entity-name) (mw/->RegularMessage proto-message))
(is (true? @deserialize-message-called?))))))

0 comments on commit ade207d

Please sign in to comment.