Add better support for manual app testing
[ric-plt/lib/rmr.git] / test / app_test / run_rr_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_multi_test.ksh
23 #       Abstract:       This is a simple script to set up and run the basic send/receive
24 #                               processes for some library validation on top of nng. This
25 #                               particular tests starts several receivers and creates a route table
26 #                               which causes messages to be sent round robin to all of the receivers.
27 #                               The number of messages command line parameter (-n) will be the number
28 #                               of messages that each receiver should expect; the sender will be asked
29 #                               to send r times that many messages so that as they are round robbined
30 #                               each receiver should get the same number of messages.
31 #
32 #                               Example command line:
33 #                                       ksh ./run_rr_test.ksh           # default 10 messages at 1 msg/sec
34 #                                       ksh ./run_rr_test.ksh -d 100 -n 10000 # send 10k messages with 100ms delay between
35 #
36 #       Date:           24 April 2019
37 #       Author:         E. Scott Daniels
38 # ---------------------------------------------------------------------------------
39
40
41 # The sender and receivers are run asynch. Their exit statuses are captured in a
42 # file in order for the 'main' to pick them up easily.
43 #
44 function run_sender {
45         export RMR_RTG_SVC=8990
46         ./sender $(( nmsg * nrcvrs ))  $delay $max_mtype
47         echo $? >/tmp/PID$$.src         # must communicate state back via file b/c asynch
48 }
49
50 # $1 is the instance so we can keep logs separate
51 function run_rcvr {
52         typeset port
53
54         port=$(( 4560 + ${1:-0} ))
55         export RMR_RTG_SVC=$(( 9990 + $1 ))
56         ./receiver $nmsg $port
57         echo $? >/tmp/PID$$.$1.rrc
58 }
59
60 #
61 #       Drop a contrived route table in such that the sender sends each message to n
62 #       receivers.
63 #
64 function set_rt {
65         typeset port=4560
66         typeset endpoints="127.0.0.1:4560"
67         for (( i=1; i < ${1:-3}; i++ ))
68         do
69                 endpoints="$endpoints,127.0.0.1:$((port+i))"
70         done
71
72         cat <<endKat >rr.rt
73                 newrt |start
74                 rte |0 | $endpoints  |0
75                 rte |1 | $endpoints  |10
76                 mse |2 | 20 | $endpoints                # new style mtype/subid entry
77                 rte |3 | $endpoints  | -1
78                 rte |4 | $endpoints  | -1
79                 rte |5 | $endpoints  | -1
80                 rte |6 | $endpoints  | -1
81                 rte |7 | $endpoints  | -1
82                 rte |8 | $endpoints  | -1
83                 rte |9 | $endpoints  | -1
84                 rte |10 | $endpoints  | -1
85                 rte |11 | $endpoints  | -1
86                 newrt |end
87 endKat
88
89 }
90
91 # ---------------------------------------------------------
92
93 if [[ ! -f local.rt ]]          # we need the real host name in the local.rt; build one from mask if not there
94 then
95         hn=$(hostname)
96         sed "s!%%hostname%%!$hn!" rt.mask >local.rt
97 fi
98
99 export RMR_ASYNC_CONN=0         # ensure we don't lose first msg as drops waiting for conn look like errors
100 nmsg=10                                         # total number of messages to be exchanged (-n value changes)
101 delay=1000                                      # microsec sleep between msg 1,000,000 == 1s (shorter than others b/c/ we are sending to multiple)
102 wait=1
103 rebuild=0
104 nopull=""
105 verbose=0
106 max_mtype=1                                     # causes all msgs to go with type 1; use -M to set up, but likely harder to validate
107 nrcvrs=3                                        # this is sane, but -r allows it to be set up
108 force_make=0
109 si=""
110
111 while [[ $1 == -* ]]
112 do
113         case $1 in
114                 -B)     rebuild=1;;
115                 -b)     rebuild=1; nopull="nopull";;            # build without pulling
116                 -d)     delay=$2; shift;;
117                 -m) max_mtype=$2; shift;;
118                 -M) force_make=1;;
119                 -n)     nmsg=$2; shift;;
120                 -N) ;;                                                          # ignored for back compat; nng nolonger supported
121                 -r)     nrcvrs=$2; shift;;
122                 -S)     ;;                                                      # ignored for back compat; only si95 is supported
123                 -v)     verbose=1;;
124
125                 *)      echo "unrecognised option: $1"
126                         echo "usage: $0 [-B] [-d micor-sec-delay] [-M] [-n num-msgs] [-S]"
127                         echo "  -B forces a rebuild which will use .build"
128                         echo "  -M force test applications to be remade"
129                         echo "  -S build/test SI95 based binaries"
130                         exit 1
131                         ;;
132         esac
133
134         shift
135 done
136
137 if (( verbose ))
138 then
139         echo "2" >.verbose
140         export RMR_VCTL_FILE=".verbose"
141 fi
142
143 if (( rebuild ))
144 then
145         set -e
146         $SHELL ./rebuild.ksh $nopull | read build_path
147         set +e
148 else
149         build_path=${BUILD_PATH:-"../../.build"}        # we prefer .build at the root level, but allow user option
150
151         if [[ ! -d $build_path ]]
152         then
153                 echo "cannot find build in: $build_path"
154                 echo "either create, and then build RMr, or set BUILD_PATH as an evironment var before running this"
155                 exit 1
156         fi
157 fi
158
159 if [[ -z $LD_LIBRARY_PATH ]]                    # honour if cmake test sets
160 then
161         if [[ -d $build_path/lib64 ]]
162         then
163                 export LD_LIBRARY_PATH=$build_path:$build_path/lib64:$LD_LIBRARY_PATH
164         else
165                 export LD_LIBRARY_PATH=$build_path:$build_path/lib:$LD_LIBRARY_PATH
166         fi
167 fi
168
169 export LIBRARY_PATH=$LD_LIBRARY_PATH
170 export RMR_SEED_RT=./rr.rt
171
172 set_rt $nrcvrs
173
174 if (( rebuild || force_make )) || [[ ! -f ./sender || ! -f ./receiver ]]
175 then
176         if ! make >/dev/null 2>&1
177         then
178                 echo "[FAIL] cannot find sender and/or receiver binary, and cannot make them.... humm?"
179                 exit 1
180         fi
181 fi
182
183 for (( i=0; i < nrcvrs; i++ ))          # start the receivers with an instance number
184 do
185         run_rcvr $i &
186 done
187
188 sleep 2                                 # wait to start sender else we might send before receivers up and drop messages
189 run_sender &
190
191 wait
192
193
194 for (( i=0; i < nrcvrs; i++ ))          # collect return codes
195 do
196         head -1 /tmp/PID$$.$i.rrc | read x
197         (( rrc += x ))
198 done
199
200 head -1 /tmp/PID$$.src | read src
201
202 if (( !! (src + rrc) ))
203 then
204         echo "[FAIL] sender rc=$src  receiver rc=$rrc"
205 else
206         echo "[PASS] sender rc=$src  receiver rc=$rrc"
207 fi
208
209 rm /tmp/PID$$.*
210 rm -f .verbose
211 rm -f rr.rt
212
213 exit $(( !! (src + rrc) ))
214