CI: Add silent cmake SonarCloud scan
[ric-plt/lib/rmr.git] / test / app_test / run_exrts_test.sh
1 #!/usr/bin/env ksh
2 # vim: ts=4 sw=4 noet :
3 #==================================================================================
4 #    Copyright (c) 2019-2021 Nokia
5 #    Copyright (c) 2018-2021 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:       run_exrts_test.ksh
23 #       Abstract:       This is a simple script to set up and run the v_send/ex_rts_receive
24 #                               processes for some library validation on top of nng. This test
25 #                               starts these processes to verify that when a receiver has an ack 
26 #                               message larger than the received message it is able to allocate
27 #                               a new payload which can be returned via the RMR return to sender
28 #                               function.
29 #
30 #                               Example command line:
31 #                                       # run with 10 caller threads sending 10,000 meessages each, 
32 #                                       # 5 receivers, and a 10 mu-s delay between each caller send
33 #                                       ksh ./run_lcall_test.ksh -d 10 -n 10000 -r 5 -c 10
34 #
35 #       Date:           28 October 2019
36 #       Author:         E. Scott Daniels
37 # ---------------------------------------------------------------------------------
38
39 ulimit -c unlimited
40
41 # driven with -L sender or -L receiver on the command line
42 # run something though valgrind to check for leaks; requires valgind
43 function leak_anal {
44         valgrind  -v --leak-resolution=high --leak-check=yes $opt "$@"
45 }
46
47 # The sender and receivers are run asynch. Their exit statuses are captured in a
48 # file in order for the 'main' to pick them up easily.
49 #
50 function run_sender {
51         if (( la_sender ))
52         then
53                 leak_anal ./v_sender ${nmsg:-10} ${delay:-100000} ${mtype_start_stop:-0:1}  >/tmp/la.log 2>&1
54         else
55                 ./v_sender ${nmsg:-10} ${delay:-100000} ${mtype_start_stop:-0:1}
56         fi
57         echo $? >/tmp/PID$$.src         # must communicate state back via file b/c asynch
58 }
59
60 # $1 is the instance so we can keep logs separate
61 function run_rcvr {
62         typeset port
63
64         port=$(( 4460 + ${1:-0} ))
65         export RMR_RTG_SVC=$(( 9990 + $1 ))
66         if (( la_receiver ))
67         then
68                 leak_anal ./ex_rts_receiver $copyclone -p $port >/tmp/la.log 2>&1
69         else
70                 ./ex_rts_receiver $copyclone -p $port
71         fi
72         echo $? >/tmp/PID$$.$1.rrc
73 }
74
75 #       Drop a contrived route table in such that the sender sends each message to n
76 #       receivers.
77 #
78 function set_rt {
79         typeset port=4460
80         typeset groups="127.0.0.1:4460"
81         for (( i=1; i < ${1:-3}; i++ ))
82         do
83                 groups="$groups,127.0.0.1:$((port+i))"
84         done
85
86         cat <<endKat >ex_rts.rt
87                 newrt | start
88                 mse |0 | 0 | $groups
89                 mse |1 | 10 | $groups
90                 mse |2 | 20 | $groups
91                 rte |3 | $groups
92                 rte |4 | $groups
93                 rte |5 | $groups
94                 rte |6 | $groups
95                 rte |7 | $groups
96                 rte |8 | $groups
97                 rte |9 | $groups
98                 rte |10 | $groups
99                 rte |11 | $groups
100                 newrt | end
101 endKat
102
103         cat ex_rts.rt
104 }
105
106 # ---------------------------------------------------------
107
108 if [[ ! -f local.rt ]]          # we need the real host name in the local.rt; build one from mask if not there
109 then
110         hn=$(hostname)
111         sed "s!%%hostname%%!$hn!" rt.mask >local.rt
112 fi
113
114 export EX_CFLAGS=""
115 export RMR_ASYNC_CONN=0         # ensure we don't lose first msg as drops waiting for conn look like errors
116 nmsg=100                                        # total number of messages to be exchanged (-n value changes)
117 delay=500                                       # microsec sleep between msg 1,000,000 == 1s
118 wait=1
119 rebuild=0
120 verbose=0
121 nrcvrs=1                                        # this is sane, but -r allows it to be set up
122 use_installed=0
123 mtype_start_stop=""
124
125 while [[ $1 == -* ]]
126 do
127         case $1 in
128                 -c)     copyclone="-c $2"; shift;;
129                 -B)     rebuild=1;;
130                 -d)     delay=${2//,/}; shift;;                         # delay in micro seconds allow 1,000 to make it easier on user
131                 -i)     use_installed=1;;
132                 -L)     leak_anal=$2; shift;;
133                 -m)     mtype_start_stop="$2"; shift;;
134                 -M)     mt_call_flag="EX_CFLAGS=-DMTC=1";;      # turn on mt-call receiver option
135                 -N)     ;;                                                                      # ignored for back compat; nng not supported
136                 -n)     nmsg=$2; shift;;
137                 -r) nrcvrs=$2; shift;;
138                 -S)     ;;                                                                      # ignored for back compat; si95 is the only supported transport
139                 -v)     verbose=1;;
140
141                 *)      echo "unrecognised option: $1"
142                         echo "usage: $0 [-B] [-c caller-threads] [-d micor-sec-delay] [-i] [-M] [-m mtype] [-n num-msgs] [-r num-receivers] [-S] [-v]"
143                         echo "  -B forces a rebuild which will use .build"
144                         echo "  -i will use installed libraries (/usr/local) and cause -B to be ignored if supplied)"
145                         echo "  -L {sender|receiver} run the sender or recevier code under valgrind for leak analysis (output to /tmp/la.log)"
146                         echo "  -m mtype  will set the stopping (max) message type; sender will loop through 0 through mtype-1"
147                         echo "  -m start:stop  will set the starting and stopping mtypes; start through stop -1"
148                         echo "  -M enables mt-call receive processing to test the RMR asynch receive pthread"
149                         echo "  -N enables NNG based testing (default)"
150                         echo "  -S enables SI95 based testing"
151                         echo ""
152                         echo "The receivers will run until they have not received a message for a few seconds. The"
153                         echo "sender will send the requested number of messages, 10 by default."
154                         exit 1
155                         ;;
156         esac
157
158         shift
159 done
160
161 # set leak analysis (do not do this from any automated tests)
162 case $leak_anal in 
163         s*)     la_sender=1;;
164         r*)     la_receiver=1;;
165 esac
166
167 if (( verbose ))
168 then
169         echo "2" >.verbose
170         export RMR_VCTL_FILE=".verbose"
171 fi
172
173 if (( use_installed ))                  # point at installed library
174 then
175         export LD_LIBRARY_PATH=${LD_LIBRARY_PATH-:/usr/local/lib}       # use caller's or set sane default
176 else
177         if (( rebuild ))
178         then
179                 build_path=../../.build         # if we rebuild we can insist that it is in .build :)
180                 set -e
181                 $SHELL ./rebuild.ksh
182                 set +e
183         else
184                 build_path=${BUILD_PATH:-"../../.build"}        # we prefer .build at the root level, but allow user option
185
186                 if [[ ! -d $build_path ]]
187                 then
188                         echo "cannot find build in: $build_path"
189                         echo "either create, and then build RMr, or set BUILD_PATH as an evironment var before running this"
190                         exit 1
191                 fi
192         fi
193
194         if [[ -z $LD_LIBRARY_PATH ]]            # cmake test will set this up and we must honour
195         then
196                 if [[ -d $build_path/lib64 ]]
197                 then
198                         export LD_LIBRARY_PATH=$build_path:$build_path/lib64:$LD_LIBRARY_PATH
199                 else
200                         export LD_LIBRARY_PATH=$build_path:$build_path/lib:$LD_LIBRARY_PATH
201                 fi
202         fi
203 fi
204
205 export LIBRARY_PATH=$LD_LIBRARY_PATH
206 export RMR_SEED_RT=./ex_rts.rt
207
208 set_rt $nrcvrs                                                                                                          # set up the rt for n receivers
209
210 if ! build_path=$build_path make -B $mt_call_flag v_sender ex_rts_receiver >/tmp/PID$$.log 2>&1                 # for sanity, always rebuild test binaries
211 then
212         echo "[FAIL] cannot make binaries"
213         echo "====="
214         env
215         echo "====="
216         cat /tmp/PID$$.log
217         rm -f /tmp/PID$$*
218         exit 1
219 fi
220
221 echo "<RUN> binaries built"
222
223 for (( i=0; i < nrcvrs; i++  ))
224 do
225         run_rcvr $i &
226 done
227
228 sleep 2                         # let receivers init so we don't shoot at empty targets
229 if (( verbose ))
230 then
231         netstat -an|grep LISTEN
232 fi
233 run_sender &
234
235 wait
236
237
238 for (( i=0; i < nrcvrs; i++ ))          # collect return codes
239 do
240         head -1 /tmp/PID$$.$i.rrc | read x
241         (( rrc += x ))
242 done
243
244 head -1 /tmp/PID$$.src | read src
245
246 if (( !! (src + rrc) ))
247 then
248         echo "[FAIL] sender rc=$src  receiver rc=$rrc"
249 else
250         echo "[PASS] sender rc=$src  receiver rc=$rrc"
251         rm -f ex_rts.rt
252 fi
253
254 rm /tmp/PID$$.*
255 rm -f .verbose
256
257 exit $(( !! (src + rrc) ))
258