1 (in-package #:zmq.tests
)
10 (defparameter *test-endpoint
* "tcp://127.0.0.1:31723")
15 "Version should return"
16 (is (not (null (zmq:version
)))))
18 ;: Creating and configuring contexts
21 "Context should be creatable and destroyable."
22 (let ((ctx (zmq:ctx-new
)))
24 (is (zerop (zmq:ctx-destroy ctx
)))
25 (signals (zmq:zmq-error
26 "Destroyed context shouldn't be destroyable")
27 (zmq:ctx-destroy ctx
))))
30 "Context settings should be retrievable."
31 (zmq:with-context
(ctx)
32 (let ((ctx (zmq:ctx-new
)))
33 (is (= 1 (zmq:ctx-get ctx
:io-threads
)))
34 (is (= 1024 (zmq:ctx-get ctx
:max-sockets
)))
35 (signals (simple-error
36 "Invalid setting keywords should signal an error")
37 (zmq:ctx-get ctx
:invalid-stuff
)))))
40 "Context setting should be changeable."
41 (zmq:with-context
(ctx)
42 (is (zerop (zmq:ctx-set ctx
:io-threads
10)))
43 (is (= 10 (zmq:ctx-get ctx
:io-threads
))
44 "Setting settings should make getting return appropriately")
45 (signals (simple-error
46 "Invalid setting keywords should signal an error")
47 (zmq:ctx-set ctx
:invalid-stuff
10))))
49 (test ctx-abnormal-get-set
50 "Accesing settings of destroyed context should signal an error"
51 (let ((ctx (zmq:ctx-new
)))
53 (signals (zmq:zmq-error
)
54 (zmq:ctx-set ctx
:io-threads
10))
55 (signals (zmq:zmq-error
)
56 (zmq:ctx-get ctx
:max-sockets
))))
58 ;; Creating and configuring sockets
61 "Sockets should be creatable and closable"
62 (zmq:with-context
(ctx)
63 (let ((socket (zmq:socket ctx
:pub
)))
64 (is (not (null socket
)))
65 (is (zerop (zmq:close socket
)))
66 (signals (zmq:zmq-error
67 "Closed sockets can't be closed again")
68 (zmq:close socket
)))))
70 (test socket-abnormal-creation
71 "Abnormal socket creation should signal error."
72 (let ((ctx (zmq:ctx-new
)))
73 (signals (simple-error
74 "Creating socket with wrong type should signal error")
75 (zmq:socket ctx
:invalid-type
))
77 (signals (zmq:zmq-error
78 "Socket can't be created from closed context")
79 (zmq:socket ctx
:pub
))))
82 "Socket should be bindable and unbindable."
83 (zmq:with-context
(ctx)
84 (zmq:with-socket
(s ctx
:push
)
85 (is (zerop (zmq:bind s
*test-endpoint
*)))
86 (signals (zmq:zmq-error
87 "Can't bind to one endpoint twice")
88 (zerop (zmq:bind s
*test-endpoint
*)))
89 (is (zerop (zmq:unbind s
*test-endpoint
*)))
90 (signals (zmq:zmq-error
91 "Socket can not be unbound if not bound")
92 (zerop (zmq:unbind s
*test-endpoint
*))))))
95 "Socket should be able to connect and disconnect."
96 (zmq:with-context
(ctx)
97 (zmq:with-socket
(s ctx
:push
)
98 (is (zerop (zmq:connect s
*test-endpoint
*)))
99 (is (zerop (zmq:disconnect s
*test-endpoint
*)))
100 (signals (zmq:zmq-error
101 "Socket can not be connected if it is not")
102 (zerop (zmq:disconnect s
*test-endpoint
*))))))
105 "Socket settings should be retrievable."
106 (zmq:with-context
(ctx)
107 (zmq:with-socket
(s ctx
:pub
)
108 (is (= 0 (zmq:getsockopt s
:affinity
)))
109 (signals (simple-error
110 "Invalid setting keywords should signal an error.")
111 (zmq:getsockopt s
:invalid-stuff
)))))
114 "Socket settings should be settable."
115 (zmq:with-context
(ctx)
116 (zmq:with-socket
(s ctx
:pub
)
117 (is (zerop (zmq:setsockopt s
:affinity
10)))
118 (is (= 10 (zmq:getsockopt s
:affinity
)))
119 (signals (simple-error
120 "Invalid setting keywords should signal an error")
121 (zmq:setsockopt s
:invalid-stuff
10)))))
123 (test socket-get-set-string
124 "Socket settings that are strings should work too."
125 (zmq:with-context
(ctx)
126 (zmq:with-socket
(s ctx
:pub
)
127 (is (zerop (zmq:setsockopt s
:identity
"Foobar")))
128 (is (equal "Foobar" (zmq:getsockopt s
:identity
))))))
130 ;; Making and working with messages
133 "Calling make-msg without arguments yield empty message and it's really
135 (let ((msg (zmq:make-msg
)))
136 (is (zerop (zmq:msg-size msg
)))
137 (is (equalp #() (zmq:msg-data-as-array msg
)))
138 (is (equal "" (zmq:msg-data-as-string msg
)))))
140 (test make-msg-string
141 "Calling make-msg with string as :data should produce message with payload
142 matching the string."
143 (let* ((empty-msg (zmq:make-msg
:data
""))
144 (content "payload of message")
145 (content-vec (map 'vector
#'char-code content
))
146 (msg (zmq:make-msg
:data content
)))
147 (is (zerop (zmq:msg-size empty-msg
)))
148 (is (equalp #() (zmq:msg-data-as-array empty-msg
)))
149 (is (equal "" (zmq:msg-data-as-string empty-msg
)))
150 (is (= (length content
) (zmq:msg-size msg
)))
151 (is (equal content
(zmq:msg-data-as-string msg
)))
152 (is (equalp content-vec
(zmq:msg-data-as-array msg
)))))
154 (test make-msg-string-unicode
155 "Calling make-msg with unicode data should produce message with payload
157 (let ((msg (zmq:make-msg
:data
"ю")))
158 (is (= 2 (zmq:msg-size msg
)))
159 (is (equal "ю" (zmq:msg-data-as-string msg
)))))
162 "Calling make-msg with array as data should produce message with matching
164 (let ((msg (zmq:make-msg
:data
#(1 2 3 4))))
165 (is (= 4 (zmq:msg-size msg
)))
166 (is (equalp #(1 2 3 4) (zmq:msg-data-as-array msg
)))))
169 "Calling make-msg with size argument should produce message of appropriate
171 (let ((msg (zmq:make-msg
:size
10)))
172 (is (= 10 (zmq:msg-size msg
)))))
175 "Calling msg-copy should copy the payload of the source without disturbing
177 (let ((src-msg (zmq:make-msg
:data
#(1 2 3 4)))
178 (dst-msg (zmq:make-msg
)))
179 (zmq:msg-copy dst-msg src-msg
)
180 (is (= (zmq:msg-size src-msg
) (zmq:msg-size dst-msg
) 4))
181 (is (equalp (zmq:msg-data-as-array src-msg
)
182 (zmq:msg-data-as-array dst-msg
)))
183 (is (equalp #(1 2 3 4)
184 (zmq:msg-data-as-array src-msg
)))))
187 "Calling msg-move should copy the payload of the source while making original
189 (let ((src-msg (zmq:make-msg
:data
#(1 2 3 4)))
190 (dst-msg (zmq:make-msg
)))
191 (zmq:msg-move dst-msg src-msg
)
192 (is (equalp #() (zmq:msg-data-as-array src-msg
)))
193 (is (= 4 (zmq:msg-size dst-msg
)))
194 (is (equalp #(1 2 3 4)
195 (zmq:msg-data-as-array dst-msg
)))))