/
echo.clj
83 lines (69 loc) · 2.69 KB
/
echo.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(ns md-clj.test.echo
(:require [md-clj.broker :only [start-broker] :as mdb]
[md-clj.worker :only [new-worker run] :as mdw]
[md-clj.client :only [new-client send! recv] :as mdc])
(:use clojure.test
re-rand)
(:import org.zeromq.ZMsg))
(defn random-strings-fixture
[f]
(def random-strings (repeatedly 1000 #(re-rand #"[A-Za-z0-9]{20}")))
(f)
())
(use-fixtures :once random-strings-fixture)
;; start our broker in a future
(future (mdb/start-broker "tcp://*:5555" false))
;; super simple echo test. the request data is copied verbatim back into the
;; the response, which is then tested.
(deftest echo-test
(let [echo-worker (mdw/new-worker
"echo" "tcp://localhost:5555"
(fn [request reply]
(doall (map #(.add reply %) (.toArray request)))))
echo-client (mdc/new-client "tcp://localhost:5555")]
(future (mdw/run echo-worker))
(time
(doseq [x random-strings]
(let [reply (mdc/send! echo-client "echo" x)]
(is (= x (-> (.toArray reply)
first
.getData
String.))))))))
;; test an async client. send! a bunch of request. then go back and get
;; all the results
(deftest echo-async-test
(let [echo-async-worker (mdw/new-worker
"echo-async" "tcp://localhost:5555"
(fn [request reply]
(doall (map #(.add reply %) (.toArray request)))))
echo-async-client (mdc/new-client "tcp://localhost:5555" true)]
(future (mdw/run echo-async-worker))
(doseq [x random-strings]
(let [request (ZMsg.)
_ (.addString request x)]
(mdc/send! echo-async-client "echo-async" request)))
(doseq [x random-strings]
(let [reply (mdc/recv echo-async-client)]
(is (= x (-> (.toArray reply)
first
.getData
String.)))))))
(deftest echo-multi-test
(let [echo-workers (repeat 10
(mdw/new-worker
"echo-multi"
"tcp://localhost:5555"
(fn [request reply]
;;(Thread/sleep 500)
(doall (map #(.add reply %)
(.toArray request))))))
echo-client (mdc/new-client "tcp://localhost:5555")]
(doseq [w echo-workers]
(future (mdw/run w)))
(time
(doseq [x random-strings]
(let [reply (mdc/send! echo-client "echo-multi" x)]
(is (= x (-> (.toArray reply)
first
.getData
String.))))))))