Add writeback call to objdb
[openais.git] / test / testevt.c
blob9bf55d17ec17df5972de8442068f38fa01a70360
1 /*
2 * Copyright (c) 2004-2005 Mark Haverkamp
3 * Copyright (c) 2004-2005 Open Source Development Lab
5 * All rights reserved.
7 * This software licensed under BSD license, the text of which follows:
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * - Neither the name of the Open Source Development Lab nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
35 * Test program for event service
37 * test_initialize ();
38 * Version check in saEvtInitialze()
40 * test_channel();
41 * Test basic channel operations. Open/close/subscribe/unsubscribe
43 * test_event();
44 * Event operations: allocation, free, setting and getting
45 * attributes, publishing.
47 * test_multi_channel1();
48 * test_multi_channel1();
49 * test_multi_channel1();
50 * Test events on multiple channels and multiple subscriptions.
52 * test_retention();
53 * Test event retention times.
55 * test_unlink_channel();
56 * Test event channel unlink.
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 #include <sys/poll.h>
64 #include <stdlib.h>
65 #include "saAis.h"
66 #include "saEvt.h"
68 extern int get_sa_error(SaAisErrorT, char *, int);
69 char result_buf[256];
70 int result_buf_len = sizeof(result_buf);
71 #define TRY_WAIT 2
74 void testresult (SaAisErrorT result, SaAisErrorT expected, int test_no)
76 if (result == expected) {
77 printf ("Test %d passed\n", test_no);
78 } else {
79 get_sa_error(result, result_buf, result_buf_len);
80 printf ("ERROR: Test %d FAILED (expected %d got %s)\n",
81 test_no, expected, result_buf);
85 SaVersionT version1 = { 'B', 0x01, 0x01 };
86 SaVersionT version2 = { 'A', 0x01, 0x01 };
87 SaVersionT version3 = { 'B', 0x02, 0x01 };
88 SaVersionT version4 = { 'B', 0x01, 0xff };
89 SaVersionT version5 = { 'C', 0xff, 0xff };
91 struct version_test {
92 SaVersionT *version;
93 SaAisErrorT result;
96 struct version_test versions[] = {
97 { &version1, SA_AIS_OK },
98 { &version2, SA_AIS_ERR_VERSION },
99 { &version3, SA_AIS_ERR_VERSION },
100 { &version4, SA_AIS_OK},
101 { &version5, SA_AIS_ERR_VERSION},
102 { 0, SA_AIS_ERR_INVALID_PARAM}
105 int version_size = sizeof(versions) / sizeof(struct version_test);
107 void open_callback(SaInvocationT invocation,
108 SaEvtChannelHandleT channelHandle,
109 SaAisErrorT error);
110 void event_callback(SaEvtSubscriptionIdT subscriptionId,
111 const SaEvtEventHandleT eventHandle,
112 const SaSizeT eventDataSize);
114 SaEvtCallbacksT callbacks = {
115 open_callback,
116 event_callback
119 char channel[256] = "TESTEVT_CHANNEL";
120 char unlink_channel[256] = "TESTEVT_UNLINK_CHANNEL";
121 SaEvtSubscriptionIdT subscription_id = 0xabcdef;
122 SaInvocationT open_invocation = 0xaa55cc33;
123 unsigned long long test_ret_time = 30000000000ULL; /* 30 seconds */
127 * event data
130 long *exp_data;
131 #define DATA_SIZE 2048 /* default data size */
132 #define LCOUNT DATA_SIZE/sizeof(long)
135 * Test saEvtInitialize and version checking.
137 void test_initialize (void) {
138 SaAisErrorT result;
139 SaEvtHandleT handle;
140 int i;
144 * version check tests
146 printf("Test lib version check on initlialize\n");
147 for (i=0; i < version_size; i++) {
148 do {
149 result = saEvtInitialize (&handle, 0, versions[i].version);
150 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
152 testresult (result, versions[i].result, i);
153 if (result == SA_AIS_OK) {
154 saEvtFinalize(handle);
161 #define _patt1 "Filter pattern 1"
162 #define patt1 (SaUint8T *) _patt1
163 #define patt1_size sizeof(_patt1)
165 #define _patt2 "Filter pattern 2"
166 #define patt2 (SaUint8T *) _patt2
167 #define patt2_size sizeof(_patt2)
169 #define _patt3 "Filter pattern 3"
170 #define patt3 (SaUint8T *) _patt3
171 #define patt3_size sizeof(_patt3)
173 #define _patt4 "Filter pattern 4"
174 #define patt4 (SaUint8T *) _patt4
175 #define patt4_size sizeof(_patt4)
178 SaEvtEventFilterT filters[] = {
179 {SA_EVT_PREFIX_FILTER, {patt1_size, patt1_size, patt1}},
180 {SA_EVT_SUFFIX_FILTER, {patt2_size, patt2_size, patt2}},
181 {SA_EVT_EXACT_FILTER, {patt3_size, patt3_size, patt3}},
182 {SA_EVT_PASS_ALL_FILTER, {patt4_size, patt4_size, patt4}}
185 SaEvtEventFilterArrayT subscribe_filters = {
186 sizeof(filters)/sizeof(SaEvtEventFilterT),
187 filters
191 * Process the open callback
193 void open_callback(SaInvocationT invocation,
194 SaEvtChannelHandleT channel_handle,
195 SaAisErrorT error)
197 SaAisErrorT result;
199 printf(" Received open channel callback\n");
200 if (error != SA_AIS_OK) {
201 get_sa_error(error, result_buf, result_buf_len);
202 printf("ERROR: async channel open result: %s\n", result_buf);
203 return;
205 if (invocation != open_invocation) {
206 printf("ERROR: Unexpected invocation value: e 0x%llx, a 0x%llx\n",
207 (long long)open_invocation, (long long)invocation);
210 printf(" Close async channel:\n");
211 do {
212 result = saEvtChannelClose(channel_handle);
213 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
215 if (result != SA_AIS_OK) {
216 get_sa_error(result, result_buf, result_buf_len);
217 printf("ERROR: channel close result: %s\n", result_buf);
218 return;
222 * Test channel operations.
223 * 1. Open a channel.
224 * 2. Close a channel.
225 * 3. Channel subscription.
226 * 4. Channel unsubscribe
227 * 5. unsubscribe/finalize with no channel close.
228 * 6. Multiple subscriptions.
229 * 7. Duplicate subscription ID.
230 * 8. unsubscribe non-exsistent subscription ID.
231 * 9. Open a channel async.
234 void
235 test_channel()
237 SaEvtHandleT handle;
238 SaEvtChannelHandleT channel_handle;
239 SaEvtChannelOpenFlagsT flags;
240 SaNameT channel_name;
241 SaAisErrorT result;
243 struct pollfd pfd;
244 int nfd;
245 SaSelectionObjectT fd;
246 int timeout = 5000;
248 flags = SA_EVT_CHANNEL_PUBLISHER |
249 SA_EVT_CHANNEL_SUBSCRIBER |
250 SA_EVT_CHANNEL_CREATE;
251 strcpy((char *)channel_name.value, channel);
252 channel_name.length = strlen(channel);
254 * Channel open/close test
256 printf("Test Channel operations:\n");
258 printf(" Channel open:\n");
259 do {
260 result = saEvtInitialize (&handle,
261 &callbacks, versions[0].version);
262 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
264 if (result != SA_AIS_OK) {
265 get_sa_error(result, result_buf, result_buf_len);
266 printf("ERROR: Event Initialize result: %s\n", result_buf);
267 return;
270 do {
271 result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
272 &channel_handle);
273 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
276 if (result != SA_AIS_OK) {
277 get_sa_error(result, result_buf, result_buf_len);
278 printf("ERROR: channel open result: %s\n", result_buf);
279 do {
280 result = saEvtFinalize(handle);
281 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
282 if (result != SA_AIS_OK) {
283 get_sa_error(result, result_buf, result_buf_len);
284 printf("ERROR: Event Finalize result: %s\n", result_buf);
286 return;
289 printf(" Channel close:\n");
290 do {
291 result = saEvtChannelClose(channel_handle);
292 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
294 if (result != SA_AIS_OK) {
295 get_sa_error(result, result_buf, result_buf_len);
296 printf("ERROR: channel close result: %s\n", result_buf);
297 return;
300 do {
301 result = saEvtFinalize(handle);
302 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
304 if (result != SA_AIS_OK) {
305 get_sa_error(result, result_buf, result_buf_len);
306 printf("ERROR: Event Finalize result: %s\n", result_buf);
307 return;
311 * Test channel subscribe
313 printf(" Channel subscribe:\n");
314 do {
315 result = saEvtInitialize (&handle, &callbacks,
316 versions[0].version);
317 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
318 if (result != SA_AIS_OK) {
319 get_sa_error(result, result_buf, result_buf_len);
320 printf("ERROR: Event Initialize result: %s\n", result_buf);
321 return;
323 do {
324 result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
325 &channel_handle);
326 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
327 if (result != SA_AIS_OK) {
328 get_sa_error(result, result_buf, result_buf_len);
329 printf("ERROR: channel open result: %s\n", result_buf);
330 do {
331 result = saEvtFinalize(handle);
332 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
333 if (result != SA_AIS_OK) {
334 get_sa_error(result, result_buf, result_buf_len);
335 printf("ERROR: Finalize result: %s\n", result_buf);
337 return;
340 do {
341 result = saEvtEventSubscribe(channel_handle,
342 &subscribe_filters,
343 subscription_id);
344 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
346 if (result != SA_AIS_OK) {
347 get_sa_error(result, result_buf, result_buf_len);
348 printf("ERROR: event subscribe result: %s\n", result_buf);
349 do {
350 result = saEvtChannelClose(channel_handle);
351 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
352 if (result != SA_AIS_OK) {
353 get_sa_error(result, result_buf, result_buf_len);
354 printf("ERROR: Channel close result: %s\n", result_buf);
356 do {
357 result = saEvtFinalize(handle);
358 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
359 if (result != SA_AIS_OK) {
360 get_sa_error(result, result_buf, result_buf_len);
361 printf("ERROR: Finalize result: %s\n", result_buf);
363 return;
367 printf(" Channel unsubscribe:\n");
369 do {
370 result = saEvtEventUnsubscribe(channel_handle, subscription_id);
371 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
372 if (result != SA_AIS_OK) {
373 get_sa_error(result, result_buf, result_buf_len);
374 printf("ERROR: event unsubscribe result: %s\n", result_buf);
375 do {
376 result = saEvtChannelClose(channel_handle);
377 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
378 if (result != SA_AIS_OK) {
379 get_sa_error(result, result_buf, result_buf_len);
380 printf("ERROR: Channel close result: %s\n", result_buf);
382 do {
383 result = saEvtFinalize(handle);
384 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
385 if (result != SA_AIS_OK) {
386 get_sa_error(result, result_buf, result_buf_len);
387 printf("ERROR: Finalize result: %s\n", result_buf);
389 return;
391 do {
392 result = saEvtChannelClose(channel_handle);
393 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
394 if (result != SA_AIS_OK) {
395 get_sa_error(result, result_buf, result_buf_len);
396 printf("ERROR: Channel close result: %s\n", result_buf);
398 do {
399 result = saEvtFinalize(handle);
400 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
401 if (result != SA_AIS_OK) {
402 get_sa_error(result, result_buf, result_buf_len);
403 printf("ERROR: Finalize result: %s\n", result_buf);
407 * Test channel subscribe with no close
409 printf(" Channel subscribe with no close at end:\n");
410 do {
411 result = saEvtInitialize (&handle, &callbacks,
412 versions[0].version);
413 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
414 if (result != SA_AIS_OK) {
415 get_sa_error(result, result_buf, result_buf_len);
416 printf("ERROR: Event Initialize result: %s\n", result_buf);
417 return;
419 do {
420 result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
421 &channel_handle);
422 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
423 if (result != SA_AIS_OK) {
424 get_sa_error(result, result_buf, result_buf_len);
425 printf("ERROR: channel open result: %s\n", result_buf);
426 result = saEvtFinalize(handle);
427 return;
430 do {
431 result = saEvtEventSubscribe(channel_handle,
432 &subscribe_filters,
433 subscription_id);
434 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
436 if (result != SA_AIS_OK) {
437 get_sa_error(result, result_buf, result_buf_len);
438 printf("ERROR: event subscribe result: %s\n", result_buf);
439 result = saEvtChannelClose(channel_handle);
442 do {
443 result = saEvtFinalize(handle);
444 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
445 if (result != SA_AIS_OK) {
446 get_sa_error(result, result_buf, result_buf_len);
447 printf("ERROR: Finalize failed\n");
448 return;
452 * Test multiple subscriptions
454 printf(" Multiple subscriptions\n");
455 do {
456 result = saEvtInitialize (&handle, &callbacks,
457 versions[0].version);
458 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
459 if (result != SA_AIS_OK) {
460 get_sa_error(result, result_buf, result_buf_len);
461 printf("ERROR: Event Initialize result: %s\n", result_buf);
462 return;
464 do {
465 result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
466 &channel_handle);
467 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
468 if (result != SA_AIS_OK) {
469 get_sa_error(result, result_buf, result_buf_len);
470 printf("ERROR: channel open result: %s\n", result_buf);
471 do {
472 result = saEvtFinalize(handle);
473 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
474 if (result != SA_AIS_OK) {
475 get_sa_error(result, result_buf, result_buf_len);
476 printf("ERROR: Finalize result: %s\n", result_buf);
478 return;
481 do {
482 result = saEvtEventSubscribe(channel_handle,
483 &subscribe_filters,
484 subscription_id);
485 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
487 if (result != SA_AIS_OK) {
488 get_sa_error(result, result_buf, result_buf_len);
489 printf("ERROR: First event subscribe result: %s\n", result_buf);
490 do {
491 result = saEvtChannelClose(channel_handle);
492 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
493 if (result != SA_AIS_OK) {
494 get_sa_error(result, result_buf, result_buf_len);
495 printf("ERROR: Channel close result: %s\n", result_buf);
497 do {
498 result = saEvtFinalize(handle);
499 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
500 if (result != SA_AIS_OK) {
501 get_sa_error(result, result_buf, result_buf_len);
502 printf("ERROR: Finalize result: %s\n", result_buf);
504 return;
507 do {
508 result = saEvtEventSubscribe(channel_handle,
509 &subscribe_filters,
510 subscription_id+1);
511 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
513 if (result != SA_AIS_OK) {
514 get_sa_error(result, result_buf, result_buf_len);
515 printf("ERROR: second event subscribe result: %s\n", result_buf);
516 do {
517 result = saEvtChannelClose(channel_handle);
518 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
519 if (result != SA_AIS_OK) {
520 get_sa_error(result, result_buf, result_buf_len);
521 printf("ERROR: Channel close result: %s\n", result_buf);
523 do {
524 result = saEvtFinalize(handle);
525 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
526 if (result != SA_AIS_OK) {
527 get_sa_error(result, result_buf, result_buf_len);
528 printf("ERROR: Finalize result: %s\n", result_buf);
530 return;
534 * Test duplicate subscription
536 printf(" Duplicate subscription\n");
538 do {
539 result = saEvtEventSubscribe(channel_handle,
540 &subscribe_filters,
541 subscription_id);
542 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
544 if (result != SA_AIS_ERR_EXIST) {
545 get_sa_error(result, result_buf, result_buf_len);
546 printf("ERROR: First event subscribe result: %s\n", result_buf);
547 result = saEvtChannelClose(channel_handle);
548 result = saEvtFinalize(handle);
549 return;
553 * Test unsubscribe non-existent sub_id
555 printf(" Unsubscribe non-existent sub_id\n");
557 do {
558 result = saEvtEventUnsubscribe(channel_handle,
559 subscription_id+2);
560 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
561 if (result != SA_AIS_ERR_NOT_EXIST) {
562 get_sa_error(result, result_buf, result_buf_len);
563 printf("ERROR: event unsubscribe result: %s\n", result_buf);
564 do {
565 result = saEvtChannelClose(channel_handle);
566 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
567 if (result != SA_AIS_OK) {
568 get_sa_error(result, result_buf, result_buf_len);
569 printf("ERROR: Channel close result: %s\n", result_buf);
571 do {
572 result = saEvtFinalize(handle);
573 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
574 if (result != SA_AIS_OK) {
575 get_sa_error(result, result_buf, result_buf_len);
576 printf("ERROR: Finalize result: %s\n", result_buf);
578 return;
581 do {
582 result = saEvtEventUnsubscribe(channel_handle, subscription_id);
583 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
584 if (result != SA_AIS_OK) {
585 get_sa_error(result, result_buf, result_buf_len);
586 printf("ERROR: first event unsubscribe result: %s\n", result_buf);
588 do {
589 result = saEvtEventUnsubscribe(channel_handle,
590 subscription_id+1);
591 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
592 if (result != SA_AIS_OK) {
593 get_sa_error(result, result_buf, result_buf_len);
594 printf("ERROR: second event unsubscribe result: %s\n", result_buf);
596 do {
597 result = saEvtChannelClose(channel_handle);
598 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
599 if (result != SA_AIS_OK) {
600 get_sa_error(result, result_buf, result_buf_len);
601 printf("ERROR: Channel close result: %s\n", result_buf);
603 do {
604 result = saEvtFinalize(handle);
605 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
606 if (result != SA_AIS_OK) {
607 get_sa_error(result, result_buf, result_buf_len);
608 printf("ERROR: Finalize result: %s\n", result_buf);
612 * Test opening a channel async.
614 printf(" Channel open async:\n");
615 do {
616 result = saEvtInitialize (&handle, &callbacks,
617 versions[0].version);
618 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
620 if (result != SA_AIS_OK) {
621 get_sa_error(result, result_buf, result_buf_len);
622 printf("ERROR: Event Initialize result: %s\n", result_buf);
623 return;
626 do {
627 result = saEvtChannelOpenAsync(handle, open_invocation,
628 &channel_name, flags);
629 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
632 if (result != SA_AIS_OK) {
633 get_sa_error(result, result_buf, result_buf_len);
634 printf("ERROR: channel open async result: %s\n", result_buf);
635 do {
636 result = saEvtFinalize(handle);
637 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
638 if (result != SA_AIS_OK) {
639 get_sa_error(result, result_buf, result_buf_len);
640 printf("ERROR: Event Finalize result: %s\n", result_buf);
642 return;
645 * See if we got the open callback
647 do {
648 result = saEvtSelectionObjectGet(handle, &fd);
649 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
650 if (result != SA_AIS_OK) {
651 get_sa_error(result, result_buf, result_buf_len);
652 printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
653 /* error */
654 return;
656 pfd.fd = fd;
657 pfd.events = POLLIN;
658 nfd = poll(&pfd, 1, timeout);
659 if (nfd <= 0) {
660 printf("ERROR: poll fds %d\n", nfd);
661 if (nfd < 0) {
662 perror("ERROR: poll error");
664 /* Error */
665 return;
668 do {
669 result = saEvtDispatch(handle, SA_DISPATCH_ONE);
670 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
671 if (result != SA_AIS_OK) {
672 get_sa_error(result, result_buf, result_buf_len);
673 printf("ERROR: saEvtDispatch %s\n", result_buf);
674 /* error */
675 return;
680 do {
681 result = saEvtFinalize(handle);
682 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
684 if (result != SA_AIS_OK) {
685 get_sa_error(result, result_buf, result_buf_len);
686 printf("ERROR: Event Finalize result: %s\n", result_buf);
687 return;
692 printf("Done\n");
696 SaUint8T pat0[100];
697 SaUint8T pat1[100];
698 SaUint8T pat2[100];
699 SaUint8T pat3[100];
700 SaUint8T pat4[100];
701 SaEvtEventPatternT evt_patts[5] = {
702 {100, 100, pat0},
703 {100, 100, pat1},
704 {100, 100, pat2},
705 {100, 100, pat3},
706 {100, 100, pat4}};
707 SaEvtEventPatternArrayT evt_pat_get_array = { 5, 0, evt_patts };
709 SaEvtEventPatternT patterns[] = {
710 {patt1_size, patt1_size, patt1},
711 {patt2_size, patt2_size, patt2},
712 {patt3_size, patt3_size, patt3},
713 {patt4_size, patt4_size, patt4}
715 SaNameT test_pub_name = {13, "Test Pub Name"};
716 #define TEST_PRIORITY 2
718 SaEvtEventPatternArrayT evt_pat_set_array = {
719 sizeof(patterns)/sizeof(SaEvtEventPatternT),
720 sizeof(patterns)/sizeof(SaEvtEventPatternT),
721 patterns
724 char event_data[1000];
725 #define EVENT_DATA_SIZE 1000
727 SaEvtEventIdT event_id = 0;
728 SaUint8T priority;
729 SaTimeT retention_time = 0ULL;
730 SaNameT publisher_name = {0, {0}};
731 SaSizeT event_data_size = 0;
732 int expected_pat_count;
735 * Test event operations.
737 * 1. Event allocation
738 * 2. Get event attributes (no pointers).
739 * 3. Get event attributes with pointers.
740 * 4. Set/Get event attributes.
741 * 5. Get event user data no pointer or count
742 * 6. Get event user data with pointer and no count
743 * 7. Get event user data with no pointer with a count.
744 * 8. Get event user data with pointer and count.
745 * 9. Get event user data woth a short count.
746 * 10. Free event.
747 * 11. Publish with no set patterns.
748 * 12. Publish with set patterns and event user data.
751 void
752 event_callback(SaEvtSubscriptionIdT my_subscription_id,
753 const SaEvtEventHandleT event_handle,
754 const SaSizeT my_event_data_size)
756 SaAisErrorT result;
757 SaUint8T my_priority;
758 SaTimeT my_retention_time;
759 SaNameT my_publisher_name = {0, {0}};
760 SaTimeT my_publish_time;
761 SaEvtEventIdT my_event_id;
762 int i;
763 long *act_data;
764 SaSizeT data_size;
766 printf(" event_callback called\n");
767 if (my_subscription_id != subscription_id) {
768 printf("ERROR: sub ID: e=%x, a=%x\n",
769 subscription_id, my_subscription_id);
771 if (my_event_data_size != event_data_size) {
772 printf("ERROR: event data size e=%llu, a=%llu\n",
773 (unsigned long long)event_data_size,
774 (unsigned long long)my_event_data_size);
777 evt_pat_get_array.patternsNumber = 4;
778 do {
779 result = saEvtEventAttributesGet(event_handle,
780 &evt_pat_get_array, /* patterns */
781 &my_priority, /* priority */
782 &my_retention_time, /* retention time */
783 &my_publisher_name, /* publisher name */
784 &my_publish_time, /* publish time */
785 &my_event_id /* event_id */
787 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
788 if (result != SA_AIS_OK) {
789 get_sa_error(result, result_buf, result_buf_len);
790 printf("ERROR: event get attr result(2): %s\n", result_buf);
791 goto evt_free;
794 if (my_event_id != event_id) {
795 printf("ERROR: Call back event ID error: e=%llx, a=%llx\n",
796 (unsigned long long)event_id, (unsigned long long)my_event_id);
799 if (evt_pat_get_array.patternsNumber != expected_pat_count) {
800 printf("ERROR: pattern array count not %d: %llu\n",
801 expected_pat_count,
802 (unsigned long long)evt_pat_get_array.patternsNumber);
805 if (expected_pat_count == 0) {
806 goto evt_free;
809 for (i = 0; i < evt_pat_get_array.patternsNumber; i++) {
810 if (evt_pat_get_array.patterns[i].patternSize !=
811 evt_pat_set_array.patterns[i].patternSize) {
812 printf("ERROR: pattern %d count not equal g=%llu, s=%llu\n",
814 (unsigned long long)evt_pat_get_array.patterns[i].patternSize,
815 (unsigned long long)evt_pat_set_array.patterns[i].patternSize);
816 printf("ERROR: pattern %d content g=\"%s\", s=\"%s\"\n",
818 evt_pat_get_array.patterns[i].pattern,
819 evt_pat_set_array.patterns[i].pattern);
820 } else {
821 if (memcmp(evt_pat_get_array.patterns[i].pattern,
822 evt_pat_set_array.patterns[i].pattern,
823 evt_pat_get_array.patterns[i].patternSize) != 0){
824 printf(
825 "ERROR: pattern %d don't match g=\"%s\", s=\"%s\"\n",
827 evt_pat_get_array.patterns[i].pattern,
828 evt_pat_set_array.patterns[i].pattern);
833 if (priority != my_priority) {
834 printf("ERROR: priority: e=0x%x a=0x%x\n",
835 priority, my_priority);
837 if (retention_time != my_retention_time) {
838 printf("ERROR: retention: e=0x%llx a=0x%llx\n",
839 (unsigned long long)retention_time,
840 (unsigned long long)my_retention_time);
842 if (publisher_name.length != my_publisher_name.length) {
843 printf("ERROR: publisher name length: e=%d, a=%d\n",
844 publisher_name.length,
845 my_publisher_name.length);
846 } else {
847 if (memcmp(publisher_name.value, my_publisher_name.value,
848 publisher_name.length) != 0) {
849 printf("ERROR: publisher name content: e=%s, a=%s\n",
850 publisher_name.value,
851 my_publisher_name.value);
855 act_data = malloc(my_event_data_size);
856 memset(act_data, 0, my_event_data_size);
857 data_size = my_event_data_size;
858 do {
859 result = saEvtEventDataGet(event_handle, act_data, &data_size);
860 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
861 if (result != SA_AIS_OK) {
862 get_sa_error(result, result_buf, result_buf_len);
863 printf("ERROR: event data get result: %s\n", result_buf);
864 goto dat_free;
866 if (data_size != event_data_size) {
867 printf("ERROR: Data size: e=%llu a=%llu\n",
868 (unsigned long long)event_data_size,
869 (unsigned long long)data_size);
871 for (i = 0; i < (data_size/sizeof(long)); i++) {
872 if (act_data[i] != exp_data[i]) {
873 printf("ERROR: Event Data e=%lx a=%lx at index %d\n",
874 exp_data[i], act_data[i], i);
875 break;
879 dat_free:
880 free(act_data);
881 evt_free:
882 do {
883 result = saEvtEventFree(event_handle);
884 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
885 if (result != SA_AIS_OK) {
886 get_sa_error(result, result_buf, result_buf_len);
887 printf("ERROR: event free result: %s\n", result_buf);
891 void
892 test_event()
894 SaEvtHandleT handle;
895 SaEvtChannelHandleT channel_handle;
896 SaEvtEventHandleT event_handle;
897 SaEvtChannelOpenFlagsT flags;
898 SaNameT channel_name;
900 SaTimeT publish_time;
901 struct pollfd pfd;
902 int nfd;
903 SaSelectionObjectT fd;
904 int timeout = 5000;
908 SaAisErrorT result;
910 int i;
912 flags = SA_EVT_CHANNEL_PUBLISHER|SA_EVT_CHANNEL_SUBSCRIBER |
913 SA_EVT_CHANNEL_CREATE;
914 strcpy((char *)channel_name.value, channel);
915 channel_name.length = strlen(channel);
917 printf("Test Event operations:\n");
919 do {
920 result = saEvtInitialize (&handle, &callbacks, versions[0].version);
921 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
922 if (result != SA_AIS_OK) {
923 get_sa_error(result, result_buf, result_buf_len);
924 printf("ERROR: Event Initialize result: %s\n", result_buf);
925 return;
927 do {
928 result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
929 &channel_handle);
930 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
931 if (result != SA_AIS_OK) {
932 get_sa_error(result, result_buf, result_buf_len);
933 printf("ERROR: channel open result: %s\n", result_buf);
934 goto evt_fin;
938 * Allocate an event
940 printf(" Event allocation\n");
941 do {
942 result = saEvtEventAllocate(channel_handle, &event_handle);
943 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
944 if (result != SA_AIS_OK) {
945 get_sa_error(result, result_buf, result_buf_len);
946 printf("ERROR: event Allocate result: %s\n", result_buf);
947 goto evt_close;
950 printf(" Get event attributes(1)\n");
951 do {
952 result = saEvtEventAttributesGet(event_handle,
953 0, /* patterns */
954 0, /* priority */
955 0, /* retention time */
956 0, /* publisher name */
957 0, /* publish time */
958 0 /* event_id */
960 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
961 if (result != SA_AIS_OK) {
962 get_sa_error(result, result_buf, result_buf_len);
963 printf("ERROR: event get attr result(1): %s\n", result_buf);
964 goto evt_free;
968 * Get event attributes, this time supply pointers.
969 * validate the default values.
971 printf(" Get event attributes(2)\n");
972 evt_pat_get_array.patternsNumber = 4;
973 do {
974 result = saEvtEventAttributesGet(event_handle,
975 &evt_pat_get_array, /* patterns */
976 &priority, /* priority */
977 &retention_time, /* retention time */
978 &publisher_name, /* publisher name */
979 &publish_time, /* publish time */
980 &event_id /* event_id */
982 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
983 if (result != SA_AIS_OK) {
984 get_sa_error(result, result_buf, result_buf_len);
985 printf("ERROR: event get attr result(2): %s\n", result_buf);
986 goto evt_free;
988 if (evt_pat_get_array.patternsNumber != 0) {
989 printf("ERROR: pattern array count not zero: %llu\n",
990 (unsigned long long)evt_pat_get_array.patternsNumber);
992 if (priority != SA_EVT_LOWEST_PRIORITY) {
993 printf("ERROR: priority not lowest: 0x%x\n", priority);
995 if (retention_time != 0) {
996 printf("ERROR: retention time not zero: %0llx\n",
997 (unsigned long long)retention_time);
999 if (publisher_name.length != 0) {
1000 printf("ERROR: publisher name not null: %s\n", publisher_name.value);
1002 if (event_id != 0) {
1003 printf("ERROR: event id not zero: 0x%llx\n",
1004 (unsigned long long)event_id);
1009 * Set some attributes, then read them back
1011 printf(" Set/get event attributes(1)\n");
1012 do {
1013 result = saEvtEventAttributesSet(event_handle,
1014 &evt_pat_set_array,
1015 TEST_PRIORITY,
1016 test_ret_time,
1017 &test_pub_name);
1018 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1019 if (result != SA_AIS_OK) {
1020 get_sa_error(result, result_buf, result_buf_len);
1021 printf("ERROR: event set attr result(1): %s\n", result_buf);
1022 goto evt_free;
1025 evt_pat_get_array.patternsNumber = 4;
1026 do {
1027 result = saEvtEventAttributesGet(event_handle,
1028 &evt_pat_get_array, /* patterns */
1029 &priority, /* priority */
1030 &retention_time, /* retention time */
1031 &publisher_name, /* publisher name */
1032 &publish_time, /* publish time */
1033 &event_id /* event_id */
1035 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1036 if (result != SA_AIS_OK) {
1037 get_sa_error(result, result_buf, result_buf_len);
1038 printf("ERROR: event get attr result(2): %s\n", result_buf);
1039 goto evt_free;
1041 if (evt_pat_get_array.patternsNumber != 4) {
1042 printf("ERROR: pattern array count not 4: %llu\n",
1043 (unsigned long long)evt_pat_get_array.patternsNumber);
1045 for (i = 0; i < evt_pat_get_array.patternsNumber; i++) {
1046 if (evt_pat_get_array.patterns[i].patternSize !=
1047 evt_pat_set_array.patterns[i].patternSize) {
1048 printf("ERROR: pattern %d count not equal g=%lld, s=%lld\n",
1050 (unsigned long long)evt_pat_get_array.patterns[i].patternSize,
1051 (unsigned long long)evt_pat_set_array.patterns[i].patternSize);
1052 printf("ERROR: pattern %d content g=\"%s\", s=\"%s\"\n",
1054 evt_pat_get_array.patterns[i].pattern,
1055 evt_pat_set_array.patterns[i].pattern);
1056 } else {
1057 if (memcmp(evt_pat_get_array.patterns[i].pattern,
1058 evt_pat_set_array.patterns[i].pattern,
1059 evt_pat_get_array.patterns[i].patternSize) != 0){
1060 printf(
1061 "ERROR: pattern %d don't match g=\"%s\", s=\"%s\"\n",
1063 evt_pat_get_array.patterns[i].pattern,
1064 evt_pat_set_array.patterns[i].pattern);
1069 if (priority != TEST_PRIORITY) {
1070 printf("ERROR: priority: e=0x%x a=0x%x\n",
1071 TEST_PRIORITY, priority);
1073 if (retention_time != test_ret_time) {
1074 printf("ERROR: retention: e=0x%llx a=0x%llx\n",
1075 (unsigned long long)test_ret_time,
1076 (unsigned long long)retention_time);
1078 if (publisher_name.length != test_pub_name.length) {
1079 printf("ERROR: publisher name length: e=%d, a=%d\n",
1080 test_pub_name.length,
1081 publisher_name.length);
1082 } else {
1083 if (memcmp(publisher_name.value, test_pub_name.value,
1084 publisher_name.length) != 0) {
1085 printf("ERROR: publisher name content: e=%s, a=%s\n",
1086 test_pub_name.value,
1087 publisher_name.value);
1091 if (event_id != 0) {
1092 printf("ERROR: event id not zero: 0x%llx\n",
1093 (unsigned long long)event_id);
1097 * event user data
1099 event_data_size = 0;
1100 printf(" Get event data(1)\n");
1101 do {
1102 result = saEvtEventDataGet(event_handle, 0, &event_data_size);
1103 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1104 if (result != SA_AIS_ERR_NO_SPACE) {
1105 get_sa_error(result, result_buf, result_buf_len);
1106 printf("ERROR: Get event data(1) result: %s\n", result_buf);
1108 printf(" Get event data(2)\n");
1109 do {
1110 result = saEvtEventDataGet(event_handle, event_data, &event_data_size);
1111 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1112 if (result != SA_AIS_ERR_BAD_HANDLE) {
1113 get_sa_error(result, result_buf, result_buf_len);
1114 printf("ERROR: Get event data(2) result: %s\n", result_buf);
1116 printf(" Get event data(3)\n");
1117 event_data_size = EVENT_DATA_SIZE;
1118 do {
1119 result = saEvtEventDataGet(event_handle, 0, &event_data_size);
1120 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1121 if (result != SA_AIS_ERR_NO_SPACE) {
1122 get_sa_error(result, result_buf, result_buf_len);
1123 printf("ERROR: Get event data(3) result: %s\n", result_buf);
1125 printf(" Get event data(4)\n");
1126 event_data_size = EVENT_DATA_SIZE;
1127 do {
1128 result = saEvtEventDataGet(event_handle, event_data,
1129 &event_data_size);
1130 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1131 if (result != SA_AIS_ERR_BAD_HANDLE) {
1132 get_sa_error(result, result_buf, result_buf_len);
1133 printf("ERROR: Get event data(4) result: %s\n", result_buf);
1135 printf(" Get event data(5)\n");
1136 event_data_size = 1;
1137 do {
1138 result = saEvtEventDataGet(event_handle, event_data,
1139 &event_data_size);
1140 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1141 if (result != SA_AIS_ERR_BAD_HANDLE) {
1142 get_sa_error(result, result_buf, result_buf_len);
1143 printf("ERROR: Get event data(5) result: %s\n", result_buf);
1146 printf(" Free event(1)\n");
1147 do {
1148 result = saEvtEventFree(event_handle);
1149 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1150 if (result != SA_AIS_OK) {
1151 get_sa_error(result, result_buf, result_buf_len);
1152 printf("ERROR: event free result: %s\n", result_buf);
1156 * Test publication.
1159 printf(" Publish with no patterns set\n");
1161 event_data_size = 0;
1162 do {
1163 result = saEvtEventSubscribe(channel_handle,
1164 &subscribe_filters,
1165 subscription_id);
1166 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1168 if (result != SA_AIS_OK) {
1169 get_sa_error(result, result_buf, result_buf_len);
1170 printf("ERROR: event subscribe result: %s\n", result_buf);
1171 do {
1172 result = saEvtChannelClose(channel_handle);
1173 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1174 if (result != SA_AIS_OK) {
1175 get_sa_error(result, result_buf, result_buf_len);
1176 printf("ERROR: Channel close result: %s\n", result_buf);
1178 do {
1179 result = saEvtFinalize(handle);
1180 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1181 if (result != SA_AIS_OK) {
1182 get_sa_error(result, result_buf, result_buf_len);
1183 printf("ERROR: Finalize result: %s\n", result_buf);
1185 return;
1188 do {
1189 result = saEvtEventAllocate(channel_handle, &event_handle);
1190 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1191 if (result != SA_AIS_OK) {
1192 get_sa_error(result, result_buf, result_buf_len);
1193 printf("ERROR: event Allocate result: %s\n", result_buf);
1194 goto evt_close;
1197 expected_pat_count = 0;
1198 do {
1199 result = saEvtEventPublish(event_handle, 0, 0, &event_id);
1200 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1201 if (result != SA_AIS_OK) {
1202 get_sa_error(result, result_buf, result_buf_len);
1203 printf("ERROR: event Publish result(1): %s\n", result_buf);
1204 goto evt_close;
1208 * See if we got the event
1210 do {
1211 result = saEvtSelectionObjectGet(handle, &fd);
1212 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1213 if (result != SA_AIS_OK) {
1214 get_sa_error(result, result_buf, result_buf_len);
1215 printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
1216 /* error */
1217 return;
1219 pfd.fd = fd;
1220 pfd.events = POLLIN;
1221 nfd = poll(&pfd, 1, timeout);
1222 if (nfd <= 0) {
1223 printf("ERROR: poll fds %d\n", nfd);
1224 if (nfd < 0) {
1225 perror("ERROR: poll error");
1227 /* Error */
1228 return;
1231 do {
1232 result = saEvtDispatch(handle, SA_DISPATCH_ONE);
1233 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1234 if (result != SA_AIS_OK) {
1235 get_sa_error(result, result_buf, result_buf_len);
1236 printf("ERROR: saEvtDispatch %s\n", result_buf);
1237 /* error */
1238 return;
1243 * Publish with pattens
1245 printf(" Publish with patterns set\n");
1247 retention_time = 0ULL;
1248 do {
1249 result = saEvtEventAttributesSet(event_handle,
1250 &evt_pat_set_array,
1251 TEST_PRIORITY,
1252 retention_time,
1253 &test_pub_name);
1254 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1255 if (result != SA_AIS_OK) {
1256 get_sa_error(result, result_buf, result_buf_len);
1257 printf("ERROR: event set attr result(2): %s\n", result_buf);
1258 goto evt_free;
1262 * Generate event data
1264 exp_data = malloc(DATA_SIZE);
1265 for (i = 0; i < LCOUNT; i++) {
1266 exp_data[i] = lrand48();
1268 event_data_size = DATA_SIZE;
1269 expected_pat_count = 4;
1272 * Send it
1274 do {
1275 result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
1276 &event_id);
1277 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1278 if (result != SA_AIS_OK) {
1279 get_sa_error(result, result_buf, result_buf_len);
1280 printf("ERROR: event Publish result(2): %s\n", result_buf);
1281 goto evt_close;
1285 * See if we got the event
1287 do {
1288 result = saEvtSelectionObjectGet(handle, &fd);
1289 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1290 if (result != SA_AIS_OK) {
1291 get_sa_error(result, result_buf, result_buf_len);
1292 printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
1293 /* error */
1294 return;
1296 pfd.fd = fd;
1297 pfd.events = POLLIN;
1298 nfd = poll(&pfd, 1, timeout);
1299 if (nfd <= 0) {
1300 printf("ERROR: poll fds %d\n", nfd);
1301 if (nfd < 0) {
1302 perror("ERROR: poll error");
1304 /* Error */
1305 return;
1308 do {
1309 result = saEvtDispatch(handle, SA_DISPATCH_ONE);
1310 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1311 if (result != SA_AIS_OK) {
1312 get_sa_error(result, result_buf, result_buf_len);
1313 printf("ERROR: saEvtDispatch %s\n", result_buf);
1314 /* error */
1315 return;
1321 * Test cleanup
1323 evt_free:
1324 do {
1325 result = saEvtEventFree(event_handle);
1326 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1327 if (result != SA_AIS_OK) {
1328 get_sa_error(result, result_buf, result_buf_len);
1329 printf("ERROR: event free result: %s\n", result_buf);
1332 evt_close:
1333 do {
1334 result = saEvtChannelClose(channel_handle);
1335 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1337 if (result != SA_AIS_OK) {
1338 get_sa_error(result, result_buf, result_buf_len);
1339 printf("ERROR: channel close result: %s\n", result_buf);
1341 evt_fin:
1342 do {
1343 result = saEvtFinalize(handle);
1344 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1346 if (result != SA_AIS_OK) {
1347 get_sa_error(result, result_buf, result_buf_len);
1348 printf("ERROR: Event Finalize result: %s\n", result_buf);
1350 printf("Done\n");
1354 SaEvtEventIdT event_id1;
1355 SaEvtEventIdT event_id2;
1356 SaEvtEventIdT event_id3;
1357 SaEvtSubscriptionIdT sub1 = 0x101010;
1358 SaEvtSubscriptionIdT sub2 = 0x202020;
1359 static int call_count = 0;
1362 * Handle call back for multi-test1
1363 * Checks event ID with subscription ID to make sure that we
1364 * received an event on the correct subscription.
1366 void
1367 multi_test_callback1(SaEvtSubscriptionIdT my_subscription_id,
1368 const SaEvtEventHandleT event_handle,
1369 const SaSizeT my_event_data_size)
1371 SaAisErrorT result;
1372 SaUint8T my_priority;
1373 SaTimeT my_retention_time;
1374 SaNameT my_publisher_name = {0, {0}};
1375 SaTimeT my_publish_time;
1376 SaEvtEventIdT my_event_id;
1377 SaEvtSubscriptionIdT exp_sub_id;
1379 printf(" multi_test_callback1 called(%d)\n", ++call_count);
1381 evt_pat_get_array.patternsNumber = 4;
1382 do {
1383 result = saEvtEventAttributesGet(event_handle,
1384 &evt_pat_get_array, /* patterns */
1385 &my_priority, /* priority */
1386 &my_retention_time, /* retention time */
1387 &my_publisher_name, /* publisher name */
1388 &my_publish_time, /* publish time */
1389 &my_event_id /* event_id */
1391 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1392 if (result != SA_AIS_OK) {
1393 get_sa_error(result, result_buf, result_buf_len);
1394 printf("ERROR: event get attr result: %s\n", result_buf);
1395 goto evt_free;
1398 if (my_event_id == event_id1) {
1399 exp_sub_id = sub1;
1400 } else if (my_event_id == event_id2) {
1401 exp_sub_id = sub2;
1402 } else if (my_event_id == event_id3) {
1403 printf("ERROR: Received event 3 but not subscribed\n");
1404 goto evt_free;
1405 } else {
1406 printf("ERROR: Received event %llx but not sent\n",
1407 (unsigned long long)my_event_id);
1408 goto evt_free;
1411 if (my_subscription_id != exp_sub_id) {
1412 printf("ERROR: sub ID: e=%x, a=%x\n",
1413 exp_sub_id, my_subscription_id);
1414 goto evt_free;
1417 if (evt_pat_get_array.patternsNumber != 1) {
1418 printf("ERROR: pattern array count not 1: %lld\n",
1419 (unsigned long long)evt_pat_get_array.patternsNumber);
1422 evt_free:
1423 do {
1424 result = saEvtEventFree(event_handle);
1425 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1426 if (result != SA_AIS_OK) {
1427 get_sa_error(result, result_buf, result_buf_len);
1428 printf("ERROR: event free result: %s\n", result_buf);
1433 * Test multiple channel operations
1434 * 1. Test multiple subscriptions on a single channel and receiving
1435 * events.
1436 * 2. Test multiple openings of a single channel and receving events.
1437 * 3. Test opening of multiple channels and receiving events
1439 void
1440 test_multi_channel1()
1443 SaEvtEventFilterT filt1[1] = {
1444 {SA_EVT_EXACT_FILTER, {8,8, (SaUint8T *) "ChanPat1"}},
1446 SaEvtEventFilterT filt2[1] = {
1447 {SA_EVT_EXACT_FILTER, {8, 8, (SaUint8T *) "ChanPat2"}},
1450 SaEvtEventFilterArrayT sub_filt = {
1451 1, NULL
1454 SaEvtEventPatternT pat1 = {8, 8, (SaUint8T *) "ChanPat1"};
1455 SaEvtEventPatternT pat2 = {8, 8, (SaUint8T *) "ChanPat2"};
1456 SaEvtEventPatternT pat3 = {8, 8, (SaUint8T *) "ChanPat3"};
1458 SaEvtEventPatternArrayT evt_pat = {
1459 1, 1, NULL
1463 SaEvtHandleT handle;
1464 SaEvtChannelHandleT channel_handle;
1465 SaEvtEventHandleT event_handle;
1466 SaEvtChannelOpenFlagsT flags;
1467 SaNameT channel_name;
1468 SaEvtCallbacksT multi_callbacks = {
1470 multi_test_callback1
1473 struct pollfd pfd;
1474 int nfd;
1475 SaSelectionObjectT fd;
1476 int timeout = 5000;
1480 SaAisErrorT result;
1482 flags = SA_EVT_CHANNEL_PUBLISHER|SA_EVT_CHANNEL_SUBSCRIBER |
1483 SA_EVT_CHANNEL_CREATE;
1484 strcpy((char *)channel_name.value, channel);
1485 channel_name.length = strlen(channel);
1487 printf("Test multiple operations:\n");
1489 do {
1490 result = saEvtInitialize (&handle, &multi_callbacks,
1491 versions[0].version);
1492 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1493 if (result != SA_AIS_OK) {
1494 get_sa_error(result, result_buf, result_buf_len);
1495 printf("ERROR: Event Initialize result: %s\n", result_buf);
1496 return;
1498 do {
1499 result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
1500 &channel_handle);
1501 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1502 if (result != SA_AIS_OK) {
1503 get_sa_error(result, result_buf, result_buf_len);
1504 printf("ERROR: channel open result: %s\n", result_buf);
1505 goto evt_fin;
1509 * Allocate an event
1511 do {
1512 result = saEvtEventAllocate(channel_handle, &event_handle);
1513 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1514 if (result != SA_AIS_OK) {
1515 get_sa_error(result, result_buf, result_buf_len);
1516 printf("ERROR: event Allocate result: %s\n", result_buf);
1517 goto evt_close;
1521 * 1. Test multiple subscriptions on a single channel and receiving
1522 * events.
1524 * Subscribe twice with two different filters. Then send three events.
1525 * One will match the first filter, the second will match the second
1526 * filter, the third will match none. We will validate that we receive
1527 * two events and that the subscription IDs match what we expect for the
1528 * given pattern.
1531 sub_filt.filters = filt1;
1532 do {
1533 result = saEvtEventSubscribe(channel_handle,
1534 &sub_filt,
1535 sub1);
1536 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1537 if (result != SA_AIS_OK) {
1538 get_sa_error(result, result_buf, result_buf_len);
1539 printf("ERROR: event subscribe(1) result: %s\n", result_buf);
1540 goto evt_free;
1543 sub_filt.filters = filt2;
1544 do {
1545 result = saEvtEventSubscribe(channel_handle,
1546 &sub_filt,
1547 sub2);
1548 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1549 if (result != SA_AIS_OK) {
1550 get_sa_error(result, result_buf, result_buf_len);
1551 printf("ERROR: event subscribe(2) result: %s\n", result_buf);
1552 goto evt_free;
1556 retention_time = 0ULL;
1558 evt_pat.patterns = &pat1;
1559 do {
1560 result = saEvtEventAttributesSet(event_handle,
1561 &evt_pat,
1562 TEST_PRIORITY,
1563 retention_time,
1564 &test_pub_name);
1565 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1566 if (result != SA_AIS_OK) {
1567 get_sa_error(result, result_buf, result_buf_len);
1568 printf("ERROR: event set attr result(1): %s\n", result_buf);
1569 goto evt_free;
1572 do {
1573 result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
1574 &event_id1);
1575 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1576 if (result != SA_AIS_OK) {
1577 get_sa_error(result, result_buf, result_buf_len);
1578 printf("ERROR: event Publish result:(1) %s\n", result_buf);
1579 goto evt_close;
1582 evt_pat.patterns = &pat2;
1583 do {
1584 result = saEvtEventAttributesSet(event_handle,
1585 &evt_pat,
1586 TEST_PRIORITY,
1587 retention_time,
1588 &test_pub_name);
1589 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1590 if (result != SA_AIS_OK) {
1591 get_sa_error(result, result_buf, result_buf_len);
1592 printf("ERROR: event set attr result(2): %s\n", result_buf);
1593 goto evt_free;
1596 do {
1597 result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
1598 &event_id2);
1599 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1600 if (result != SA_AIS_OK) {
1601 get_sa_error(result, result_buf, result_buf_len);
1602 printf("ERROR: event Publish result:(2) %s\n", result_buf);
1603 goto evt_close;
1606 evt_pat.patterns = &pat3;
1607 do {
1608 result = saEvtEventAttributesSet(event_handle,
1609 &evt_pat,
1610 TEST_PRIORITY,
1611 retention_time,
1612 &test_pub_name);
1613 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1614 if (result != SA_AIS_OK) {
1615 get_sa_error(result, result_buf, result_buf_len);
1616 printf("ERROR: event set attr result(3): %s\n", result_buf);
1617 goto evt_free;
1620 do {
1621 result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
1622 &event_id3);
1623 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1624 if (result != SA_AIS_OK) {
1625 get_sa_error(result, result_buf, result_buf_len);
1626 printf("ERROR: event Publish result:(3) %s\n", result_buf);
1627 goto evt_close;
1631 * See if we got the event
1633 do {
1634 result = saEvtSelectionObjectGet(handle, &fd);
1635 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1636 if (result != SA_AIS_OK) {
1637 get_sa_error(result, result_buf, result_buf_len);
1638 printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
1639 /* error */
1640 return;
1643 while(1) {
1644 pfd.fd = fd;
1645 pfd.events = POLLIN;
1646 nfd = poll(&pfd, 1, timeout);
1647 if (nfd == 0) {
1648 break;
1649 } else if (nfd < 0) {
1650 perror("ERROR: poll error");
1651 break;
1655 do {
1656 result = saEvtDispatch(handle, SA_DISPATCH_ALL);
1657 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1658 if (result != SA_AIS_OK) {
1659 get_sa_error(result, result_buf, result_buf_len);
1660 printf("ERROR: saEvtDispatch %s\n", result_buf);
1661 /* error */
1662 goto evt_free;
1667 if (call_count != 2) {
1668 printf("ERROR: call back count: e=2, a=%d\n", call_count);
1669 goto evt_free;
1673 * Test cleanup
1675 evt_free:
1676 do {
1677 result = saEvtEventFree(event_handle);
1678 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1679 if (result != SA_AIS_OK) {
1680 get_sa_error(result, result_buf, result_buf_len);
1681 printf("ERROR: event free result: %s\n", result_buf);
1684 evt_close:
1685 do {
1686 result = saEvtChannelClose(channel_handle);
1687 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1689 if (result != SA_AIS_OK) {
1690 get_sa_error(result, result_buf, result_buf_len);
1691 printf("ERROR: channel close result: %s\n", result_buf);
1693 evt_fin:
1694 do {
1695 result = saEvtFinalize(handle);
1696 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1698 if (result != SA_AIS_OK) {
1699 get_sa_error(result, result_buf, result_buf_len);
1700 printf("ERROR: Event Finalize result: %s\n", result_buf);
1702 printf("Done\n");
1707 * Handle call back for multi-test2
1708 * Counts events received. Makes sure that we get one event from
1709 * each subscription.
1712 void
1713 multi_test_callback2(SaEvtSubscriptionIdT my_subscription_id,
1714 const SaEvtEventHandleT event_handle,
1715 const SaSizeT my_event_data_size)
1717 SaAisErrorT result;
1718 SaUint8T my_priority;
1719 SaTimeT my_retention_time;
1720 SaNameT my_publisher_name = {0, {0}};
1721 SaTimeT my_publish_time;
1722 SaEvtEventIdT my_event_id;
1723 SaEvtSubscriptionIdT last_sub_id = 0;
1725 printf(" multi_test_callback2 called(%d)\n", ++call_count);
1727 evt_pat_get_array.patternsNumber = 4;
1728 do {
1729 result = saEvtEventAttributesGet(event_handle,
1730 &evt_pat_get_array, /* patterns */
1731 &my_priority, /* priority */
1732 &my_retention_time, /* retention time */
1733 &my_publisher_name, /* publisher name */
1734 &my_publish_time, /* publish time */
1735 &my_event_id /* event_id */
1737 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1738 if (result != SA_AIS_OK) {
1739 get_sa_error(result, result_buf, result_buf_len);
1740 printf("ERROR: event get attr result: %s\n", result_buf);
1741 goto evt_free;
1744 if (my_event_id != event_id1) {
1745 printf("ERROR: Received wrong event\n");
1746 goto evt_free;
1749 if (last_sub_id == 0) {
1750 if (my_subscription_id != sub1 &&
1751 my_subscription_id != sub2) {
1752 printf("ERROR: Received bad subscription ID\n");
1753 goto evt_free;
1755 last_sub_id = my_subscription_id;
1756 } else {
1757 if (my_subscription_id == last_sub_id) {
1758 printf("ERROR: Received subscription ID twice\n");
1759 goto evt_free;
1761 if (my_subscription_id != sub1 &&
1762 my_subscription_id != sub2) {
1763 printf("ERROR: Received bad subscription ID\n");
1764 goto evt_free;
1768 if (evt_pat_get_array.patternsNumber != 1) {
1769 printf("ERROR: pattern array count not 1: %llu\n",
1770 (unsigned long long)evt_pat_get_array.patternsNumber);
1773 evt_free:
1774 do {
1775 result = saEvtEventFree(event_handle);
1776 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1777 if (result != SA_AIS_OK) {
1778 get_sa_error(result, result_buf, result_buf_len);
1779 printf("ERROR: event free result: %s\n", result_buf);
1783 void
1784 test_multi_channel2()
1787 SaEvtEventFilterT filt1[1] = {
1788 {SA_EVT_EXACT_FILTER, {8, 8, (SaUint8T *) "ChanPat1"}},
1791 SaEvtEventFilterArrayT sub_filt = {
1792 1, NULL
1795 SaEvtEventPatternT pat1 = {8, 8, (SaUint8T *) "ChanPat1"};
1797 SaEvtEventPatternArrayT evt_pat = {
1798 1, 1, NULL
1802 SaEvtHandleT handle;
1803 SaEvtChannelHandleT channel_handle;
1804 SaEvtChannelHandleT channel_handle1;
1805 SaEvtEventHandleT event_handle;
1806 SaEvtChannelOpenFlagsT flags;
1807 SaNameT channel_name;
1808 SaEvtCallbacksT multi_callbacks = {
1810 multi_test_callback2
1813 struct pollfd pfd;
1814 int nfd;
1815 SaSelectionObjectT fd;
1816 int timeout = 5000;
1820 SaAisErrorT result;
1822 flags = SA_EVT_CHANNEL_PUBLISHER|SA_EVT_CHANNEL_SUBSCRIBER |
1823 SA_EVT_CHANNEL_CREATE;
1824 strcpy((char *)channel_name.value, channel);
1825 channel_name.length = strlen(channel);
1828 * 2. Test multiple openings of a single channel and receving events.
1830 * Open and subscribe to a channel twice. When an event is sent, it
1831 * should be delivered twice, once for each open channel.
1833 printf("Test multiple opens/subscribes:\n");
1835 do {
1836 result = saEvtInitialize (&handle, &multi_callbacks,
1837 versions[0].version);
1838 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1839 if (result != SA_AIS_OK) {
1840 get_sa_error(result, result_buf, result_buf_len);
1841 printf("ERROR: Event Initialize result: %s\n", result_buf);
1842 return;
1845 do {
1846 result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
1847 &channel_handle);
1848 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1849 if (result != SA_AIS_OK) {
1850 get_sa_error(result, result_buf, result_buf_len);
1851 printf("ERROR: channel open(0) result: %s\n", result_buf);
1852 goto evt_fin;
1855 do {
1856 result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
1857 &channel_handle1);
1858 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1859 if (result != SA_AIS_OK) {
1860 get_sa_error(result, result_buf, result_buf_len);
1861 printf("ERROR: channel open(1) result: %s\n", result_buf);
1862 goto evt_fin;
1865 do {
1866 result = saEvtEventAllocate(channel_handle, &event_handle);
1867 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1868 if (result != SA_AIS_OK) {
1869 get_sa_error(result, result_buf, result_buf_len);
1870 printf("ERROR: event Allocate result: %s\n", result_buf);
1871 goto evt_close;
1873 sub_filt.filters = filt1;
1874 do {
1875 result = saEvtEventSubscribe(channel_handle,
1876 &sub_filt,
1877 sub1);
1878 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1879 if (result != SA_AIS_OK) {
1880 get_sa_error(result, result_buf, result_buf_len);
1881 printf("ERROR: event subscribe(0) result: %s\n", result_buf);
1882 goto evt_free;
1884 sub_filt.filters = filt1;
1885 do {
1886 result = saEvtEventSubscribe(channel_handle1,
1887 &sub_filt,
1888 sub2);
1889 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1890 if (result != SA_AIS_OK) {
1891 get_sa_error(result, result_buf, result_buf_len);
1892 printf("ERROR: event subscribe(1) result: %s\n", result_buf);
1893 goto evt_free;
1895 retention_time = 0ULL;
1897 evt_pat.patterns = &pat1;
1898 do {
1899 result = saEvtEventAttributesSet(event_handle,
1900 &evt_pat,
1901 TEST_PRIORITY,
1902 retention_time,
1903 &test_pub_name);
1904 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1905 if (result != SA_AIS_OK) {
1906 get_sa_error(result, result_buf, result_buf_len);
1907 printf("ERROR: event set attr result: %s\n", result_buf);
1908 goto evt_free;
1911 do {
1912 result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
1913 &event_id1);
1914 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1915 if (result != SA_AIS_OK) {
1916 get_sa_error(result, result_buf, result_buf_len);
1917 printf("ERROR: event Publish result: %s\n", result_buf);
1918 goto evt_close;
1921 * See if we got the event
1923 do {
1924 result = saEvtSelectionObjectGet(handle, &fd);
1925 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1926 if (result != SA_AIS_OK) {
1927 get_sa_error(result, result_buf, result_buf_len);
1928 printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
1929 /* error */
1930 return;
1933 call_count = 0;
1935 while(1) {
1936 pfd.fd = fd;
1937 pfd.events = POLLIN;
1938 nfd = poll(&pfd, 1, timeout);
1939 if (nfd == 0) {
1940 break;
1941 } else if (nfd < 0) {
1942 perror("ERROR: poll error");
1943 break;
1947 do {
1948 result = saEvtDispatch(handle, SA_DISPATCH_ALL);
1949 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1950 if (result != SA_AIS_OK) {
1951 get_sa_error(result, result_buf, result_buf_len);
1952 printf("ERROR: saEvtDispatch %s\n", result_buf);
1953 /* error */
1954 goto evt_free;
1959 if (call_count != 2) {
1960 printf("ERROR: call back count: e=2, a=%d\n", call_count);
1961 goto evt_free;
1965 * Test cleanup
1967 evt_free:
1968 do {
1969 result = saEvtEventFree(event_handle);
1970 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1971 if (result != SA_AIS_OK) {
1972 get_sa_error(result, result_buf, result_buf_len);
1973 printf("ERROR: event free result: %s\n", result_buf);
1976 evt_close:
1977 do {
1978 result = saEvtChannelClose(channel_handle);
1979 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1981 if (result != SA_AIS_OK) {
1982 get_sa_error(result, result_buf, result_buf_len);
1983 printf("ERROR: channel close result(0): %s\n", result_buf);
1985 do {
1986 result = saEvtChannelClose(channel_handle1);
1987 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1989 if (result != SA_AIS_OK) {
1990 get_sa_error(result, result_buf, result_buf_len);
1991 printf("ERROR: channel close result(1): %s\n", result_buf);
1993 evt_fin:
1994 do {
1995 result = saEvtFinalize(handle);
1996 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
1998 if (result != SA_AIS_OK) {
1999 get_sa_error(result, result_buf, result_buf_len);
2000 printf("ERROR: Event Finalize result: %s\n", result_buf);
2002 printf("Done\n");
2007 * Handle call back for multi-test3
2008 * Verifies that the event recevied is associated with the correct
2009 * subscription.
2012 void
2013 multi_test_callback3(SaEvtSubscriptionIdT my_subscription_id,
2014 const SaEvtEventHandleT event_handle,
2015 const SaSizeT my_event_data_size)
2017 SaAisErrorT result;
2018 SaUint8T my_priority;
2019 SaTimeT my_retention_time;
2020 SaNameT my_publisher_name = {0, {0}};
2021 SaTimeT my_publish_time;
2022 SaEvtEventIdT my_event_id;
2024 printf(" multi_test_callback2 called(%d)\n", ++call_count);
2026 evt_pat_get_array.patternsNumber = 4;
2027 do {
2028 result = saEvtEventAttributesGet(event_handle,
2029 &evt_pat_get_array, /* patterns */
2030 &my_priority, /* priority */
2031 &my_retention_time, /* retention time */
2032 &my_publisher_name, /* publisher name */
2033 &my_publish_time, /* publish time */
2034 &my_event_id /* event_id */
2036 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2037 if (result != SA_AIS_OK) {
2038 get_sa_error(result, result_buf, result_buf_len);
2039 printf("ERROR: event get attr result: %s\n", result_buf);
2040 goto evt_free;
2043 if ((my_subscription_id != sub1) && (my_subscription_id != sub2)) {
2044 printf("ERROR: Received wrong subscription ID %x\n",
2045 my_subscription_id);
2046 printf(" sub1 %x, sub2 %x\n", sub1, sub2);
2047 goto evt_free;
2050 if ((my_event_id != event_id1) && (my_event_id != event_id2)) {
2051 printf("ERROR: Received wrong event ID %llx\n",
2052 (unsigned long long)my_event_id);
2053 printf(" id1 %llx, id2 %llx\n",
2054 (unsigned long long)event_id1,
2055 (unsigned long long)event_id2);
2056 goto evt_free;
2059 if ((my_subscription_id == sub1) && (my_event_id != event_id1)) {
2060 printf("ERROR: Received event on wrong subscription\n");
2061 goto evt_free;
2063 if ((my_subscription_id == sub2) && (my_event_id != event_id2)) {
2064 printf("ERROR: Received event on wrong subscription\n");
2065 goto evt_free;
2068 if (evt_pat_get_array.patternsNumber != 1) {
2069 printf("ERROR: pattern array count not 1: %llu\n",
2070 (unsigned long long)evt_pat_get_array.patternsNumber);
2073 evt_free:
2074 do {
2075 result = saEvtEventFree(event_handle);
2076 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2077 if (result != SA_AIS_OK) {
2078 get_sa_error(result, result_buf, result_buf_len);
2079 printf("ERROR: event free result: %s\n", result_buf);
2083 void
2084 test_multi_channel3()
2087 SaEvtEventFilterT filt1[1] = {
2088 {SA_EVT_PREFIX_FILTER, {7, 7, (SaUint8T *) "ChanPat"}},
2091 SaEvtEventFilterArrayT sub_filt = {
2092 1, NULL
2095 SaEvtEventPatternT pat1 = {8, 8, (SaUint8T *) "ChanPat1"};
2096 SaEvtEventPatternT pat2 = {8, 8, (SaUint8T *) "ChanPat2"};
2098 SaEvtEventPatternArrayT evt_pat = {
2099 1, 1, NULL
2103 SaEvtHandleT handle;
2104 SaEvtChannelHandleT channel_handle;
2105 SaEvtChannelHandleT channel_handle1;
2106 SaEvtEventHandleT event_handle;
2107 SaEvtEventHandleT event_handle1;
2108 SaEvtChannelOpenFlagsT flags;
2109 SaNameT channel_name;
2110 SaNameT channel_name1;
2111 SaEvtCallbacksT multi_callbacks = {
2113 multi_test_callback3
2116 struct pollfd pfd;
2117 int nfd;
2118 SaSelectionObjectT fd;
2119 int timeout = 5000;
2123 SaAisErrorT result;
2125 flags = SA_EVT_CHANNEL_PUBLISHER|SA_EVT_CHANNEL_SUBSCRIBER |
2126 SA_EVT_CHANNEL_CREATE;
2127 strcpy((char *)channel_name.value, channel);
2128 channel_name.length = strlen((char *)channel_name.value);
2129 strcpy((char *)channel_name1.value, channel);
2130 strcat((char *)channel_name1.value, "_1");
2131 channel_name1.length = strlen((char *)channel_name1.value);
2135 * 3. Test opening of multiple channels and receiving events.
2136 * Open and subscribe to two different channels twice.
2137 * Subscribe to each channel with the same filters.
2138 * Sending an event on one channel should be received in the
2139 * call-back with the subscription ID corresponding to the sent
2140 * channel.
2142 printf("Test multiple different channels/subscribes:\n");
2144 do {
2145 result = saEvtInitialize (&handle, &multi_callbacks,
2146 versions[0].version);
2147 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2148 if (result != SA_AIS_OK) {
2149 get_sa_error(result, result_buf, result_buf_len);
2150 printf("ERROR: Event Initialize result: %s\n", result_buf);
2151 return;
2154 do {
2155 result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
2156 &channel_handle);
2157 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2158 if (result != SA_AIS_OK) {
2159 get_sa_error(result, result_buf, result_buf_len);
2160 printf("ERROR: channel open(0) result: %s\n", result_buf);
2161 goto evt_fin;
2164 do {
2165 result = saEvtChannelOpen(handle, &channel_name1, flags, SA_TIME_MAX,
2166 &channel_handle1);
2167 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2168 if (result != SA_AIS_OK) {
2169 get_sa_error(result, result_buf, result_buf_len);
2170 printf("ERROR: channel open(1) result: %s\n", result_buf);
2171 goto evt_fin;
2174 do {
2175 result = saEvtEventAllocate(channel_handle, &event_handle);
2176 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2177 if (result != SA_AIS_OK) {
2178 get_sa_error(result, result_buf, result_buf_len);
2179 printf("ERROR: event Allocate(0) result: %s\n", result_buf);
2180 goto evt_close;
2182 do {
2183 result = saEvtEventAllocate(channel_handle1, &event_handle1);
2184 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2185 if (result != SA_AIS_OK) {
2186 get_sa_error(result, result_buf, result_buf_len);
2187 printf("ERROR: event Allocate(1) result: %s\n", result_buf);
2188 goto evt_close;
2191 sub_filt.filters = filt1;
2192 do {
2193 result = saEvtEventSubscribe(channel_handle,
2194 &sub_filt,
2195 sub1);
2196 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2197 if (result != SA_AIS_OK) {
2198 get_sa_error(result, result_buf, result_buf_len);
2199 printf("ERROR: event subscribe(0) result: %s\n", result_buf);
2200 goto evt_free;
2202 sub_filt.filters = filt1;
2203 do {
2204 result = saEvtEventSubscribe(channel_handle1,
2205 &sub_filt,
2206 sub2);
2207 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2208 if (result != SA_AIS_OK) {
2209 get_sa_error(result, result_buf, result_buf_len);
2210 printf("ERROR: event subscribe(1) result: %s\n", result_buf);
2211 goto evt_free;
2213 retention_time = 0ULL;
2215 evt_pat.patterns = &pat1;
2216 do {
2217 result = saEvtEventAttributesSet(event_handle,
2218 &evt_pat,
2219 TEST_PRIORITY,
2220 retention_time,
2221 &test_pub_name);
2222 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2223 if (result != SA_AIS_OK) {
2224 get_sa_error(result, result_buf, result_buf_len);
2225 printf("ERROR: event set attr(0) result: %s\n", result_buf);
2226 goto evt_free;
2229 evt_pat.patterns = &pat2;
2230 do {
2231 result = saEvtEventAttributesSet(event_handle1,
2232 &evt_pat,
2233 TEST_PRIORITY,
2234 retention_time,
2235 &test_pub_name);
2236 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2237 if (result != SA_AIS_OK) {
2238 get_sa_error(result, result_buf, result_buf_len);
2239 printf("ERROR: event set attr(1) result: %s\n", result_buf);
2240 goto evt_free;
2243 do {
2244 result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
2245 &event_id1);
2246 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2247 if (result != SA_AIS_OK) {
2248 get_sa_error(result, result_buf, result_buf_len);
2249 printf("ERROR: event Publish result: %s\n", result_buf);
2250 goto evt_close;
2252 do {
2253 result = saEvtEventPublish(event_handle1, exp_data, DATA_SIZE,
2254 &event_id2);
2255 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2256 if (result != SA_AIS_OK) {
2257 get_sa_error(result, result_buf, result_buf_len);
2258 printf("ERROR: event Publish result: %s\n", result_buf);
2259 goto evt_close;
2262 * See if we got the events
2264 do {
2265 result = saEvtSelectionObjectGet(handle, &fd);
2266 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2267 if (result != SA_AIS_OK) {
2268 get_sa_error(result, result_buf, result_buf_len);
2269 printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
2270 /* error */
2271 return;
2274 call_count = 0;
2276 while(1) {
2277 pfd.fd = fd;
2278 pfd.events = POLLIN;
2279 nfd = poll(&pfd, 1, timeout);
2280 if (nfd == 0) {
2281 break;
2282 } else if (nfd < 0) {
2283 perror("ERROR: poll error");
2284 break;
2288 do {
2289 result = saEvtDispatch(handle, SA_DISPATCH_ALL);
2290 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2291 if (result != SA_AIS_OK) {
2292 get_sa_error(result, result_buf, result_buf_len);
2293 printf("ERROR: saEvtDispatch %s\n", result_buf);
2294 /* error */
2295 goto evt_free;
2300 if (call_count != 2) {
2301 printf("ERROR: call back count: e=2, a=%d\n", call_count);
2302 goto evt_free;
2306 * Test cleanup
2308 evt_free:
2309 do {
2310 result = saEvtEventFree(event_handle);
2311 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2312 if (result != SA_AIS_OK) {
2313 get_sa_error(result, result_buf, result_buf_len);
2314 printf("ERROR: event free result: %s\n", result_buf);
2316 do {
2317 result = saEvtEventFree(event_handle1);
2318 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2319 if (result != SA_AIS_OK) {
2320 get_sa_error(result, result_buf, result_buf_len);
2321 printf("ERROR: event free result: %s\n", result_buf);
2324 evt_close:
2325 do {
2326 result = saEvtChannelClose(channel_handle);
2327 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2329 if (result != SA_AIS_OK) {
2330 get_sa_error(result, result_buf, result_buf_len);
2331 printf("ERROR: channel close result(0): %s\n", result_buf);
2333 do {
2334 result = saEvtChannelClose(channel_handle1);
2335 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2337 if (result != SA_AIS_OK) {
2338 get_sa_error(result, result_buf, result_buf_len);
2339 printf("ERROR: channel close result(1): %s\n", result_buf);
2341 evt_fin:
2342 do {
2343 result = saEvtFinalize(handle);
2344 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2346 if (result != SA_AIS_OK) {
2347 get_sa_error(result, result_buf, result_buf_len);
2348 printf("ERROR: Event Finalize result: %s\n", result_buf);
2350 printf("Done\n");
2355 * Test event retention
2356 * Test 1: publish the event with a retention time and then
2357 * subscribe. If the event was retained, we should receive it.
2359 * Test 2: Publish the event, sleep until it expires, then
2360 * subscribe. We shouldn't get an event delivered.
2362 * Test 3: Publish an event with a retention time.
2363 * subscribe.
2364 * wait for it.
2365 * unsubscribe
2366 * Clear it.
2367 * Then subscribe and make sure that the messages isn't delivered.
2370 #define EXPIRE_TIME 10 /* Seconds */
2371 SaEvtEventIdT retained_id;
2372 int got_event;
2374 void
2375 event_callback_retained(SaEvtSubscriptionIdT my_subscription_id,
2376 const SaEvtEventHandleT event_handle,
2377 const SaSizeT my_event_data_size)
2379 SaAisErrorT result;
2380 //printf("event_callback_retained called\n");
2381 do {
2382 result = saEvtEventAttributesGet(event_handle,
2383 0, /* patterns */
2384 0, /* priority */
2385 0, /* retention time */
2386 0, /* publisher name */
2387 0, /* publish time */
2388 &retained_id /* event_id */
2390 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2391 if (result != SA_AIS_OK) {
2392 get_sa_error(result, result_buf, result_buf_len);
2393 printf("ERROR: callback attr get result: %s\n", result_buf);
2394 return;
2396 got_event = 1;
2399 void
2400 test_retention()
2402 SaEvtHandleT handle;
2403 SaEvtChannelHandleT channel_handle;
2404 SaEvtEventHandleT event_handle;
2405 SaEvtChannelOpenFlagsT flags;
2406 SaNameT channel_name;
2407 SaEvtCallbacksT callbacks_retain = {
2409 event_callback_retained
2412 struct pollfd pfd;
2413 int nfd;
2414 SaSelectionObjectT fd;
2415 int timeout = (EXPIRE_TIME + 5);
2416 SaAisErrorT result;
2418 flags = SA_EVT_CHANNEL_PUBLISHER |
2419 SA_EVT_CHANNEL_SUBSCRIBER |
2420 SA_EVT_CHANNEL_CREATE;
2421 strcpy((char *)channel_name.value, channel);
2422 channel_name.length = strlen(channel);
2424 printf("Test Event retention:\n");
2426 do {
2427 result = saEvtInitialize (&handle, &callbacks_retain,
2428 versions[0].version);
2429 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2430 if (result != SA_AIS_OK) {
2431 get_sa_error(result, result_buf, result_buf_len);
2432 printf("ERROR: Event Initialize result: %s\n", result_buf);
2433 return;
2436 do {
2437 result = saEvtSelectionObjectGet(handle, &fd);
2438 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2439 if (result != SA_AIS_OK) {
2440 get_sa_error(result, result_buf, result_buf_len);
2441 printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
2442 /* error */
2443 return;
2446 do {
2447 result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
2448 &channel_handle);
2449 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2450 if (result != SA_AIS_OK) {
2451 get_sa_error(result, result_buf, result_buf_len);
2452 printf("ERROR: channel open result: %s\n", result_buf);
2453 goto evt_fin;
2457 * Allocate an event
2459 do {
2460 result = saEvtEventAllocate(channel_handle, &event_handle);
2461 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2462 if (result != SA_AIS_OK) {
2463 get_sa_error(result, result_buf, result_buf_len);
2464 printf("ERROR: event Allocate result: %s\n", result_buf);
2465 goto evt_close;
2469 retention_time = (EXPIRE_TIME)*1000000000ULL;
2470 do {
2471 result = saEvtEventAttributesSet(event_handle,
2472 &evt_pat_set_array,
2473 TEST_PRIORITY,
2474 retention_time,
2475 &test_pub_name);
2476 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2477 if (result != SA_AIS_OK) {
2478 get_sa_error(result, result_buf, result_buf_len);
2479 printf("ERROR: event set attr result: %s\n", result_buf);
2480 goto evt_free;
2484 * Test 1: publish the event with a retention time and then
2485 * subscribe. If the event was retained, we should receive it.
2487 printf(" Receive retained event\n");
2488 got_event=0;
2489 retained_id=0;
2490 do {
2491 result = saEvtEventPublish(event_handle, exp_data, 0, &event_id);
2492 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2493 if (result != SA_AIS_OK) {
2494 get_sa_error(result, result_buf, result_buf_len);
2495 printf("ERROR: event Publish result(1): %s\n", result_buf);
2496 goto evt_close;
2500 do {
2501 result = saEvtEventSubscribe(channel_handle,
2502 &subscribe_filters,
2503 subscription_id);
2504 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2506 if (result != SA_AIS_OK) {
2507 get_sa_error(result, result_buf, result_buf_len);
2508 printf("ERROR: event subscribe result: %s\n", result_buf);
2509 goto evt_free;
2512 pfd.fd = fd;
2513 pfd.events = POLLIN;
2514 nfd = poll(&pfd, 1, 1000);
2515 if (nfd <= 0) {
2516 printf("ERROR: poll fds %d\n", nfd);
2517 if (nfd < 0) {
2518 perror("ERROR: poll error");
2520 /* Error */
2521 goto evt_free;
2524 do {
2525 result = saEvtDispatch(handle, SA_DISPATCH_ONE);
2526 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2527 if (result != SA_AIS_OK) {
2528 get_sa_error(result, result_buf, result_buf_len);
2529 printf("ERROR: saEvtDispatch %s\n", result_buf);
2530 /* error */
2531 goto evt_free;
2534 if (!got_event) {
2535 printf("ERROR: retained event not recevied\n");
2536 goto evt_free;
2538 if (retained_id != event_id) {
2539 printf("ERROR: received the wrong event: e=%llx, a=%llx\n",
2540 (unsigned long long)event_id,
2541 (unsigned long long)retained_id);
2542 goto evt_free;
2545 do {
2546 result = saEvtEventUnsubscribe(channel_handle, subscription_id);
2547 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2548 if (result != SA_AIS_OK) {
2549 get_sa_error(result, result_buf, result_buf_len);
2550 printf("ERROR: unsubscribe result: %s\n", result_buf);
2551 goto evt_free;
2555 * Test 2: Publish the event, sleep until it expires, then
2556 * subscribe. We shouldn't get an event delivered.
2558 printf(" Expire retained event\n");
2559 got_event=0;
2560 retained_id=0;
2561 do {
2562 result = saEvtEventPublish(event_handle, exp_data, 0, &event_id);
2563 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2564 if (result != SA_AIS_OK) {
2565 get_sa_error(result, result_buf, result_buf_len);
2566 printf("ERROR: event Publish result(1): %s\n", result_buf);
2567 goto evt_close;
2570 do {
2571 result = saEvtSelectionObjectGet(handle, &fd);
2572 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2573 if (result != SA_AIS_OK) {
2574 get_sa_error(result, result_buf, result_buf_len);
2575 printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
2576 /* error */
2577 return;
2581 * Wait for the event to expire, then subscribe. We shouldn't get
2582 * an event
2584 sleep(timeout);
2586 do {
2587 result = saEvtEventSubscribe(channel_handle,
2588 &subscribe_filters,
2589 subscription_id);
2590 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2592 if (result != SA_AIS_OK) {
2593 get_sa_error(result, result_buf, result_buf_len);
2594 printf("ERROR: event subscribe result: %s\n", result_buf);
2595 result = saEvtChannelClose(channel_handle);
2596 goto evt_free;
2599 pfd.fd = fd;
2600 pfd.events = POLLIN;
2601 nfd = poll(&pfd, 1, 1000);
2602 if (nfd != 0) {
2603 printf("ERROR: poll fds %d\n", nfd);
2604 if (nfd < 0) {
2605 perror("ERROR: poll error");
2607 /* Error */
2608 goto evt_free;
2611 do {
2612 result = saEvtEventUnsubscribe(channel_handle, subscription_id);
2613 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2614 if (result != SA_AIS_OK) {
2615 get_sa_error(result, result_buf, result_buf_len);
2616 printf("ERROR: unsubscribe result: %s\n", result_buf);
2617 goto evt_free;
2621 * Test 3:
2622 * Publish an event with a retention time.
2623 * subscribe.
2624 * wait for it.
2625 * unsubscribe
2626 * Clear it.
2627 * Then subscribe and make sure that the message isn't delivered.
2629 printf(" Clear event retention time\n");
2630 got_event=0;
2631 retained_id=0;
2632 do {
2633 result = saEvtEventPublish(event_handle, exp_data, 0, &event_id);
2634 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2635 if (result != SA_AIS_OK) {
2636 get_sa_error(result, result_buf, result_buf_len);
2637 printf("ERROR: event Publish result(2): %s\n", result_buf);
2638 goto evt_free;
2641 do {
2642 result = saEvtEventSubscribe(channel_handle,
2643 &subscribe_filters,
2644 subscription_id);
2645 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2646 if (result != SA_AIS_OK) {
2647 get_sa_error(result, result_buf, result_buf_len);
2648 printf("ERROR: event subscribe result: %s\n", result_buf);
2649 goto evt_free;
2652 pfd.fd = fd;
2653 pfd.events = POLLIN;
2654 nfd = poll(&pfd, 1, 1000);
2655 if (nfd <= 0) {
2656 printf("ERROR: poll fds %d\n", nfd);
2657 if (nfd < 0) {
2658 perror("ERROR: poll error");
2660 /* Error */
2661 goto evt_free;
2664 do {
2665 result = saEvtDispatch(handle, SA_DISPATCH_ONE);
2666 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2667 if (result != SA_AIS_OK) {
2668 get_sa_error(result, result_buf, result_buf_len);
2669 printf("ERROR: saEvtDispatch %s\n", result_buf);
2670 /* error */
2671 goto evt_free;
2674 if (!got_event) {
2675 printf("ERROR: retained event not recevied\n");
2676 goto evt_free;
2678 if (retained_id != event_id) {
2679 printf("ERROR: received the wrong event: e=%llx, a=%llx\n",
2680 (unsigned long long)event_id,
2681 (unsigned long long)retained_id);
2682 goto evt_free;
2685 do {
2686 result = saEvtEventUnsubscribe(channel_handle, subscription_id);
2687 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2688 if (result != SA_AIS_OK) {
2689 get_sa_error(result, result_buf, result_buf_len);
2690 printf("ERROR: unsubscribe result: %s\n", result_buf);
2691 goto evt_free;
2694 do {
2695 result = saEvtEventRetentionTimeClear(channel_handle, event_id);
2696 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2697 if (result != SA_AIS_OK) {
2698 get_sa_error(result, result_buf, result_buf_len);
2699 printf("ERROR: clear retention time result: %s\n", result_buf);
2700 goto evt_free;
2703 do {
2704 result = saEvtEventSubscribe(channel_handle,
2705 &subscribe_filters,
2706 subscription_id);
2707 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2708 if (result != SA_AIS_OK) {
2709 get_sa_error(result, result_buf, result_buf_len);
2710 printf("ERROR: event subscribe result: %s\n", result_buf);
2711 goto evt_free;
2714 pfd.fd = fd;
2715 pfd.events = POLLIN;
2716 nfd = poll(&pfd, 1, 1000);
2717 if (nfd != 0) {
2718 printf("ERROR: poll fds %d\n", nfd);
2719 if (nfd < 0) {
2720 perror("ERROR: poll error");
2722 /* Error */
2723 goto evt_free;
2727 * Test cleanup
2729 evt_free:
2730 do {
2731 result = saEvtEventFree(event_handle);
2732 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2733 if (result != SA_AIS_OK) {
2734 get_sa_error(result, result_buf, result_buf_len);
2735 printf("ERROR: event free result: %s\n", result_buf);
2738 evt_close:
2739 do {
2740 result = saEvtChannelClose(channel_handle);
2741 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2743 if (result != SA_AIS_OK) {
2744 get_sa_error(result, result_buf, result_buf_len);
2745 printf("ERROR: channel close result: %s\n", result_buf);
2747 evt_fin:
2748 do {
2749 result = saEvtFinalize(handle);
2750 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2752 if (result != SA_AIS_OK) {
2753 get_sa_error(result, result_buf, result_buf_len);
2754 printf("ERROR: Event Finalize result: %s\n", result_buf);
2756 printf("Done\n");
2760 void
2761 unlink_chan_callback(SaEvtSubscriptionIdT my_subscription_id,
2762 const SaEvtEventHandleT event_handle,
2763 const SaSizeT my_event_data_size)
2765 SaAisErrorT result;
2766 SaUint8T my_priority;
2767 SaTimeT my_retention_time;
2768 SaNameT my_publisher_name = {0, {0}};
2769 SaTimeT my_publish_time;
2770 SaEvtEventIdT my_event_id;
2771 SaEvtSubscriptionIdT exp_sub_id;
2773 printf(" unlink_chan_callback called(%d)\n", ++call_count);
2775 evt_pat_get_array.patternsNumber = 4;
2776 do {
2777 result = saEvtEventAttributesGet(event_handle,
2778 &evt_pat_get_array, /* patterns */
2779 &my_priority, /* priority */
2780 &my_retention_time, /* retention time */
2781 &my_publisher_name, /* publisher name */
2782 &my_publish_time, /* publish time */
2783 &my_event_id /* event_id */
2785 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2786 if (result != SA_AIS_OK) {
2787 get_sa_error(result, result_buf, result_buf_len);
2788 printf("ERROR: event get attr result: %s\n", result_buf);
2789 goto evt_free;
2792 if (my_event_id == event_id1) {
2793 exp_sub_id = sub1;
2794 } else if (my_event_id == event_id2) {
2795 exp_sub_id = sub2;
2796 } else {
2797 printf("ERROR: Received event %llx but not sent\n",
2798 (unsigned long long)my_event_id);
2799 goto evt_free;
2802 if (my_subscription_id != exp_sub_id) {
2803 printf("ERROR: sub ID: e=%x, a=%x\n",
2804 exp_sub_id, my_subscription_id);
2805 goto evt_free;
2808 evt_free:
2809 do {
2810 result = saEvtEventFree(event_handle);
2811 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2812 if (result != SA_AIS_OK) {
2813 get_sa_error(result, result_buf, result_buf_len);
2814 printf("ERROR: event free result: %s\n", result_buf);
2819 * Test channel unlink operations.
2820 * 1. Unlink channel.
2821 * 2. Open/create a channel, close channel, open channel.
2822 * 3. unlink channel, Open channel.
2823 * 4. Open/create, unlink channel, close channel, open channel.
2824 * 5. Open/create a channel, unlink channel, open/create channel, send
2825 * event on each.
2826 * 6. unlink all, close all.
2828 SaEvtCallbacksT unlink_callbacks = {
2829 open_callback,
2830 unlink_chan_callback
2832 void
2833 test_unlink_channel()
2835 SaEvtHandleT handle;
2836 SaEvtChannelHandleT channel_handle1;
2837 SaEvtChannelHandleT channel_handle2;
2838 SaEvtEventHandleT event_handle1;
2839 SaEvtEventHandleT event_handle2;
2840 SaEvtChannelOpenFlagsT flags1, flags2;
2841 SaNameT channel_name;
2842 SaAisErrorT result;
2844 struct pollfd pfd;
2845 int nfd;
2846 SaSelectionObjectT fd;
2847 int timeout = 5000;
2849 flags1 = SA_EVT_CHANNEL_PUBLISHER |
2850 SA_EVT_CHANNEL_SUBSCRIBER |
2851 SA_EVT_CHANNEL_CREATE;
2853 flags2 = SA_EVT_CHANNEL_PUBLISHER |
2854 SA_EVT_CHANNEL_SUBSCRIBER;
2857 printf("Test Channel Unlink operations:\n");
2859 do {
2860 result = saEvtInitialize (&handle, &unlink_callbacks,
2861 versions[0].version);
2862 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2864 if (result != SA_AIS_OK) {
2865 get_sa_error(result, result_buf, result_buf_len);
2866 printf("ERROR: Event Initialize result: %s\n", result_buf);
2867 goto unlink_exit;
2871 * 1. Unlink channel.
2873 * Unlink previously opened channel should return OK.
2874 * Unlink of non-existent channel should return error.
2876 printf(" 1 Channel unlink:\n");
2878 strcpy((char *)channel_name.value, channel);
2879 channel_name.length = strlen(channel);
2880 do {
2881 result = saEvtChannelUnlink(handle, &channel_name);
2882 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2883 if (result != SA_AIS_OK) {
2884 get_sa_error(result, result_buf, result_buf_len);
2885 printf("ERROR: channel unlink(1) result: %s\n", result_buf);
2886 goto unlink_exit;
2889 strcpy((char *)channel_name.value, unlink_channel);
2890 channel_name.length = strlen(unlink_channel);
2891 do {
2892 result = saEvtChannelUnlink(handle, &channel_name);
2893 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2894 if (result != SA_AIS_ERR_NOT_EXIST) {
2895 get_sa_error(result, result_buf, result_buf_len);
2896 printf("ERROR: channel unlink(2) result: %s\n", result_buf);
2897 goto unlink_exit;
2901 * 2. Open/create a channel, close channel, open channel.
2903 * Open/create the channel.
2904 * Close the channel.
2905 * Open without create. This should succeed in the B spec.
2907 printf(" 2 Channel open/close/open:\n");
2909 do {
2910 result = saEvtChannelOpen(handle, &channel_name, flags1, SA_TIME_MAX,
2911 &channel_handle1);
2912 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2915 if (result != SA_AIS_OK) {
2916 get_sa_error(result, result_buf, result_buf_len);
2917 printf("ERROR: channel open(1) result: %s\n", result_buf);
2918 goto unlink_exit;
2921 do {
2922 result = saEvtChannelClose(channel_handle1);
2923 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2925 if (result != SA_AIS_OK) {
2926 get_sa_error(result, result_buf, result_buf_len);
2927 printf("ERROR: channel close(1) result: %s\n", result_buf);
2928 goto unlink_exit;
2931 do {
2932 result = saEvtChannelOpen(handle, &channel_name, flags2, SA_TIME_MAX,
2933 &channel_handle1);
2934 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2935 if (result != SA_AIS_OK) {
2936 get_sa_error(result, result_buf, result_buf_len);
2937 printf("ERROR: channel open(2) result: %s\n", result_buf);
2938 goto unlink_exit;
2942 * 3. unlink channel, Open channel, close channel
2944 * Unlink the channel. Should mark for deletion but not
2945 * delete it since it is already open.
2946 * Open the channel without create. This should fail since
2947 * the channel is marked for deletion and a new version
2948 * hasn't been created.
2949 * Close channel.
2951 printf(" 3 Channel unlink/open/close:\n");
2953 do {
2954 result = saEvtChannelUnlink(handle, &channel_name);
2955 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2956 if (result != SA_AIS_OK) {
2957 get_sa_error(result, result_buf, result_buf_len);
2958 printf("ERROR: channel unlink result: %s\n", result_buf);
2959 goto unlink_exit;
2962 do {
2963 result = saEvtChannelOpen(handle, &channel_name, flags2, SA_TIME_MAX,
2964 &channel_handle2);
2965 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2966 if (result != SA_AIS_ERR_NOT_EXIST) {
2967 get_sa_error(result, result_buf, result_buf_len);
2968 printf("ERROR: channel open result: %s\n", result_buf);
2969 goto unlink_exit;
2971 do {
2972 result = saEvtChannelClose(channel_handle1);
2973 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2974 if (result != SA_AIS_OK) {
2975 get_sa_error(result, result_buf, result_buf_len);
2976 printf("ERROR: channel close(1) result: %s\n", result_buf);
2977 goto unlink_exit;
2982 * 4. Open/create, unlink channel, close channel, open channel.
2984 * Open/create the channel.
2985 * unlink the channel.
2986 * close the channel. This should delete the channel instance since
2987 * it was marked for deletion.
2988 * open the channel without create. This should fail since the
2989 * channel doesn't exist anymore.
2991 printf(" 4 Channel open/unlink/close/open:\n");
2992 do {
2993 result = saEvtChannelOpen(handle, &channel_name, flags1, SA_TIME_MAX,
2994 &channel_handle1);
2995 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
2996 if (result != SA_AIS_OK) {
2997 get_sa_error(result, result_buf, result_buf_len);
2998 printf("ERROR: channel open(1) result: %s\n", result_buf);
2999 goto unlink_exit;
3002 do {
3003 result = saEvtChannelUnlink(handle, &channel_name);
3004 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
3005 if (result != SA_AIS_OK) {
3006 get_sa_error(result, result_buf, result_buf_len);
3007 printf("ERROR: channel unlink result: %s\n", result_buf);
3008 goto unlink_exit;
3011 do {
3012 result = saEvtChannelClose(channel_handle1);
3013 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
3014 if (result != SA_AIS_OK) {
3015 get_sa_error(result, result_buf, result_buf_len);
3016 printf("ERROR: channel close(1) result: %s\n", result_buf);
3017 goto unlink_exit;
3020 do {
3021 result = saEvtChannelOpen(handle, &channel_name, flags2, SA_TIME_MAX,
3022 &channel_handle1);
3023 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
3024 if (result != SA_AIS_ERR_NOT_EXIST) {
3025 get_sa_error(result, result_buf, result_buf_len);
3026 printf("ERROR: channel open(2) result: %s\n", result_buf);
3027 goto unlink_exit;
3031 * 5. Open/create a channel, unlink channel, open/create channel, send
3032 * event on each.
3034 * Open/create.
3035 * unlink. Mark for deletion.
3036 * open/create. Create new channel of same name.
3037 * send event on each open channel. The events should be received on
3038 * separate channels.
3040 printf(" 5 Channel open/unlink/open/send:\n");
3042 do {
3043 result = saEvtChannelOpen(handle, &channel_name, flags1, SA_TIME_MAX,
3044 &channel_handle1);
3045 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
3046 if (result != SA_AIS_OK) {
3047 get_sa_error(result, result_buf, result_buf_len);
3048 printf("ERROR: channel open result: %s\n", result_buf);
3049 goto unlink_exit;
3052 do {
3053 result = saEvtChannelUnlink(handle, &channel_name);
3054 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
3055 if (result != SA_AIS_OK) {
3056 get_sa_error(result, result_buf, result_buf_len);
3057 printf("ERROR: channel unlink result: %s\n", result_buf);
3058 goto unlink_exit;
3061 do {
3062 result = saEvtChannelOpen(handle, &channel_name, flags1, SA_TIME_MAX,
3063 &channel_handle2);
3064 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
3065 if (result != SA_AIS_OK) {
3066 get_sa_error(result, result_buf, result_buf_len);
3067 printf("ERROR: channel open result: %s\n", result_buf);
3068 goto unlink_exit;
3071 do {
3072 result = saEvtEventSubscribe(channel_handle1,
3073 &subscribe_filters,
3074 sub1);
3075 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
3076 if (result != SA_AIS_OK) {
3077 get_sa_error(result, result_buf, result_buf_len);
3078 printf("ERROR: channel subscribe(1) result: %s\n", result_buf);
3079 goto unlink_exit;
3082 do {
3083 result = saEvtEventSubscribe(channel_handle2,
3084 &subscribe_filters,
3085 sub2);
3086 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
3087 if (result != SA_AIS_OK) {
3088 get_sa_error(result, result_buf, result_buf_len);
3089 printf("ERROR: channel subscribe(2) result: %s\n", result_buf);
3090 goto unlink_exit;
3093 retention_time = 0ULL;
3094 do {
3095 result = saEvtEventAllocate(channel_handle1, &event_handle1);
3096 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
3097 if (result != SA_AIS_OK) {
3098 get_sa_error(result, result_buf, result_buf_len);
3099 printf("ERROR: event allocate(1) result: %s\n", result_buf);
3100 goto unlink_exit;
3102 do {
3103 result = saEvtEventAttributesSet(event_handle1,
3104 &evt_pat_set_array,
3105 TEST_PRIORITY,
3106 retention_time,
3107 &test_pub_name);
3108 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
3109 if (result != SA_AIS_OK) {
3110 get_sa_error(result, result_buf, result_buf_len);
3111 printf("ERROR: event set(1) result: %s\n", result_buf);
3112 goto unlink_exit;
3115 do {
3116 result = saEvtEventAllocate(channel_handle2, &event_handle2);
3117 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
3118 if (result != SA_AIS_OK) {
3119 get_sa_error(result, result_buf, result_buf_len);
3120 printf("ERROR: event allocate(2) result: %s\n", result_buf);
3121 goto unlink_exit;
3123 do {
3124 result = saEvtEventAttributesSet(event_handle2,
3125 &evt_pat_set_array,
3126 TEST_PRIORITY,
3127 retention_time,
3128 &test_pub_name);
3129 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
3130 if (result != SA_AIS_OK) {
3131 get_sa_error(result, result_buf, result_buf_len);
3132 printf("ERROR: event set(2) result: %s\n", result_buf);
3133 goto unlink_exit;
3136 do {
3137 result = saEvtEventPublish(event_handle1, 0, 0, &event_id1);
3138 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
3139 if (result != SA_AIS_OK) {
3140 get_sa_error(result, result_buf, result_buf_len);
3141 printf("ERROR: event publish(1) result: %s\n", result_buf);
3142 goto unlink_exit;
3144 do {
3145 result = saEvtEventPublish(event_handle2, 0, 0, &event_id2);
3146 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
3147 if (result != SA_AIS_OK) {
3148 get_sa_error(result, result_buf, result_buf_len);
3149 printf("ERROR: event publish(2) result: %s\n", result_buf);
3150 goto unlink_exit;
3154 do {
3155 result = saEvtSelectionObjectGet(handle, &fd);
3156 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
3157 if (result != SA_AIS_OK) {
3158 get_sa_error(result, result_buf, result_buf_len);
3159 printf("ERROR: select object get result: %s\n", result_buf);
3160 goto unlink_exit;
3164 * We should see a total of two events processed, not four
3165 * as if both events were recevied on both channels.
3167 call_count = 0;
3168 do {
3169 pfd.fd = fd;
3170 pfd.events = POLLIN;
3171 nfd = poll(&pfd, 1, timeout);
3172 if (nfd <= 0) {
3173 if (nfd < 0) {
3174 perror("ERROR: poll error");
3175 goto unlink_exit;
3177 } else {
3179 do {
3180 result = saEvtDispatch(handle, SA_DISPATCH_ONE);
3181 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
3182 if (result != SA_AIS_OK) {
3183 get_sa_error(result, result_buf, result_buf_len);
3184 printf("ERROR: saEvtDispatch %s\n", result_buf);
3185 goto unlink_exit;
3188 } while (nfd > 0);
3190 if (call_count != 2) {
3191 printf("ERROR: processed %d events\n", call_count);
3192 goto unlink_exit;
3197 * 6. unlink all, close all.
3199 * close all open channels.
3200 * unlink the channel.
3201 * open without create the channel. Verify that the channel no
3202 * longer exists.
3204 printf(" 6 Channel unlink all/close all/open:\n");
3206 unlink_exit:
3207 saEvtChannelClose(channel_handle1);
3208 saEvtChannelClose(channel_handle2);
3209 saEvtChannelUnlink(handle, &channel_name);
3210 do {
3211 result = saEvtFinalize(handle);
3212 } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
3213 if (result != SA_AIS_OK) {
3214 get_sa_error(result, result_buf, result_buf_len);
3215 printf("ERROR: Event Finalize result: %s\n", result_buf);
3218 printf("Done\n");
3221 int main (void)
3223 test_initialize ();
3224 test_channel();
3225 test_event();
3226 test_multi_channel1();
3227 test_multi_channel2();
3228 test_multi_channel3();
3229 test_retention();
3230 test_unlink_channel();
3232 return (0);
3236 * vi: set autoindent tabstop=4 shiftwidth=4 :