|
6 | 6 | [clojure.core.async :as async]
|
7 | 7 | [ketu.clients.consumer :as consumer]
|
8 | 8 | [ketu.clients.producer :as producer]
|
| 9 | + [ketu.decorators.consumer.protocol :refer [ConsumerDecorator]] |
9 | 10 | [ketu.async.source :as source]
|
10 | 11 | [ketu.async.sink :as sink]
|
11 | 12 | [ketu.test.kafka-setup :as kafka-setup])
|
|
213 | 214 | (.close ^AdminClient admin-client)))))
|
214 | 215 |
|
215 | 216 | (deftest consumer-decorator
|
216 |
| - (let [consumer-chan (async/chan 10) |
217 |
| - result-chan (async/chan 100) |
218 |
| - clicks-consumer-opts {:name "clicks-consumer" |
219 |
| - :brokers (kafka-setup/get-bootstrap-servers) |
220 |
| - :topic "clicks" |
221 |
| - :group-id "clicks-test-consumer" |
222 |
| - :auto-offset-reset "earliest" |
223 |
| - :shape :value |
224 |
| - :ketu.source/consumer-decorator (fn [{_consumer :ketu.source/consumer} poll-fn] |
225 |
| - (let [records (poll-fn)] |
226 |
| - (doseq [^ConsumerRecord record records] |
227 |
| - (async/>!! result-chan (String. ^"[B" (.value record)))) |
228 |
| - records))} |
229 |
| - source (source/source consumer-chan clicks-consumer-opts) |
230 |
| - clicks-producer-opts {:name "clicks-producer" |
231 |
| - :brokers (kafka-setup/get-bootstrap-servers) |
232 |
| - :topic "clicks" |
233 |
| - :key-type :string |
234 |
| - :internal-config {"value.serializer" "org.apache.kafka.common.serialization.StringSerializer"} |
235 |
| - :shape [:vector :key :value]} |
236 |
| - producer-chan (async/chan 10) |
237 |
| - sink (sink/sink producer-chan clicks-producer-opts) |
238 |
| - input-values #{"1" "2" "3"}] |
239 |
| - (try |
240 |
| - (doseq [value input-values] |
241 |
| - (async/>!! producer-chan ["1" value])) |
242 |
| - (is (= input-values (into #{} (repeatedly 3 #(u/try-take! result-chan))))) |
243 |
| - (is (= input-values (into #{} (map #(String. ^"[B" %)) (repeatedly 3 #(u/try-take! consumer-chan))))) |
244 |
| - (finally |
245 |
| - (Thread/sleep 2000) |
246 |
| - (source/stop! source) |
247 |
| - (async/close! producer-chan) |
248 |
| - (sink/stop! sink))))) |
| 217 | + (testing "consumer decorator functionality" |
| 218 | + (let [consumer-chan (async/chan 10) |
| 219 | + result-chan (async/chan 100) |
| 220 | + clicks-consumer-opts {:name "clicks-consumer" |
| 221 | + :brokers (kafka-setup/get-bootstrap-servers) |
| 222 | + :topic "clicks" |
| 223 | + :group-id "clicks-test-consumer" |
| 224 | + :auto-offset-reset "earliest" |
| 225 | + :shape :value |
| 226 | + :ketu.source/consumer-decorator (reify ConsumerDecorator |
| 227 | + (poll! [_ {_consumer :ketu.source/consumer} poll-fn] |
| 228 | + (let [records (poll-fn)] |
| 229 | + (doseq [^ConsumerRecord record records] |
| 230 | + (async/>!! result-chan (String. ^"[B" (.value record)))) |
| 231 | + records)) |
| 232 | + (valid? [_ _] |
| 233 | + true))} |
| 234 | + source (source/source consumer-chan clicks-consumer-opts) |
| 235 | + clicks-producer-opts {:name "clicks-producer" |
| 236 | + :brokers (kafka-setup/get-bootstrap-servers) |
| 237 | + :topic "clicks" |
| 238 | + :key-type :string |
| 239 | + :internal-config {"value.serializer" "org.apache.kafka.common.serialization.StringSerializer"} |
| 240 | + :shape [:vector :key :value]} |
| 241 | + producer-chan (async/chan 10) |
| 242 | + sink (sink/sink producer-chan clicks-producer-opts) |
| 243 | + input-values #{"1" "2" "3"}] |
| 244 | + (try |
| 245 | + (doseq [value input-values] |
| 246 | + (async/>!! producer-chan ["1" value])) |
| 247 | + (is (= input-values (into #{} (repeatedly 3 #(u/try-take! result-chan))))) |
| 248 | + (is (= input-values (into #{} (map #(String. ^"[B" %)) (repeatedly 3 #(u/try-take! consumer-chan))))) |
| 249 | + (finally |
| 250 | + (Thread/sleep 2000) |
| 251 | + (source/stop! source) |
| 252 | + (async/close! producer-chan) |
| 253 | + (sink/stop! sink)))) |
249 | 254 |
|
| 255 | + (testing "consumer decorator validation failure" |
| 256 | + (let [consumer-chan (async/chan 10) |
| 257 | + clicks-consumer-opts {:name "clicks-consumer" |
| 258 | + :brokers (kafka-setup/get-bootstrap-servers) |
| 259 | + :topic "clicks" |
| 260 | + :group-id "clicks-test-consumer" |
| 261 | + :auto-offset-reset "earliest" |
| 262 | + :shape :value |
| 263 | + :ketu.source/consumer-decorator (reify ConsumerDecorator |
| 264 | + (poll! [_ _ _] |
| 265 | + nil) |
| 266 | + (valid? [_ _] |
| 267 | + false))}] |
| 268 | + (is (thrown-with-msg? Exception #"Consumer decorator validation failed" |
| 269 | + (source/source consumer-chan clicks-consumer-opts))))))) |
0 commit comments