blob: afd998038e193738ac7a67d1ab6bbb07819fe7d3 [file] [log] [blame]
Tommy Carpenter3a6ac012020-04-06 14:42:57 -04001# vim: ts=4 sw=4 expandtab:
2# =================================================================================2
3# Copyright (c) 2019-2020 Nokia
4# Copyright (c) 2018-2020 AT&T Intellectual Property.
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17# ==================================================================================
18import time
19import pytest
20from ricxappframe.rmr import rmr, helpers, exceptions
21
22
23SIZE = 256
24MRC_SEND = None
25MRC_RCV = None
26
27
28def setup_module():
29 """
30 test_rmr module setup
31 """
32 global MRC_SEND
33 MRC_SEND = rmr.rmr_init(b"3562", rmr.RMR_MAX_RCV_BYTES, 0x00)
34 while rmr.rmr_ready(MRC_SEND) == 0:
35 time.sleep(1)
36
37 global MRC_RCV
38 MRC_RCV = rmr.rmr_init(b"3563", rmr.RMR_MAX_RCV_BYTES, 0x00)
39 while rmr.rmr_ready(MRC_RCV) == 0:
40 time.sleep(1)
41
42 global MRC_BUF_RCV
43 MRC_BUF_RCV = rmr.rmr_init(b"3564", rmr.RMR_MAX_RCV_BYTES, 0x02)
44 while rmr.rmr_ready(MRC_BUF_RCV) == 0:
45 time.sleep(1)
46
47
48def teardown_module():
49 """
50 test rmr module teardown
51 """
52 rmr.rmr_close(MRC_SEND)
53 rmr.rmr_close(MRC_RCV)
54
55
56def _assert_new_sbuf(sbuf):
57 """
58 verify the initial state of an alloced message is what we expect
59 """
60 summary = rmr.message_summary(sbuf)
61 assert summary["payload"] == b""
62 assert summary["payload length"] == 0
63 assert summary["subscription id"] == -1
64 assert summary["transaction id"] == b""
65 assert summary["message state"] == 0
66 assert summary["message status"] == "RMR_OK"
67 assert summary["meid"] == b""
68 assert summary["errno"] == 0
69
70
Tommy Carpenter3a6ac012020-04-06 14:42:57 -040071def test_meid():
72 """
73 test meid stringification
74 """
75 sbuf = rmr.rmr_alloc_msg(MRC_SEND, SIZE)
76
77 rmr.rmr_set_meid(sbuf, b"\x01\x02")
78 assert rmr.rmr_get_meid(sbuf) == rmr.message_summary(sbuf)["meid"] == b"\x01\x02"
79 assert len(rmr.rmr_get_meid(sbuf)) == 2
80
81 rmr.rmr_set_meid(sbuf, b"\x00" * 31)
82 assert rmr.rmr_get_meid(sbuf) == rmr.message_summary(sbuf)["meid"] == b"" # NULL bytes get truncated
83
84 rmr.rmr_set_meid(sbuf, b"6" * 31)
85 assert rmr.rmr_get_meid(sbuf) == rmr.message_summary(sbuf)["meid"] == b"6" * 31 # string in string out
86
87 rmr.rmr_set_meid(sbuf, b"\x01\x02")
88 assert (
89 rmr.rmr_get_meid(sbuf) == rmr.message_summary(sbuf)["meid"] == b"\x01\x02"
90 ) # Ctypes will chop at first nil, so expect only 2 bytes back
91
92 assert len(rmr.rmr_get_meid(sbuf)) == 2
93
94 # test that an exception is raised for buffers which are too long
95 with pytest.raises(exceptions.MeidSizeOutOfRange):
96 rmr.rmr_set_meid(sbuf, b"8" * 32)
97
98
99def test_rmr_set_get():
100 """
101 test set functions
102 """
103 sbuf = rmr.rmr_alloc_msg(MRC_SEND, SIZE)
104 _assert_new_sbuf(sbuf)
105
106 # test payload
107 pay = b"\x01\x00\x80"
108 rmr.set_payload_and_length(pay, sbuf)
109 summary = rmr.message_summary(sbuf)
110 assert summary["payload"] == pay
111 assert summary["payload length"] == 3
112
113 # test transid (note we cant test payload because it's randomly gen)
114 assert summary["transaction id"] == b""
115 assert len(summary["transaction id"]) == 0
116 rmr.generate_and_set_transaction_id(sbuf)
117 summary = rmr.message_summary(sbuf)
118 assert summary["transaction id"] != b""
119 assert len(summary["transaction id"]) == 32
120
121 # test meid
122 assert rmr.rmr_get_meid(sbuf) == summary["meid"] == b""
123 rmr.rmr_set_meid(sbuf, b"666\x01\x00\x01")
124 summary = rmr.message_summary(sbuf)
125 assert rmr.rmr_get_meid(sbuf) == summary["meid"] == b"666\x01"
126 assert (len(summary["meid"])) == 4
127
128
129def test_alloc_fancy():
130 """test allocation with setting payload, trans, mtype, subid"""
131 pay = b"yoo\x01\x00\x80"
132 sbuf = rmr.rmr_alloc_msg(MRC_SEND, SIZE, payload=pay, gen_transaction_id=True, mtype=14, meid=b"asdf", sub_id=654321)
133 summary = rmr.message_summary(sbuf)
134 assert summary["payload"] == pay
135 assert summary["payload length"] == 6
136 assert summary["transaction id"] != b"" # hard to test what it will be, but make sure not empty
137 assert len(summary["transaction id"]) == 32
138 assert summary["message state"] == 0
139 assert summary["message type"] == sbuf.contents.mtype == 14
140 assert rmr.rmr_get_meid(sbuf) == summary["meid"] == b"asdf"
141 assert sbuf.contents.sub_id == summary["subscription id"] == 654321
142
143
144def test_alloc_overlapping_flags():
145 """test allocation with setting the transaction id"""
146 sbuf = rmr.rmr_alloc_msg(MRC_SEND, SIZE, gen_transaction_id=True, fixed_transaction_id=b"6" * 32)
147 summary = rmr.message_summary(sbuf)
148 assert summary["transaction id"] == b"66666666666666666666666666666666"
149
150
151def test_rcv_timeout():
152 """
153 test torcv; this is a scary test because if it fails... it doesn't fail, it will run forever!
154 We receive a message (though nothing has been sent) and make sure the function doesn't block forever.
155
156 There is no unit test for rmr_rcv_msg; too dangerous, that is a blocking call that may never return.
157 """
158 sbuf_rcv = rmr.rmr_alloc_msg(MRC_RCV, SIZE)
159 sbuf_rcv = rmr.rmr_torcv_msg(MRC_RCV, sbuf_rcv, 50) # should time out after 50ms
160 summary = rmr.message_summary(sbuf_rcv)
161 assert summary["message state"] == 12
162 assert summary["message status"] == "RMR_ERR_TIMEOUT"
163
164
165def test_send_rcv():
166 """
167 test send and receive
168 """
169 pay = b"\x01\x00\x80"
170
171 # send a message
172 sbuf_send = rmr.rmr_alloc_msg(MRC_SEND, SIZE)
173 _assert_new_sbuf(sbuf_send)
174 rmr.set_payload_and_length(pay, sbuf_send)
175 sbuf_send.contents.mtype = 0
176 sbuf_send = rmr.rmr_send_msg(MRC_SEND, sbuf_send)
177 send_summary = rmr.message_summary(sbuf_send)
178 assert send_summary["message state"] == 0 # if send fails don't attempt receive
179 assert send_summary["message status"] == "RMR_OK"
180 time.sleep(0.5)
181
182 # receive it in other context
183 sbuf_rcv = rmr.rmr_alloc_msg(MRC_RCV, SIZE)
184 sbuf_rcv = rmr.rmr_torcv_msg(MRC_RCV, sbuf_rcv, 2000)
185 rcv_summary = rmr.message_summary(sbuf_rcv)
186 assert rcv_summary["message state"] == 0
187 assert rcv_summary["message status"] == "RMR_OK"
188 assert rcv_summary["message type"] == 0
189 assert rcv_summary["payload"] == pay
190
191 # send an ACK back
192 ack_pay = b"message received"
193 sbuf_rcv = rmr.rmr_rts_msg(MRC_RCV, sbuf_rcv, payload=ack_pay, mtype=6666)
194 rcv_ack_summary = rmr.message_summary(sbuf_rcv)
195
196 # have the sender receive it
197 sbuf_send = rmr.rmr_torcv_msg(MRC_SEND, sbuf_send, 2000)
198 send_ack_summary = rmr.message_summary(sbuf_send)
199
200 assert send_ack_summary["message state"] == rcv_ack_summary["message state"] == 0
201 assert send_ack_summary["message status"] == rcv_ack_summary["message status"] == "RMR_OK"
202 assert send_ack_summary["payload"] == ack_pay
203 assert send_ack_summary["message type"] == 6666
204
205
206def test_send_rcv_subid_good():
207 """
208 test send and receive where subid is used for routing
209 """
210 pay = b"\x01\x00\x80"
211 test_mtype = 46656
212 test_subid = 777
213
214 # send a message
215 sbuf_send = rmr.rmr_alloc_msg(MRC_SEND, 3, pay, mtype=test_mtype, sub_id=test_subid)
216 pre_send_summary = rmr.message_summary(sbuf_send)
217 sbuf_send = rmr.rmr_send_msg(MRC_SEND, sbuf_send)
218 send_summary = rmr.message_summary(sbuf_send)
219
220 # receive it in other context
221 time.sleep(0.5)
222 sbuf_rcv = rmr.rmr_alloc_msg(MRC_RCV, 3)
223 sbuf_rcv = rmr.rmr_torcv_msg(MRC_RCV, sbuf_rcv, 2000)
224 rcv_summary = rmr.message_summary(sbuf_rcv)
225
226 # asserts
227 assert send_summary["message state"] == rcv_summary["message state"] == 0
228 assert send_summary["message status"] == rcv_summary["message status"] == "RMR_OK"
229 assert pre_send_summary["payload"] == rcv_summary["payload"] == pay
230 assert pre_send_summary["message type"] == rcv_summary["message type"] == test_mtype
231 assert pre_send_summary["subscription id"] == rcv_summary["subscription id"] == test_subid
232
233
234def test_send_rcv_subid_bad_subid():
235 """
236 test send and receive where subid is used for routing but nobody recieves this subid
237 """
238 sbuf_send = rmr.rmr_alloc_msg(MRC_SEND, 3, b"\x01\x00\x80", mtype=46656, sub_id=778)
239 sbuf_send = rmr.rmr_send_msg(MRC_SEND, sbuf_send)
240 assert rmr.message_summary(sbuf_send)["message state"] == 2
241 assert rmr.message_summary(sbuf_send)["message status"] == "RMR_ERR_NOENDPT"
242
243
244def test_send_rcv_subid_bad_mtype():
245 """
246 test send and receive where subid is used for routing but nobody recieves this mtype
247 """
248 sbuf_send = rmr.rmr_alloc_msg(MRC_SEND, 3, b"\x01\x00\x80", mtype=46657, sub_id=777)
249 sbuf_send = rmr.rmr_send_msg(MRC_SEND, sbuf_send)
250 assert rmr.message_summary(sbuf_send)["message state"] == 2
251 assert rmr.message_summary(sbuf_send)["message status"] == "RMR_ERR_NOENDPT"
252
253
254def send_burst(mrc, fmt, mtype=1, num=13, counter=0):
255 """
256 Internal function to support test_rcv_all.
257 Send a burst of messages optionally giving the type, payload
258 and number to send.
259 """
260 sbuf_send = rmr.rmr_alloc_msg(MRC_SEND, SIZE) # seed message buffer
261
262 for i in range(num):
263 payload = bytes(fmt % counter, "UTF-8")
264 counter += 1
265
266 rmr.set_payload_and_length(payload, sbuf_send)
267 sbuf_send.contents.mtype = mtype
268
269 max_retries = 5
270 while max_retries > 0:
271 sbuf_send = rmr.rmr_send_msg(mrc, sbuf_send)
272 ms = rmr.message_summary(sbuf_send)
273 if ms["message state"] != 10: # 10 is retry
274 break
275 max_retries -= 1
276 time.sleep(0.75)
277
278 assert ms["message state"] == 0
279 assert max_retries > 0
280
281
282def test_rcv_all():
283 """
284 test the ability to receive a batch of queued messages.
285 """
286 pay_fmt = "send to ring msg: %d" # dynamic message format with counter
287
288 send_burst(MRC_SEND, pay_fmt) # send a bunch of 13 messages that should queue
289 time.sleep(1) # ensure underlying transport gets cycles to send/receive
290
291 bundle = helpers.rmr_rcvall_msgs(MRC_BUF_RCV) # use the buffered receiver to read all with a single call
292 assert len(bundle) == 13
293
294 for i, ms in enumerate(bundle):
295 ms = bundle[i] # validate each summary returned, and ordering preserved
296 assert ms["message state"] == 0
297 expected_pay = bytes(pay_fmt % i, "UTF-8")
298 assert ms["payload"] == expected_pay
299
300 send_burst(MRC_SEND, pay_fmt, mtype=1, num=10) # send a second round with msg types 1 and 2 to test filter
301 send_burst(MRC_SEND, pay_fmt, mtype=2, num=8)
302 send_burst(MRC_SEND, pay_fmt, mtype=1, num=5)
303 send_burst(MRC_SEND, pay_fmt, mtype=2, num=4, counter=8) # total of 12 messages with type 2 should be queued
304 time.sleep(1) # ensure underlying transport gets cycles to send/receive
305
306 bundle = helpers.rmr_rcvall_msgs_raw(MRC_BUF_RCV, [2]) # receive only message type 2 messages
307 assert len(bundle) == 12 # we should only get the second batch of 12 messages
308
309 for i, (ms, sbuf) in enumerate(bundle): # test the raw version
310 test_summary = rmr.message_summary(sbuf)
311 assert test_summary == ms
312 assert ms["message state"] == 0 # all should be OK
313 assert ms["message type"] == 2 # only mtype 2 should have been received
314 expected_pay = bytes(pay_fmt % i, "UTF-8") # ordering should still jive with the counter
315 assert ms["payload"] == expected_pay
316 rmr.rmr_free_msg(sbuf)
317
318
319def test_bad_buffer():
320 """test that we get a proper exception when the buffer has a null pointer"""
321 with pytest.raises(exceptions.BadBufferAllocation):
322 rmr.rmr_alloc_msg(None, 4096)
323
324
325def test_resize_payload():
326 """test the ability to insert a larger payload into an existing message"""
327 mtype = 99
328 subid = 100
329
330 mbuf = rmr.rmr_alloc_msg(MRC_SEND, 25) # allocate buffer with small payload
331 mbuf.contents.mtype = mtype # type and sub-id should not change
332 mbuf.contents.sub_id = subid
333
334 long_payload = b"This is a long payload that should force the message buffer to be reallocated"
335 rmr.set_payload_and_length(long_payload, mbuf)
336 summary = rmr.message_summary(mbuf)
337 assert summary["payload max size"] >= len(long_payload) # RMR may allocate a larger payload space
338 assert summary["payload length"] == len(long_payload) # however, the length must be exactly the same
339 assert summary["message type"] == mtype # both mtype and sub-id should be preserved in new
340 assert summary["subscription id"] == subid