Message creation and mangling tests.
[cl-zmq.git] / tests / main.lisp
blobea6d5707664d9fb06636d8b1e68623bd8922e720
1 (in-package #:zmq.tests)
3 (defun run-tests ()
4 (run! 'main))
6 (def-suite main)
8 (in-suite main)
10 (defparameter *test-endpoint* "tcp://127.0.0.1:31723")
12 ;; Version
14 (test version
15 "Version should return"
16 (is (not (null (zmq:version)))))
18 ;: Creating and configuring contexts
20 (test ctx
21 "Context should be creatable and destroyable."
22 (let ((ctx (zmq:ctx-new)))
23 (is (not (null ctx)))
24 (is (zerop (zmq:ctx-destroy ctx)))
25 (signals (zmq:zmq-error
26 "Destroyed context shouldn't be destroyable")
27 (zmq:ctx-destroy ctx))))
29 (test ctx-get
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 (zmq:zmq-error
36 "Invalid setting keywords should signal an error")
37 (zmq:ctx-get ctx :invalid-stuff)))))
39 (test ctx-set
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 (zmq:zmq-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)))
52 (zmq:ctx-destroy ctx)
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
60 (test socket
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 (zmq:zmq-error
74 "Creating socket with wrong type should signal error")
75 (zmq:socket ctx :invalid-type))
76 (zmq:ctx-destroy ctx)
77 (signals (zmq:zmq-error
78 "Socket can't be created from closed context")
79 (zmq:socket ctx :pub))))
81 (test socket-bind
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*))))))
94 (test socket-connect
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*))))))
104 (test socket-get
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 (zmq:zmq-error
110 "Invalid setting keywords should signal an error.")
111 (zmq:getsockopt s :invalid-stuff)))))
113 (test socket-set
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 (zmq:zmq-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
132 (test make-msg-empty
133 "Calling make-msg without arguments yield empty message and it's really
134 empty."
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
156 encoded correctly."
157 (let ((msg (zmq:make-msg :data "ю")))
158 (is (= 2 (zmq:msg-size msg)))
159 (is (equal "ю" (zmq:msg-data-as-string msg)))))
161 (test make-msg-array
162 "Calling make-msg with array as data should produce message with matching
163 payload."
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)))))
168 (test make-msg-size
169 "Calling make-msg with size argument should produce message of appropriate
170 size."
171 (let ((msg (zmq:make-msg :size 10)))
172 (is (= 10 (zmq:msg-size msg)))))
174 (test msg-copy
175 "Calling msg-copy should copy the payload of the source without disturbing
176 the original."
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)))))
186 (test msg-move
187 "Calling msg-move should copy the payload of the source while making original
188 an empty message."
189 (let ((src-msg (zmq:make-msg :data #(1 2 3 4)))
190 (dst-msg (zmq:make-msg)))
191 (zmq:msg-copy 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)))))