Add health check to the MC-listener application
[ric-app/mc.git] / sidecars / listener / test_rmr_em.c
1 // vim: ts=4 sw=4 noet:
2 /*
3 --------------------------------------------------------------------------------
4         Copyright (c) 2018-2019 AT&T Intellectual Property.
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10            http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 --------------------------------------------------------------------------------
18 */
19
20 /*
21         Mnemonic:       test_rmr_em.c.
22         Abstract:       Emulates some RMR functions (message receipt mostly)
23                                 so that we can unit test.
24
25                                 This file should be included by the test programme BEFORE
26                                 any module which uses RMR functions is included
27
28         Date:           10 December 2019
29         Author:         E. Scott Daniels
30 */
31
32 #include <rmr/rmr.h>
33 #include <rmr/RIC_message_types.h>
34
35
36 /*
37         The first call will generate a timeout for testing. All others will
38         succeed with a message.
39 */
40 extern rmr_mbuf_t* RMR_torcv_msg( void* ctx, rmr_mbuf_t* old_msg, int ms_to ) {
41         static int timeout = 0;
42         static int hcheck = 0;
43
44         if( old_msg == NULL ) {
45                 old_msg = rmr_alloc_msg( ctx, 256 );
46                 if( old_msg == NULL ) {
47                         return NULL;
48                 }
49         }
50
51         timeout = ! timeout;                                    // every other message results in a timeout
52         if( timeout ) {
53                 old_msg->state = RMR_ERR_TIMEOUT;
54                 return old_msg; 
55         }
56
57         if( !hcheck ) {                                                                 // send one health check message
58                 old_msg->mtype = RIC_HEALTH_CHECK_REQ;
59                 old_msg->state = 0;
60                 hcheck = 1;
61                 return old_msg;
62         }
63
64
65         snprintf( old_msg->payload, rmr_payload_size( old_msg ), "DUMMY MESSAGE" );
66         old_msg->mtype = TEST_MTYPE;
67         old_msg->len = strlen( old_msg->payload );
68         old_msg->sub_id = -1;
69         old_msg->state = 0;
70
71         return old_msg;
72 }
73
74 /*
75         Always successful if a message was passed in.
76 */
77 extern rmr_mbuf_t* RMR_rts_msg( void* ctx, rmr_mbuf_t* msg ) {
78         if( msg != NULL ) {
79                 msg->state = 0;
80         }
81
82         return msg;
83 }
84
85 /*
86         Generates "short" reads 9 out of 10 times so that we can
87         drive the buffer construction code that otherwise wouldn't
88         be driven since long reads are usually the case.
89 */
90 extern int Read( int fd, char* dest, int max ) {
91         static int count = -1;
92
93         count++;
94
95         if( count % 10 ) {
96                 return read( fd, dest, max );           // return long read
97         }
98
99         return read( fd, dest, 3 );             // short read
100 }
101
102 // ----------- defines that point included code here --------------------
103 #define rmr_torcv_msg RMR_torcv_msg
104 #define rmr_rts_msg RMR_rts_msg
105 #define read Read