CI: Add silent cmake SonarCloud scan
[ric-plt/lib/rmr.git] / test / test_msg_support.c
1 // vi: ts=4 sw=4 noet :
2 /*
3 ==================================================================================
4             Copyright (c) 2019-2020 Nokia
5             Copyright (c) 2018-2020 AT&T Intellectual Property.
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11            http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 */
20
21 /*
22         Mnemonic:       test_msg_support.c
23         Abstract:       Support for testing with messages; requires RMR defs, so not
24                                 to be included for things like ring tests etc.
25         Author:         E. Scott Daniels
26         Date:           14 April 2020   (AKD)
27 */
28
29 #ifndef _test_msg_support_c
30 #define _test_msg_support_c
31
32 #include <signal.h>
33 #include <string.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38
39 int test_support_xact_count = 0;
40 /*
41         Send a 'burst' of messages to drive some send retry failures to increase RMr coverage
42         by handling the retry caee.
43 */
44 static void send_n_msgs( void* ctx, int n ) {
45         rmr_mbuf_t*     msg;                    // message buffers
46         int i;
47
48         msg = rmr_alloc_msg( ctx,  1024 );
49         if( ! msg ) {
50                 fprintf( stderr, "<FAIL> mass send of %d messages couldn't allocate message!\n", n );
51                 return;
52         }
53
54         fprintf( stderr, "<INFO> mass send of %d messages\n", n );
55         for( i = 0; i < n; i++ ) {
56                 msg->len = 100;
57                 msg->mtype = 1;
58                 msg->state = 999;
59
60                 snprintf( msg->xaction, 32, "%015d", test_support_xact_count++ );               // simple transaction id so we can test receive specific and ring stuff
61
62                 errno = 999;
63                 msg = rmr_send_msg( ctx, msg );
64                 if( msg && msg->state != 0 ) {
65                         fprintf( stderr, "<WARN> mass send failed: state=%d type=%d\n", msg->state, msg->mtype );
66                 }
67         }
68 }
69
70
71 /*
72         Allow test to reset the transaction id counter.
73 */
74 static void reset_xact_count() {
75         test_support_xact_count = 0;
76 }
77
78 /*
79         Refresh or allocate a message with some default values
80 */
81 static rmr_mbuf_t* fresh_msg( void* ctx, rmr_mbuf_t* msg ) {
82         if( ! msg )  {
83                 msg = rmr_alloc_msg( ctx, 2048 );
84         }
85
86         msg->mtype = 0;
87         msg->sub_id = -1;
88         msg->state = 0;
89         msg->len = 100;
90
91         return msg;
92 }
93
94 #endif