08807fc5d95885c851ff73e9fba268aefb65678f
[ric-plt/lib/rmr.git] / test / app_test / run_exrts_test.ksh
1 #!/usr/bin/env ksh
2 # vim: ts=4 sw=4 noet :
3 #==================================================================================
4 #    Copyright (c) 2019 Nokia
5 #    Copyright (c) 2018-2019 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
40 # The sender and receivers are run asynch. Their exit statuses are captured in a
41 # file in order for the 'main' to pick them up easily.
42 #
43 function run_sender {
44         ./v_sender ${nmsg:-10} ${delay:-100000} ${mtype_start_stop:-0:1} 
45         echo $? >/tmp/PID$$.src         # must communicate state back via file b/c asynch
46 }
47
48 # $1 is the instance so we can keep logs separate
49 function run_rcvr {
50         typeset port
51
52         port=$(( 4460 + ${1:-0} ))
53         export RMR_RTG_SVC=$(( 9990 + $1 ))
54         ./ex_rts_receiver $port
55         echo $? >/tmp/PID$$.$1.rrc
56 }
57
58 #       Drop a contrived route table in such that the sender sends each message to n
59 #       receivers.
60 #
61 function set_rt {
62         typeset port=4460
63         typeset groups="localhost:4460"
64         for (( i=1; i < ${1:-3}; i++ ))
65         do
66                 groups="$groups,localhost:$((port+i))"
67         done
68
69         cat <<endKat >ex_rts.rt
70                 newrt | start
71                 mse |0 | 0 | $groups
72                 mse |1 | 10 | $groups
73                 mse |2 | 20 | $groups
74                 rte |3 | $groups
75                 rte |4 | $groups
76                 rte |5 | $groups
77                 rte |6 | $groups
78                 rte |7 | $groups
79                 rte |8 | $groups
80                 rte |9 | $groups
81                 rte |10 | $groups
82                 rte |11 | $groups
83                 newrt | end
84 endKat
85
86         cat ex_rts.rt
87 }
88
89 # ---------------------------------------------------------
90
91 if [[ ! -f local.rt ]]          # we need the real host name in the local.rt; build one from mask if not there
92 then
93         hn=$(hostname)
94         sed "s!%%hostname%%!$hn!" rt.mask >local.rt
95 fi
96
97 export EX_CFLAGS=""
98 export RMR_ASYNC_CONN=0         # ensure we don't lose first msg as drops waiting for conn look like errors
99 nmsg=100                                        # total number of messages to be exchanged (-n value changes)
100 delay=500                                       # microsec sleep between msg 1,000,000 == 1s
101 wait=1
102 rebuild=0
103 verbose=0
104 nrcvrs=1                                        # this is sane, but -r allows it to be set up
105 use_installed=0
106 mtype_start_stop=""
107
108 while [[ $1 == -* ]]
109 do
110         case $1 in
111                 -c)     cthreads=$2; shift;;
112                 -B)     rebuild=1;;
113                 -d)     delay=${2//,/}; shift;;                         # delay in micro seconds allow 1,000 to make it easier on user
114                 -i)     use_installed=1;;
115                 -m)     mtype_start_stop="$2"; shift;;
116                 -M)     mt_call="EX_CFLAGS=-DMTC=1";;                                   # turn on mt-call receiver option
117                 -n)     nmsg=$2; shift;;
118                 -r) nrcvrs=$2; shift;;
119                 -v)     verbose=1;;
120
121                 *)      echo "unrecognised option: $1"
122                         echo "usage: $0 [-B] [-c caller-threads] [-d micor-sec-delay] [-i] [-M] [-m mtype] [-n num-msgs] [-r num-receivers] [-v]"
123                         echo "  -B forces a rebuild which will use .build"
124                         echo "  -i will use installed libraries (/usr/local) and cause -B to be ignored if supplied)"
125                         echo "  -m mtype  will set the stopping (max) message type; sender will loop through 0 through mtype-1"
126                         echo "  -m start:stop  will set the starting and stopping mtypes; start through stop -1"
127                         echo "  -M enables mt-call receive processing to test the RMR asynch receive pthread"
128                         echo ""
129                         echo "The receivers will run until they have not received a message for a few seconds. The"
130                         echo "sender will send the requested number of messages, 10 by default."
131                         exit 1
132                         ;;
133         esac
134
135         shift
136 done
137
138
139 if (( verbose ))
140 then
141         echo "2" >.verbose
142         export RMR_VCTL_FILE=".verbose"
143 fi
144
145 if (( use_installed ))                  # point at installed library
146 then
147         export LD_LIBRARY_PATH=/usr/local/lib
148         export LIBRARY_PATH=$LD_LIBRARY_PATH
149 else
150         if (( rebuild ))
151         then
152                 build_path=../../.build         # if we rebuild we can insist that it is in .build :)
153                 set -e
154                 ksh ./rebuild.ksh
155                 set +e
156         else
157                 build_path=${BUILD_PATH:-"../../.build"}        # we prefer .build at the root level, but allow user option
158
159                 if [[ ! -d $build_path ]]
160                 then
161                         echo "cannot find build in: $build_path"
162                         echo "either create, and then build RMr, or set BUILD_PATH as an evironment var before running this"
163                         exit 1
164                 fi
165         fi
166
167         if [[ -d $build_path/lib64 ]]
168         then
169                 export LD_LIBRARY_PATH=$build_path:$build_path/lib64
170         else
171                 export LD_LIBRARY_PATH=$build_path:$build_path/lib
172         fi
173 fi
174
175 export LIBRARY_PATH=$LD_LIBRARY_PATH
176 export RMR_SEED_RT=./ex_rts.rt
177
178 set_rt $nrcvrs                                                                                                          # set up the rt for n receivers
179
180 if ! make $mt_call -B v_sender ex_rts_receiver >/tmp/PID$$.log 2>&1                     # for sanity, always rebuild test binaries
181 then
182         echo "[FAIL] cannot make binaries"
183         cat /tmp/PID$$.log
184         rm -f /tmp/PID$$*
185         exit 1
186 fi
187
188 echo "<RUN> binaries built"
189
190 for (( i=0; i < nrcvrs; i++  ))
191 do
192         run_rcvr $i &
193 done
194
195 sleep 2                         # let receivers init so we don't shoot at empty targets
196 if (( verbose ))
197 then
198         netstat -an|grep LISTEN
199 fi
200 run_sender &
201
202 wait
203
204
205 for (( i=0; i < nrcvrs; i++ ))          # collect return codes
206 do
207         head -1 /tmp/PID$$.$i.rrc | read x
208         (( rrc += x ))
209 done
210
211 head -1 /tmp/PID$$.src | read src
212
213 if (( !! (src + rrc) ))
214 then
215         echo "[FAIL] sender rc=$src  receiver rc=$rrc"
216 else
217         echo "[PASS] sender rc=$src  receiver rc=$rrc"
218         rm -f ex_rts.rt
219 fi
220
221 rm /tmp/PID$$.*
222 rm -f .verbose
223
224 exit $(( !! (src + rrc) ))
225